Class: Dewey::Mime
- Inherits:
-
Object
- Object
- Dewey::Mime
- Defined in:
- lib/dewey/mime.rb
Class Method Summary collapse
-
.extension(file) ⇒ Object
Determine file extension by parsing the filename.
- .guess_service(mime_type) ⇒ Object
-
.mime_type(file) ⇒ Object
Attempts to provide a mime_type that Google Docs finds acceptable.
-
.mime_type_to_extension_mapping ⇒ Object
Merges then inverts all the mime type constants to make a new hash with mime types as keys and the corresponding file extensions as values.
Class Method Details
.extension(file) ⇒ Object
Determine file extension by parsing the filename. When the filename has no extension, then instead examine the file’s mime type and intuit the extension.
38 39 40 41 42 43 44 45 |
# File 'lib/dewey/mime.rb', line 38 def self.extension(file) extension = File.extname(file.path).sub('.', '') if extension.nil? || extension == '' mime_type = (`file --mime-type #{file.path}`).split(':').last.strip extension = mime_type_to_extension_mapping[mime_type] end extension.to_s.downcase end |
.guess_service(mime_type) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/dewey/mime.rb', line 104 def self.guess_service(mime_type) services = { :document => DOCUMENT_MIMETYPES, :drawing => DRAWING_MIMETYPES, :presentation => PRESENTATION_MIMETYPES, :spreadsheet => SPREADSHEET_MIMETYPES } services.each_key do |service| return service if services[service].values.flatten.include?(mime_type) end nil end |
.mime_type(file) ⇒ Object
Attempts to provide a mime_type that Google Docs finds acceptable. Certain types, when gathered from file require extra coercion and will be handled automatically.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/dewey/mime.rb', line 50 def self.mime_type(file) type = case extension(file) when /csv/ then SPREADSHEET_MIMETYPES[:csv] when /doc$/ then DOCUMENT_MIMETYPES[:doc] when /docx/ then DOCUMENT_MIMETYPES[:docx] when /jpeg/ then DRAWING_MIMETYPES[:jpeg] when /htm/ then DOCUMENT_MIMETYPES[:html] when /ods/ then SPREADSHEET_MIMETYPES[:ods] when /odt/ then DOCUMENT_MIMETYPES[:odt] when /pdf/ then DOCUMENT_MIMETYPES[:pdf] when /png/ then DRAWING_MIMETYPES[:png] when /pps/ then PRESENTATION_MIMETYPES[:pps] when /ppt/ then PRESENTATION_MIMETYPES[:ppt] when /rtf/ then DOCUMENT_MIMETYPES[:rtf] when /svg/ then DRAWING_MIMETYPES[:svg] when /sxw/ then DOCUMENT_MIMETYPES[:sxw] when /tab/ then SPREADSHEET_MIMETYPES[:tab] when /tsv/ then SPREADSHEET_MIMETYPES[:tsv] when /txt/ then DOCUMENT_MIMETYPES[:txt] when /xls$/ then SPREADSHEET_MIMETYPES[:xls] when /xlsx/ then SPREADSHEET_MIMETYPES[:xlsx] end type.is_a?(Array) ? type.first : type end |
.mime_type_to_extension_mapping ⇒ Object
Merges then inverts all the mime type constants to make a new hash with mime types as keys and the corresponding file extensions as values. For example this:
:doc => ['application/msword', 'application/vnd.ms-office'],
Becomes:
'application/msword' => :doc,
'application/vnd.ms-office' => :doc
This is useful for intuiting file extensions based on mime types, which is handy when the filename doesn’t have an extension (e.g. when doing post-processing with paperclip).
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/dewey/mime.rb', line 88 def self.mime_type_to_extension_mapping doc_types = [DOCUMENT_MIMETYPES, DRAWING_MIMETYPES, PRESENTATION_MIMETYPES, SPREADSHEET_MIMETYPES] extensions = {} mime_types = {} doc_types.each { |doc_type| extensions.merge!(doc_type) } extensions.each do |extension, mime| if mime.is_a?(Array) mime.each { |m| mime_types[m] = extension } else mime_types[mime] = extension end end mime_types end |