Module: Redmine::MimeType

Defined in:
lib/redmine/mime_type.rb

Constant Summary collapse

MIME_TYPES =
{
  'text/plain' => 'txt,tpl,properties,patch,diff,ini,readme,install,upgrade,sql',
  'text/css' => 'css',
  'text/html' => 'html,htm,xhtml',
  'text/jsp' => 'jsp',
  'text/x-c' => 'c,cpp,cc,h,hh',
  'text/x-csharp' => 'cs',
  'text/x-java' => 'java',
  'text/x-html-template' => 'rhtml',
  'text/x-perl' => 'pl,pm',
  'text/x-php' => 'php,php3,php4,php5',
  'text/x-python' => 'py',
  'text/x-ruby' => 'rb,rbw,ruby,rake,erb',
  'text/x-csh' => 'csh',
  'text/x-sh' => 'sh',
  'text/x-textile' => 'textile',
  'text/xml' => 'xml,xsd,mxml',
  'text/yaml' => 'yml,yaml',
  'text/csv' => 'csv',
  'text/x-po' => 'po',
  'image/gif' => 'gif',
  'image/jpeg' => 'jpg,jpeg,jpe',
  'image/png' => 'png',
  'image/tiff' => 'tiff,tif',
  'image/webp' => 'webp',
  'image/x-ms-bmp' => 'bmp',
  'application/javascript' => 'js',
  'application/pdf' => 'pdf',
  'video/mp4' => 'mp4',
  'video/webm' => 'webm',
}.freeze
EXTENSIONS =
MIME_TYPES.inject({}) do |map, (type, exts)|
  exts.split(',').each {|ext| map[ext.strip] = type}
  map
end

Class Method Summary collapse

Class Method Details

.by_type(type) ⇒ Object

returns all full mime types for a given (top level) type



60
61
62
# File 'lib/redmine/mime_type.rb', line 60

def self.by_type(type)
  MIME_TYPES.keys.select{|m| m.start_with? "#{type}/"}
end

.css_class_of(name) ⇒ Object

Returns the css class associated to the mime type of name



75
76
77
78
# File 'lib/redmine/mime_type.rb', line 75

def self.css_class_of(name)
  mimetype = of(name)
  mimetype&.tr('/', '-')
end

.is_type?(type, name) ⇒ Boolean

return true if mime-type for name is type/* otherwise false

Returns:

  • (Boolean)


87
88
89
90
# File 'lib/redmine/mime_type.rb', line 87

def self.is_type?(type, name)
  main_mimetype = main_mimetype_of(name)
  type.to_s == main_mimetype
end

.main_mimetype_of(name) ⇒ Object



80
81
82
83
# File 'lib/redmine/mime_type.rb', line 80

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

.of(name) ⇒ Object

returns mime type for name or nil if unknown



65
66
67
68
69
70
71
# File 'lib/redmine/mime_type.rb', line 65

def self.of(name)
  ext = File.extname(name.to_s)[1..-1]
  if ext
    ext.downcase!
    EXTENSIONS[ext] || MiniMime.lookup_by_extension(ext)&.content_type
  end
end