Chris@1517: # Redmine - project management software Chris@1517: # Copyright (C) 2006-2014 Jean-Philippe Lang Chris@1517: # Chris@1517: # This program is free software; you can redistribute it and/or Chris@1517: # modify it under the terms of the GNU General Public License Chris@1517: # as published by the Free Software Foundation; either version 2 Chris@1517: # of the License, or (at your option) any later version. Chris@1517: # Chris@1517: # This program is distributed in the hope that it will be useful, Chris@1517: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1517: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1517: # GNU General Public License for more details. Chris@1517: # Chris@1517: # You should have received a copy of the GNU General Public License Chris@1517: # along with this program; if not, write to the Free Software Chris@1517: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1517: Chris@1517: require 'mime/types' Chris@1517: Chris@1517: module Redmine Chris@1517: module MimeType Chris@1517: Chris@1517: MIME_TYPES = { Chris@1517: 'text/plain' => 'txt,tpl,properties,patch,diff,ini,readme,install,upgrade', Chris@1517: 'text/css' => 'css', Chris@1517: 'text/html' => 'html,htm,xhtml', Chris@1517: 'text/jsp' => 'jsp', Chris@1517: 'text/x-c' => 'c,cpp,cc,h,hh', Chris@1517: 'text/x-csharp' => 'cs', Chris@1517: 'text/x-java' => 'java', Chris@1517: 'text/x-html-template' => 'rhtml', Chris@1517: 'text/x-perl' => 'pl,pm', Chris@1517: 'text/x-php' => 'php,php3,php4,php5', Chris@1517: 'text/x-python' => 'py', Chris@1517: 'text/x-ruby' => 'rb,rbw,ruby,rake,erb', Chris@1517: 'text/x-csh' => 'csh', Chris@1517: 'text/x-sh' => 'sh', Chris@1517: 'text/xml' => 'xml,xsd,mxml', Chris@1517: 'text/yaml' => 'yml,yaml', Chris@1517: 'text/csv' => 'csv', Chris@1517: 'text/x-po' => 'po', Chris@1517: 'image/gif' => 'gif', Chris@1517: 'image/jpeg' => 'jpg,jpeg,jpe', Chris@1517: 'image/png' => 'png', Chris@1517: 'image/tiff' => 'tiff,tif', Chris@1517: 'image/x-ms-bmp' => 'bmp', Chris@1517: 'application/javascript' => 'js', Chris@1517: 'application/pdf' => 'pdf', Chris@1517: }.freeze Chris@1517: Chris@1517: EXTENSIONS = MIME_TYPES.inject({}) do |map, (type, exts)| Chris@1517: exts.split(',').each {|ext| map[ext.strip] = type} Chris@1517: map Chris@1517: end Chris@1517: Chris@1517: # returns mime type for name or nil if unknown Chris@1517: def self.of(name) Chris@1517: return nil unless name.present? Chris@1517: if m = name.to_s.match(/(^|\.)([^\.]+)$/) Chris@1517: extension = m[2].downcase Chris@1517: @known_types ||= Hash.new do |h, ext| Chris@1517: type = EXTENSIONS[ext] Chris@1517: type ||= MIME::Types.type_for(ext).first.to_s.presence Chris@1517: h[ext] = type Chris@1517: end Chris@1517: @known_types[extension] Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: # Returns the css class associated to Chris@1517: # the mime type of name Chris@1517: def self.css_class_of(name) Chris@1517: mime = of(name) Chris@1517: mime && mime.gsub('/', '-') Chris@1517: end Chris@1517: Chris@1517: def self.main_mimetype_of(name) Chris@1517: mimetype = of(name) Chris@1517: mimetype.split('/').first if mimetype Chris@1517: end Chris@1517: Chris@1517: # return true if mime-type for name is type/* Chris@1517: # otherwise false Chris@1517: def self.is_type?(type, name) Chris@1517: main_mimetype = main_mimetype_of(name) Chris@1517: type.to_s == main_mimetype Chris@1517: end Chris@1517: end Chris@1517: end