Class: UploadColumn::SanitizedFile

Inherits:
Object
  • Object
show all
Defined in:
lib/upload_column/sanitized_file.rb

Overview

Sanitize is a base class that takes care of all the dirtywork when dealing with file uploads. it is subclassed as UploadedFile in UploadColumn, which does most of the upload magic, but if you want to roll you own uploading system, SanitizedFile might be for you since it takes care of a lot of the unfun stuff.

Usage is pretty simple, just do SanitizedFile.new(some_uploaded_file) and you’re good to go you can now use #copy_to and #move_to to place the file wherever you want, whether it is a StringIO or a TempFile.

SanitizedFile also deals with content type detection, which it does either through the ‘file’ *nix exec or (if you are stuck on Windows) through the MIME::Types library (not to be confused with Rails’ Mime class!).

Direct Known Subclasses

UploadedFile

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, options = {}) ⇒ SanitizedFile

Returns a new instance of SanitizedFile.



21
22
23
24
25
26
27
28
29
30
# File 'lib/upload_column/sanitized_file.rb', line 21

def initialize(file, options = {})
  @options = options
  if file && file.instance_of?(String) && !file.empty? 
    @path = file
    self.filename = File.basename(file)
  else
    @file = file
    self.filename = self.original_filename unless self.empty?
  end
end

Instance Attribute Details

#basenameObject (readonly)

Returns the value of attribute basename.



19
20
21
# File 'lib/upload_column/sanitized_file.rb', line 19

def basename
  @basename
end

#extensionObject (readonly)

Returns the value of attribute extension.



19
20
21
# File 'lib/upload_column/sanitized_file.rb', line 19

def extension
  @extension
end

Instance Method Details

#content_typeObject

Returns the content_type of the file as determined through the MIME::Types library or through a *nix exec.



87
88
89
90
91
92
# File 'lib/upload_column/sanitized_file.rb', line 87

def content_type
  unless content_type = get_content_type_from_exec || get_content_type_from_mime_types
    content_type ||= @file.content_type.chomp if @file.respond_to?(:content_type) and @file.content_type
  end
  return content_type
end

#copy_to(path) ⇒ Object

Copies the file to ‘path’ and returns a new SanitizedFile that points to the copy.



80
81
82
83
84
# File 'lib/upload_column/sanitized_file.rb', line 80

def copy_to(path)
  copy = self.clone
  copy.move_to(path)
  return copy
end

#empty?Boolean

Checks if the file is empty.

Returns:

  • (Boolean)


58
59
60
# File 'lib/upload_column/sanitized_file.rb', line 58

def empty?
  (@file.nil? && @path.nil?) || self.size.nil? || self.size.zero?
end

#exists?Boolean

Checks if the file exists

Returns:

  • (Boolean)


63
64
65
# File 'lib/upload_column/sanitized_file.rb', line 63

def exists?
  File.exists?(self.path) if self.path
end

#filenameObject

Returns the files properly sanitized filename.



42
43
44
# File 'lib/upload_column/sanitized_file.rb', line 42

def filename
  @filename ||= (self.extension && !self.extension.empty?) ? "#{self.basename}.#{self.extension}" : self.basename
end

#move_to(path) ⇒ Object

Moves the file to ‘path’



68
69
70
71
72
73
74
75
76
77
# File 'lib/upload_column/sanitized_file.rb', line 68

def move_to(path)
  if copy_file(path)
    # FIXME: This gets pretty broken in UploadedFile. E.g. moving avatar-thumb.jpg will change the filename
    # to avatar-thumb-thumb.jpg
    @basename, @extension = split_extension(File.basename(path))
    @file = nil
    @filename = nil
    @path = path
  end
end

#original_filenameObject

Returns the filename before sanitation took place



33
34
35
36
37
38
39
# File 'lib/upload_column/sanitized_file.rb', line 33

def original_filename
  @original_filename ||= if @file and @file.respond_to?(:original_filename)
    @file.original_filename
  elsif self.path
    File.basename(self.path)
  end
end

#pathObject

Returns the full path to the file



53
54
55
# File 'lib/upload_column/sanitized_file.rb', line 53

def path
  @path ||= File.expand_path(@file.path) rescue nil
end

#sizeObject

Returns the file’s size



47
48
49
50
# File 'lib/upload_column/sanitized_file.rb', line 47

def size
  return @file.size if @file.respond_to?(:size)
  File.size(self.path) rescue nil
end