Module: Paperclip::Upfile
- Included in:
- File
- Defined in:
- lib/paperclip/upfile.rb
Overview
The Upfile module is a convenience module for adding uploaded-file-type methods to the File
class. Useful for testing.
user.avatar = File.new("test/test_avatar.jpg")
Instance Method Summary collapse
-
#content_type ⇒ Object
Infer the MIME-type of the file from the extension.
-
#fingerprint ⇒ Object
Returns the hash of the file.
-
#original_filename ⇒ Object
Returns the file’s normal name.
-
#size ⇒ Object
Returns the size of the file.
Instance Method Details
#content_type ⇒ Object
Infer the MIME-type of the file from the extension.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/paperclip/upfile.rb', line 8 def content_type type = (self.path.match(/\.(\w+)$/)[1] rescue "octet-stream").downcase case type when %r"jp(e|g|eg)" then "image/jpeg" when %r"tiff?" then "image/tiff" when %r"png", "gif", "bmp" then "image/#{type}" when "txt" then "text/plain" when %r"html?" then "text/html" when "js" then "application/js" when "csv", "xml", "css" then "text/#{type}" else # On BSDs, `file` doesn't give a result code of 1 if the file doesn't exist. content_type = (Paperclip.run("file", "--mime-type", self.path).split(':').last.strip rescue "application/x-#{type}") content_type = "application/x-#{type}" if content_type.match(/\(.*?\)/) content_type end end |
#fingerprint ⇒ Object
Returns the hash of the file.
37 38 39 |
# File 'lib/paperclip/upfile.rb', line 37 def fingerprint Digest::MD5.hexdigest(self.read) end |
#original_filename ⇒ Object
Returns the file’s normal name.
27 28 29 |
# File 'lib/paperclip/upfile.rb', line 27 def original_filename File.basename(self.path) end |
#size ⇒ Object
Returns the size of the file.
32 33 34 |
# File 'lib/paperclip/upfile.rb', line 32 def size File.size(self) end |