Class: Convertible::MimeTypes

Inherits:
Object
  • Object
show all
Defined in:
lib/convertible/mime_types.rb

Constant Summary collapse

MIME_TYPES =
{
  'text/plain' => %w(txt),
  'text/html' => %w(html htm xhtml),
  'text/xml' => %w(xml xsd mxml),
  'text/yaml' => %w(yml yaml),
  'text/csv' => %w(csv),
  'text/rtf' => %w(rtf),
  'image/gif' => %w(gif),
  'image/jpeg' => %w(jpg jpeg jpe),
  'image/png' => %w(png),
  'image/tiff' => %w(tiff tif),
  'image/x-ms-bmp' => %w(bmp),
  'image/x-xpixmap' => %w(xpm),
  'application/pdf' => %w(pdf),
  'application/msword' => %w(doc),
  'application/vnd.ms-excel' => %w(xls),
  'application/vnd.ms-powerpoint' => %w(ppt pps),
  'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => %w(docx),
  'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => %w(xlsx),
  'application/vnd.openxmlformats-officedocument.presentationml.presentation' => %w(pptx),
  'application/vnd.openxmlformats-officedocument.presentationml.slideshow' => %w(ppsx),
  'application/vnd.oasis.opendocument.spreadsheet' => %w(ods),
  'application/vnd.oasis.opendocument.text' => %w(odt),
  'application/vnd.oasis.opendocument.presentation' => %w(odp),
  'application/zip' => %w(zip),
  'application/x-gzip' => %w(gz)
}.freeze
EXTENSIONS =
MIME_TYPES.inject({}) do |map, (type, exts)|
  exts.each {|ext| map[ext] = type}
  map
end
ALIASES =
{
  'application/pdf' => ['application/x-pdf']
}
ALIAS_LOOKUP =
ALIASES.inject({}) do |map, (type, aliases)|
  aliases.each {|t| map[t] = type}
  map
end

Class Method Summary collapse

Class Method Details

.extension_for(mimetype) ⇒ Object



63
64
65
66
67
# File 'lib/convertible/mime_types.rb', line 63

def self.extension_for(mimetype)
  if exts = MIME_TYPES[self.for(mimetype)]
    exts.first
  end
end

.for(mimetype) ⇒ Object

input is filename or mime type string (which will be returned as is)



53
54
55
56
57
58
59
60
61
# File 'lib/convertible/mime_types.rb', line 53

def self.for(mimetype)
  if MIME_TYPES[mimetype]
    mimetype
  elsif t = ALIAS_LOOKUP[mimetype]
    t
  else
    of(mimetype)
  end
end

.image?(mimetype) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/convertible/mime_types.rb', line 74

def self.image?(mimetype)
  self.for(mimetype) =~ /^image\/.+/
end

.main_mimetype_of(name) ⇒ Object



69
70
71
72
# File 'lib/convertible/mime_types.rb', line 69

def self.main_mimetype_of(name)
  mimetype = of(name)
  mimetype.split('/').first if mimetype
end

.of(name) ⇒ Object

input is filename



46
47
48
49
50
# File 'lib/convertible/mime_types.rb', line 46

def self.of(name)
  if name && m = name.to_s.match(/(^|\.)([^\.]+)$/)
    EXTENSIONS[m[2].downcase]
  end
end

.supported?(name_or_mimetype) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/convertible/mime_types.rb', line 78

def self.supported?(name_or_mimetype)
  !self.for(name_or_mimetype).blank?
end