Method: FileColumn::TempUploadedFile#store_upload

Defined in:
lib/file_column.rb

#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