module Media::MimeType

Constants

ASSET_CLASS
DESCRIPTION
EXT
EXTENSIONS

This extension mapping is used to force certain mime types. Usually, firefox does pretty good at reporting the correct mime-type, but IE always fails (firefox fails on ogg). So, we use the MIME::Types gem to try to get the correct mime from the extension. Sometimes, however, even this doesn't work. This able will force certain types when MIME::Types fails or is ambiguous

ICON
MIME_TYPES
TYPE_ALIASES

some types can have multiple names

Public Class Methods

asset_class_from_mime_type(mime_type) click to toggle source
# File lib/media/mime_type.rb, line 26
def self.asset_class_from_mime_type(mime_type)
  asset_symbol_from_mime_type(mime_type).to_s.classify
end
asset_symbol_from_mime_type(mime_type) click to toggle source
# File lib/media/mime_type.rb, line 30
def self.asset_symbol_from_mime_type(mime_type)
  lookup(mime_type,ASSET_CLASS) || lookup(mime_group(mime_type),ASSET_CLASS) || lookup('default',ASSET_CLASS)
end
compatible_types?(type1, type2) click to toggle source
# File lib/media/mime_type.rb, line 71
def self.compatible_types?(type1, type2)
  (TYPE_ALIASES[type1] || []).include?(type2)
end
description_from_mime_type(mime_type) click to toggle source

def type_from_file_command(filename)

#  On BSDs, `file` doesn't give a result code of 1 if the file doesn't exist.
type = (filename.match(/\.(\w+)$/)[1] rescue "octet-stream").downcase
mime_type = (Paperclip.run("file", "-b --mime-type :file", :file => filename).split(':').last.strip rescue "application/x-#{type}")
mime_type = "application/x-#{type}" if mime_type.match(/\(.*?\)/)
mime_type

end

# File lib/media/mime_type.rb, line 67
def self.description_from_mime_type(mime_type)
  lookup(mime_type,DESCRIPTION) || lookup(mime_group(mime_type),DESCRIPTION) || lookup('default',DESCRIPTION)
end
extension_from_mime_type(mime_type) click to toggle source
# File lib/media/mime_type.rb, line 34
def self.extension_from_mime_type(mime_type)
  lookup(mime_type,EXT)
end
icon_for(mtype) click to toggle source

def self.group_from_mime_type(mime_type)

lookup(mime_type,GROUP) || lookup(mime_group(mime_type),GROUP)

end

# File lib/media/mime_type.rb, line 22
def self.icon_for(mtype)
  lookup(mtype,ICON) || lookup(mime_group(mtype),ICON) || lookup('default',ICON)
end
lookup(mime_type,field) click to toggle source
# File lib/media/mime_type.rb, line 14
def self.lookup(mime_type,field)
  (MIME_TYPES[simple(mime_type)]||[])[field]
end
mime_group(mime_type) click to toggle source
# File lib/media/mime_type.rb, line 6
def self.mime_group(mime_type)
  mime_type.sub(/\/.*$/,'/') if mime_type     # remove everything after /
end
mime_type_from_extension(ext) click to toggle source
# File lib/media/mime_type.rb, line 38
def self.mime_type_from_extension(ext)
  ext = ext.to_s
  ext = File.extname(ext).gsub('.','') if ext =~ /\./
  mimetype = EXTENSIONS[ext]
  if defined?(MIME::Types)
    unless MIME::Types.type_for('.'+ext).empty?
      mimetype ||= MIME::Types.type_for('.'+ext).first.content_type
    end
  end
  mimetype ||= 'application/octet-stream'
  return mimetype
end
simple(mime_type) click to toggle source
# File lib/media/mime_type.rb, line 10
def self.simple(mime_type)
  mime_type.to_s.sub(/\/x\-/,'/') if mime_type # remove x-
end
type_for(filename) click to toggle source

perhaps use code.google.com/p/mimetype-fu/ for all this?

# File lib/media/mime_type.rb, line 54
def self.type_for(filename)
  self.mime_type_from_extension(filename)
  # todo: add type_from_file_command if ext doesn't pan out.
end