Class: FileColumn::TempUploadedFile
Overview
Instance Attribute Summary
#magick_errors
Instance Method Summary
collapse
#absolute_path, #relative_path
#absolute_dir, #after_destroy, #assign, #create_magick_version_if_needed, #has_magick_errors?, #initialize, #just_uploaded?, #on_save, #options, #relative_dir, #store, #transform_with_magick
Instance Method Details
#after_save ⇒ Object
319
320
321
322
323
324
325
326
327
328
329
330
331
|
# File 'lib/file_column.rb', line 319
def after_save
super
file = clone_as PermanentUploadedFile
file.move_from(File.join(tmp_base_dir, @tmp_dir), @just_uploaded)
delete_files
file
end
|
#assign_temp(temp_path) ⇒ Object
303
304
305
306
307
308
309
310
311
312
313
|
# File 'lib/file_column.rb', line 303
def assign_temp(temp_path)
return self if temp_path.nil? or temp_path.empty?
temp = clone_as TempUploadedFile
temp.parse_temp_path(temp_path, :ignore_instance)
temp.delete_files
self
end
|
#correct_extension(filename, ext) ⇒ Object
273
274
275
|
# File 'lib/file_column.rb', line 273
def correct_extension(filename, ext)
strip_extension(filename) << ".#{ext}"
end
|
#delete ⇒ Object
297
298
299
300
301
|
# File 'lib/file_column.rb', line 297
def delete
delete_files
@instance[@attr] = ""
clone_as NoUploadedFile
end
|
#delete_files ⇒ Object
333
334
335
|
# File 'lib/file_column.rb', line 333
def delete_files
FileUtils.rm_rf(File.join(tmp_base_dir, @tmp_dir))
end
|
#get_content_type(fallback = nil) ⇒ Object
337
338
339
340
341
342
343
344
345
346
347
348
349
350
|
# File 'lib/file_column.rb', line 337
def get_content_type(fallback=nil)
if options[:file_exec]
begin
content_type = `#{options[:file_exec]} -bi "#{File.join(@dir,@filename)}"`.chomp
content_type = fallback unless $?.success?
content_type.gsub!(/;.+$/,"") if content_type
content_type
rescue
fallback
end
else
fallback
end
end
|
#parse_temp_path(temp_path, instance_options = nil) ⇒ Object
277
278
279
280
281
282
283
|
# File 'lib/file_column.rb', line 277
def parse_temp_path(temp_path, instance_options=nil)
raise ArgumentError.new("invalid format of '#{temp_path}'") unless temp_path =~ %r{^((\d+\.)+\d+)/([^/].+)$}
@tmp_dir, @filename = $1, FileColumn.sanitize_filename($3)
@dir = File.join(tmp_base_dir, @tmp_dir)
@instance[@attr] = @filename unless instance_options == :ignore_instance
end
|
#store_upload(file) ⇒ Object
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
# File 'lib/file_column.rb', line 226
def store_upload(file)
@tmp_dir = FileColumn.generate_temp_name
@dir = File.join(tmp_base_dir, @tmp_dir)
FileUtils.mkdir_p(@dir)
@filename = FileColumn::sanitize_filename(file.original_filename)
local_file_path = File.join(tmp_base_dir,@tmp_dir,@filename)
if file.respond_to?(:local_path) and file.local_path and File.exists?(file.local_path)
FileUtils.copy_file(file.local_path, local_file_path)
elsif file.respond_to?(:read)
File.open(local_file_path, "wb") { |f| f.write(file.read) }
else
raise ArgumentError.new("Do not know how to handle #{file.inspect}")
end
File.chmod(options[:permissions], local_file_path)
if options[:fix_file_extensions]
content_type = get_content_type((file.content_type.chomp if file.content_type))
if content_type and options[:mime_extensions][content_type]
@filename = correct_extension(@filename,options[:mime_extensions][content_type])
end
new_local_file_path = File.join(tmp_base_dir,@tmp_dir,@filename)
File.rename(local_file_path, new_local_file_path) unless new_local_file_path == local_file_path
local_file_path = new_local_file_path
end
@instance[@attr] = @filename
@just_uploaded = true
end
|
#strip_extension(filename) ⇒ Object
tries to identify and strip the extension of filename if an regular expresion from EXT_REGEXPS matches and the downcased extension is a known extension (in options) we’ll strip this extension
269
270
271
|
# File 'lib/file_column.rb', line 269
def strip_extension(filename)
split_extension(filename).first
end
|
#temp_path ⇒ Object
315
316
317
|
# File 'lib/file_column.rb', line 315
def temp_path
File.join(@tmp_dir, @filename)
end
|
#upload(file) ⇒ Object
285
286
287
288
289
290
291
292
293
294
295
|
# File 'lib/file_column.rb', line 285
def upload(file)
temp = clone_as TempUploadedFile
temp.store_upload(file)
delete_files
temp
end
|