Class: FileColumn::TempUploadedFile

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

Overview

:nodoc:

Instance Method Summary collapse

Methods inherited from RealUploadedFile

#absolute_path, #relative_path

Methods inherited from BaseUploadedFile

#absolute_dir, #after_destroy, #assign, #initialize, #just_uploaded?, #on_save, #options, #relative_dir, #store

Constructor Details

This class inherits a constructor from FileColumn::BaseUploadedFile

Instance Method Details

#after_saveObject



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

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



302
303
304
305
306
307
308
309
310
311
312
# File 'lib/file_column.rb', line 302

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



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

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

#deleteObject



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

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

#delete_filesObject



332
333
334
# File 'lib/file_column.rb', line 332

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

#get_content_type(fallback = nil) ⇒ Object



336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/file_column.rb', line 336

def get_content_type(fallback=nil)
  if options[:file_exec]
    begin
      content_type = `#{options[:file_exec]} -b --mime "#{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)


276
277
278
279
280
281
282
# File 'lib/file_column.rb', line 276

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



225
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
# File 'lib/file_column.rb', line 225

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



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

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

#temp_pathObject



314
315
316
# File 'lib/file_column.rb', line 314

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

#upload(file) ⇒ Object



284
285
286
287
288
289
290
291
292
293
294
# File 'lib/file_column.rb', line 284

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