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, #store, #transform_with_magick

Constructor Details

This class inherits a constructor from FileColumn::BaseUploadedFile

Instance Method Details

#after_saveObject



319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/file_column.rb', line 319

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



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?
  # 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



273
274
275
# File 'lib/file_column.rb', line 273

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

#deleteObject



297
298
299
300
301
# File 'lib/file_column.rb', line 297

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

#delete_filesObject



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

Raises:

  • (ArgumentError)


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)

  # 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



269
270
271
# File 'lib/file_column.rb', line 269

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

#temp_pathObject



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)
  # store new file
  temp = clone_as TempUploadedFile
  temp.store_upload(file)

  # delete old copy
  delete_files

  # and return new TempUploadedFile object
  temp
end