Class: FileColumn::TempUploadedFile

Inherits:
RealUploadedFile show all
Defined in:
lib/file_column.rb

Overview

:nodoc:

Instance Attribute Summary

Attributes inherited from BaseUploadedFile

#magick_errors

Instance Method Summary collapse

Methods inherited from RealUploadedFile

#absolute_path, #relative_path

Methods inherited from BaseUploadedFile

#absolute_dir, #after_destroy, #assign, #create_magick_version_if_needed, #has_magick_errors?, #initialize, #just_uploaded?, #on_save, #options, #relative_dir, #transform_with_magick

Constructor Details

This class inherits a constructor from FileColumn::BaseUploadedFile

Instance Method Details

#after_saveObject



305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/file_column.rb', line 305

def after_save
  super

  # we have a newly uploaded image, move it to the correct location
  file = clone_as PermanentUploadedFile
  file.move_from(File.join(tmp_base_dir, @tmp_dir), @just_uploaded)

  # delete temporary files
  delete_files

  # replace with the new PermanentUploadedFile object
  file
end

#assign_temp(temp_path) ⇒ Object



289
290
291
292
293
294
295
296
297
298
299
# File 'lib/file_column.rb', line 289

def assign_temp(temp_path)
  return self if temp_path.nil? or temp_path.empty?
  # we can ignore this since we've already received a newly uploaded file

  # however, we delete the old temporary files
  temp = clone_as TempUploadedFile
  temp.parse_temp_path(temp_path, :ignore_instance)
  temp.delete_files

  self
end

#correct_extension(filename, ext) ⇒ Object



259
260
261
# File 'lib/file_column.rb', line 259

def correct_extension(filename, ext)
  strip_extension(filename) << ".#{ext}"
end

#deleteObject



283
284
285
286
287
# File 'lib/file_column.rb', line 283

def delete
  delete_files
  @instance[@attr] = ""
  clone_as NoUploadedFile
end

#delete_filesObject



319
320
321
# File 'lib/file_column.rb', line 319

def delete_files
  FileUtils.rm_rf(File.join(tmp_base_dir, @tmp_dir))
end

#get_content_type(fallback = nil) ⇒ Object



323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/file_column.rb', line 323

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

Raises:

  • (ArgumentError)


263
264
265
266
267
268
269
# File 'lib/file_column.rb', line 263

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



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/file_column.rb', line 213

def store_upload(file)
  @tmp_dir = FileColumn.generate_temp_name
  @dir = File.join(tmp_base_dir, @tmp_dir)      
  FileUtils.mkdir(@dir)
  
  @filename = FileColumn::sanitize_filename(file.original_filename)
  local_file_path = File.join(tmp_base_dir,@tmp_dir,@filename)
  
  # stored uploaded file into local_file_path
  # If it was a Tempfile object, the temporary file will be
  # cleaned up automatically, so we do not have to care for this
  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]
    # try to determine correct file extension and fix
    # if necessary
    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



255
256
257
# File 'lib/file_column.rb', line 255

def strip_extension(filename)
  split_extension(filename).first
end

#temp_pathObject



301
302
303
# File 'lib/file_column.rb', line 301

def temp_path
  File.join(@tmp_dir, @filename)
end

#upload(file) ⇒ Object



271
272
273
274
275
276
277
278
279
280
281
# File 'lib/file_column.rb', line 271

def upload(file)
  # store new file
  temp = clone_as TempUploadedFile
  temp.store_upload(file)
  
  # delete old copy
  delete_files

  # and return new TempUploadedFile object
  temp
end