Module: QuickFile::Upload::InstanceMethods
- Defined in:
- lib/quick_file/upload.rb
Instance Method Summary collapse
- #add_file!(style_name, path) ⇒ Object
- #cache! ⇒ Object
- #cached? ⇒ Boolean
- #content_type(style_name = nil) ⇒ Object
- #delete ⇒ Object
- #error? ⇒ Boolean
- #extension ⇒ Object
- #get_style(style_name) ⇒ Object
- #is_image?(style_name = nil) ⇒ Boolean
- #is_video?(style_name = nil) ⇒ Boolean
- #loaded? ⇒ Boolean
- #owner=(obj) ⇒ Object
- #path(style_name = nil) ⇒ Object
- #process! ⇒ Object
- #processed? ⇒ Boolean
- #reprocess! ⇒ Object
- #sanitized_basename ⇒ Object
- #sanitized_filename ⇒ Object
- #save_cache_file(cn, file) ⇒ Object
- #size(style_name = nil) ⇒ Object
- #storage_protocol ⇒ Object
- #store! ⇒ Object
- #store_style!(style_name) ⇒ Object
- #stored? ⇒ Boolean
- #style_exists?(style_name) ⇒ Boolean
- #uploaded_file=(uf) ⇒ Object
- #url(style_name = nil, opts = {:secure=>true}) ⇒ Object
- #url_hash ⇒ Object
Instance Method Details
#add_file!(style_name, path) ⇒ Object
155 156 157 158 159 160 |
# File 'lib/quick_file/upload.rb', line 155 def add_file!(style_name, path) styles[style_name.to_s] = {"path" => path} get_style(style_name.to_s) self.state = STATES[:stored] save end |
#cache! ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/quick_file/upload.rb', line 89 def cache! return unless loaded? cp = save_cache_file(QuickFile.generate_cache_name(extension), @file) styles["original"] = {"cache" => cp, "ct" => QuickFile.content_type_for(cp), "sz" => File.size(cp)} self.validate! if errors.size > 0 self.state = STATES[:error] File.delete(cp) else self.state = STATES[:cached] end self.save end |
#cached? ⇒ Boolean
61 62 63 |
# File 'lib/quick_file/upload.rb', line 61 def cached? self.state == STATES[:cached] end |
#content_type(style_name = nil) ⇒ Object
167 168 169 170 |
# File 'lib/quick_file/upload.rb', line 167 def content_type(style_name=nil) style_name ||= :original styles[style_name.to_s]["ct"] end |
#delete ⇒ Object
264 265 266 267 268 269 270 271 |
# File 'lib/quick_file/upload.rb', line 264 def delete # delete uploaded files styles.each do |k,v| QuickFile.fog_directory.files.new(:key => v["path"]).destroy if v["path"] File.delete(v["cache"]) if (v["cache"] && File.exists?(v["cache"])) end self.state = STATES[:deleted] end |
#error? ⇒ Boolean
73 74 75 |
# File 'lib/quick_file/upload.rb', line 73 def error? self.state == STATES[:error] end |
#extension ⇒ Object
85 86 87 |
# File 'lib/quick_file/upload.rb', line 85 def extension File.extname(original_filename) end |
#get_style(style_name) ⇒ Object
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/quick_file/upload.rb', line 241 def get_style(style_name) fn = path(style_name) if storage_protocol == :fog f = QuickFile.fog_directory.files.get(fn) if f.nil? styles[style_name]["ct"] = QuickFile.content_type_for(fn) styles[style_name]["sz"] = 0 else styles[style_name]["ct"] = f.content_type.nil? ? QuickFile.content_type_for(fn) : f.content_type styles[style_name]["sz"] = f.content_length end save end f end |
#is_image?(style_name = nil) ⇒ Boolean
177 178 179 180 181 |
# File 'lib/quick_file/upload.rb', line 177 def is_image?(style_name=nil) style_name ||= :original return false if content_type(style_name).nil? content_type(style_name).include? "image" end |
#is_video?(style_name = nil) ⇒ Boolean
183 184 185 186 187 |
# File 'lib/quick_file/upload.rb', line 183 def is_video?(style_name=nil) style_name ||= :original fp = styles[style_name]["path"] || styles[style_name]["cache"] QuickFile.is_video_file? fp end |
#loaded? ⇒ Boolean
57 58 59 |
# File 'lib/quick_file/upload.rb', line 57 def loaded? self.state == STATES[:loaded] end |
#owner=(obj) ⇒ Object
44 45 46 47 |
# File 'lib/quick_file/upload.rb', line 44 def owner=(obj) self.oty = obj.class.to_s self.oid = obj.id end |
#path(style_name = nil) ⇒ Object
162 163 164 165 |
# File 'lib/quick_file/upload.rb', line 162 def path(style_name=nil) style_name ||= :original styles[style_name.to_s]["path"] end |
#process! ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/quick_file/upload.rb', line 112 def process! return unless cached? self.state = STATES[:processing] self.save begin puts "#{processes.size} processes" processes.each do |style_name, opts| puts "Processing #{style_name}..." fn = opts[:blk].call(styles["original"]["cache"]) unless fn.nil? if (styles.key?(style_name) && !styles[style_name]["cache"].nil?) File.delete(styles[style_name]["cache"]) end styles[style_name.to_s] = {"cache" => fn, "ct" => QuickFile.content_type_for(fn), "sz" => File.size(fn)} end end self.state = STATES[:processed] rescue Exception => e puts e. self.errors << "PROCESS: #{e.}" self.state = STATES[:error] end self.save store! end |
#processed? ⇒ Boolean
65 66 67 |
# File 'lib/quick_file/upload.rb', line 65 def processed? self.state == STATES[:processed] end |
#reprocess! ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/quick_file/upload.rb', line 142 def reprocess! return unless (stored? || error?) # download original file cp = QuickFile.new_cache_file File.extname(self.path) QuickFile.download(url, cp) styles["original"] = {"cache" => cp, "ct" => QuickFile.content_type_for(cp), "sz" => File.size(cp)} self.state = STATES[:cached] self.save self.process! end |
#sanitized_basename ⇒ Object
77 78 79 |
# File 'lib/quick_file/upload.rb', line 77 def sanitized_basename File.basename original_filename.gsub( /[^a-zA-Z0-9_\-\.]/, '_'), File.extname(original_filename) end |
#sanitized_filename ⇒ Object
81 82 83 |
# File 'lib/quick_file/upload.rb', line 81 def sanitized_filename File.basename original_filename.gsub( /[^a-zA-Z0-9_\-\.]/, '_') end |
#save_cache_file(cn, file) ⇒ Object
105 106 107 108 109 110 |
# File 'lib/quick_file/upload.rb', line 105 def save_cache_file(cn, file) Dir.mkdir QuickFile::CACHE_DIR unless File.directory?(QuickFile::CACHE_DIR) cp = QuickFile.cache_path(cn) File.open(cp, "wb") { |f| f.write(file.read) } cp end |
#size(style_name = nil) ⇒ Object
172 173 174 175 |
# File 'lib/quick_file/upload.rb', line 172 def size(style_name=nil) style_name ||= :original styles[style_name.to_s]["sz"] end |
#storage_protocol ⇒ Object
212 213 214 215 216 217 218 219 |
# File 'lib/quick_file/upload.rb', line 212 def storage_protocol case storage_type.to_sym when :s3 return :fog when :fog return :fog end end |
#store! ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/quick_file/upload.rb', line 193 def store! return unless processed? begin styles.keys.each do |style_name| store_style! style_name unless styles[style_name]["cache"].nil? end self.state = STATES[:stored] rescue StandardError => e puts e. self.errors << "STORE: #{e.}" if self.errors.count < 5 self.store! else self.state = STATES[:error] end end save end |
#store_style!(style_name) ⇒ Object
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/quick_file/upload.rb', line 221 def store_style!(style_name) fn = styles[style_name]["cache"] sp = storage_path(style_name, File.extname(fn)) if storage_protocol == :fog QuickFile.fog_directory.files.create({ :body => File.open(fn).read, :content_type => QuickFile.content_type_for(fn), :key => sp, :public => QuickFile.[:fog_public] }) end styles[style_name]["path"] = sp styles[style_name]["ct"] = QuickFile.content_type_for(fn) styles[style_name]["sz"] = File.size(fn) styles[style_name].delete("cache") File.delete(fn) save end |
#stored? ⇒ Boolean
69 70 71 |
# File 'lib/quick_file/upload.rb', line 69 def stored? self.state == STATES[:stored] end |
#style_exists?(style_name) ⇒ Boolean
189 190 191 |
# File 'lib/quick_file/upload.rb', line 189 def style_exists?(style_name) !styles[style_name.to_s].nil? end |
#uploaded_file=(uf) ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/quick_file/upload.rb', line 49 def uploaded_file=(uf) errors = [] @file = uf self.original_filename = uf.original_filename self.state = STATES[:loaded] cache! end |
#url(style_name = nil, opts = {:secure=>true}) ⇒ Object
257 258 259 260 261 262 |
# File 'lib/quick_file/upload.rb', line 257 def url(style_name=nil, opts={:secure=>true}) proto = opts[:secure] ? "https://" : "http://" style_name ||= "original" return default_url(style_name) unless (styles[style_name] && styles[style_name]["path"]) "#{proto}#{QuickFile.host_url[storage_type.to_sym]}#{styles[style_name]["path"]}" end |
#url_hash ⇒ Object
273 274 275 276 277 278 279 |
# File 'lib/quick_file/upload.rb', line 273 def url_hash ret = {} processes.keys.each do |style_name| ret[style_name] = self.url(style_name.to_sym) end ret end |