Module: Fclay::Attachment
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/fclay/attachment.rb
Constant Summary collapse
- CALLBACKS =
[:process,:upload,:delete]
Instance Attribute Summary collapse
-
#file ⇒ Object
Returns the value of attribute file.
Class Method Summary collapse
- .resolve_file_url(navigation_complex_id, type, file_name, style = nil) ⇒ Object
- .upload(type, id) ⇒ Object
Instance Method Summary collapse
- #create_dirs ⇒ Object
- #delete_files ⇒ Object
- #delete_local_files ⇒ Object
- #delete_remote_files ⇒ Object
- #delete_tmp_file ⇒ Object
- #fclay_attachment_presence ⇒ Object
- #fetch_file_name ⇒ Object
- #file_size_mb ⇒ Object
- #file_url(style = nil) ⇒ Object
- #local_file_dir(style = nil) ⇒ Object
- #local_file_path(style = nil) ⇒ Object
- #local_file_url(style = nil) ⇒ Object
- #need_upload ⇒ Object
- #process_file ⇒ Object
- #remote_file_path(style = nil) ⇒ Object
- #remote_file_url(style = nil) ⇒ Object
- #set_file_size(style = nil) ⇒ Object
- #short_local_file_url(style = nil) ⇒ Object
- #upload ⇒ Object
- #upload_later ⇒ Object
Instance Attribute Details
#file ⇒ Object
Returns the value of attribute file.
28 29 30 |
# File 'lib/fclay/attachment.rb', line 28 def file @file end |
Class Method Details
.resolve_file_url(navigation_complex_id, type, file_name, style = nil) ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/fclay/attachment.rb', line 215 def self.resolve_file_url ,type,file_name,style=nil return "" if file_name.nil? || type.nil? path = "http://s3.amazonaws.com/#{Fclay.remote_storage.bucket_name}" path += "/navigation_complex/#{}" if path += "/#{type}" path += "/#{style.to_s}" if style path += "/#{file_name}" path end |
.upload(type, id) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/fclay/attachment.rb', line 53 def self.upload type,id type = type.constantize uploading_object = type.find(id) return unless uploading_object.need_upload content_type = uploading_object.try(:content_type) bucket = Fclay.remote_storage.bucket_object (uploading_object.class.[:styles] || [nil]).each do |style| obj = bucket.object(uploading_object.remote_file_path(style)) obj.put({ body: File.read(uploading_object.local_file_path(style)), acl: "public-read", content_type: uploading_object.class.[:content_type] }) end uploading_object.update_attributes(:file_status => 'idle', :file_location => Fclay.remote_storage.name) uploading_object.delete_local_files uploading_object.try(:uploaded) end |
Instance Method Details
#create_dirs ⇒ Object
140 141 142 143 144 145 146 |
# File 'lib/fclay/attachment.rb', line 140 def create_dirs (self.class.[:styles] || [nil]).each do |style| FileUtils.mkdir_p(local_file_dir(style)) end end |
#delete_files ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/fclay/attachment.rb', line 30 def delete_files case file_location when 's3' delete_remote_files when 'local' delete_local_files end end |
#delete_local_files ⇒ Object
191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/fclay/attachment.rb', line 191 def delete_local_files begin (self.class.[:styles] || [nil]).each do |style| FileUtils.rm(local_file_path(style),{:force => true}) end rescue Rails.logger.info "Deleting Media #{id} sync file not found" end true end |
#delete_remote_files ⇒ Object
204 205 206 207 208 209 |
# File 'lib/fclay/attachment.rb', line 204 def delete_remote_files (self.class.[:styles] || [nil]).each do |style| Fclay.remote_storage.bucket_object.object(remote_file_path(style)).delete end end |
#delete_tmp_file ⇒ Object
135 136 137 138 |
# File 'lib/fclay/attachment.rb', line 135 def delete_tmp_file FileUtils.rm(@file.try(:path) || @file[:path],{:force => true}) if @file @file = nil end |
#fclay_attachment_presence ⇒ Object
76 77 78 |
# File 'lib/fclay/attachment.rb', line 76 def errors.add(:file, 'must be present') if id.blank? && !@file end |
#fetch_file_name ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/fclay/attachment.rb', line 177 def fetch_file_name ext = self.class.[:extension] if ext != false || (ext == nil && @file.original_filename) filename_part = @file.original_filename.split(".") ext = filename_part.last if filename_part.size > 1 end self.file_name = try(:fclay_attachment_filename) self.file_name = SecureRandom.hex unless self.file_name self.file_name += ".#{ext}" if ext end |
#file_size_mb ⇒ Object
80 81 82 83 84 |
# File 'lib/fclay/attachment.rb', line 80 def file_size_mb "#{((self.file_size >> 10).to_f / 1024).round(2)} Mb" if self.file_size end |
#file_url(style = nil) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/fclay/attachment.rb', line 86 def file_url(style=nil) case file_location when "external_link" self.file_name when "local" local_file_url(style) when "s3" remote_file_url(style) else "" end end |
#local_file_dir(style = nil) ⇒ Object
121 122 123 124 125 |
# File 'lib/fclay/attachment.rb', line 121 def local_file_dir(style=nil) dir = "#{Rails.root.to_s + Fclay.configuration.local_folder}/#{self.class.name.tableize}" dir += "/#{style.to_s}" if style dir end |
#local_file_path(style = nil) ⇒ Object
103 104 105 106 107 |
# File 'lib/fclay/attachment.rb', line 103 def local_file_path(style=nil) local_file_dir(style) + "/" + file_name end |
#local_file_url(style = nil) ⇒ Object
109 110 111 112 113 114 115 |
# File 'lib/fclay/attachment.rb', line 109 def local_file_url(style=nil) url = Fclay.configuration.local_storage_host url += "#{Fclay.configuration.local_url}/#{self.class.name.tableize}" url += "/#{style.to_s}" if style url += "/#{file_name}" url end |
#need_upload ⇒ Object
41 42 43 |
# File 'lib/fclay/attachment.rb', line 41 def need_upload Fclay.configuration.storage_policy != :local && self.file_location == "local" end |
#process_file ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/fclay/attachment.rb', line 148 def process_file return unless @file delete_files path = @file.try(:path) || @file[:path] return unless path if path[0..3] == "http" self.file_status = 'idle' self.file_location = 'external_link' self.file_name = path else create_dirs fetch_file_name FileUtils.mv(path,local_file_path) `chmod 0755 #{local_file_path}` delete_tmp_file set_file_size self.file_location = 'local' self.file_status = need_upload ? "processing" : "idle" end end |
#remote_file_path(style = nil) ⇒ Object
127 128 129 130 131 132 133 |
# File 'lib/fclay/attachment.rb', line 127 def remote_file_path(style=nil) path = "" path += "#{self.class.name.tableize}" path += "/#{style.to_s}" if style path += "/#{file_name}" path end |
#remote_file_url(style = nil) ⇒ Object
99 100 101 |
# File 'lib/fclay/attachment.rb', line 99 def remote_file_url(style=nil) "http://#{Fclay.remote_storage.bucket_name}.s3.amazonaws.com/#{remote_file_path(style)}" end |
#set_file_size(style = nil) ⇒ Object
211 212 213 |
# File 'lib/fclay/attachment.rb', line 211 def set_file_size style=nil self.file_size = File.size local_file_path(style) end |
#short_local_file_url(style = nil) ⇒ Object
117 118 119 |
# File 'lib/fclay/attachment.rb', line 117 def short_local_file_url(style=nil) end |
#upload ⇒ Object
49 50 51 |
# File 'lib/fclay/attachment.rb', line 49 def upload Fclay::Attachment.upload self.class.name,self.id end |