Chris@0: # Redmine - project management software Chris@1295: # Copyright (C) 2006-2013 Jean-Philippe Lang Chris@0: # Chris@0: # This program is free software; you can redistribute it and/or Chris@0: # modify it under the terms of the GNU General Public License Chris@0: # as published by the Free Software Foundation; either version 2 Chris@0: # of the License, or (at your option) any later version. Chris@909: # Chris@0: # This program is distributed in the hope that it will be useful, Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: # GNU General Public License for more details. Chris@909: # Chris@0: # You should have received a copy of the GNU General Public License Chris@0: # along with this program; if not, write to the Free Software Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@0: Chris@0: module Redmine Chris@0: module MimeType Chris@0: Chris@0: MIME_TYPES = { Chris@0: 'text/plain' => 'txt,tpl,properties,patch,diff,ini,readme,install,upgrade', Chris@0: 'text/css' => 'css', Chris@0: 'text/html' => 'html,htm,xhtml', Chris@0: 'text/jsp' => 'jsp', Chris@0: 'text/x-c' => 'c,cpp,cc,h,hh', Chris@0: 'text/x-csharp' => 'cs', Chris@0: 'text/x-java' => 'java', Chris@0: 'text/x-html-template' => 'rhtml', Chris@0: 'text/x-perl' => 'pl,pm', Chris@0: 'text/x-php' => 'php,php3,php4,php5', Chris@0: 'text/x-python' => 'py', Chris@0: 'text/x-ruby' => 'rb,rbw,ruby,rake,erb', Chris@0: 'text/x-csh' => 'csh', Chris@0: 'text/x-sh' => 'sh', Chris@0: 'text/xml' => 'xml,xsd,mxml', Chris@0: 'text/yaml' => 'yml,yaml', Chris@0: 'text/csv' => 'csv', Chris@441: 'text/x-po' => 'po', Chris@0: 'image/gif' => 'gif', Chris@0: 'image/jpeg' => 'jpg,jpeg,jpe', Chris@0: 'image/png' => 'png', Chris@0: 'image/tiff' => 'tiff,tif', Chris@0: 'image/x-ms-bmp' => 'bmp', Chris@0: 'image/x-xpixmap' => 'xpm', Chris@1115: 'image/svg+xml'=> 'svg', Chris@1115: 'application/javascript' => 'js', Chris@0: 'application/pdf' => 'pdf', Chris@0: 'application/rtf' => 'rtf', Chris@0: 'application/msword' => 'doc', Chris@0: 'application/vnd.ms-excel' => 'xls', Chris@0: 'application/vnd.ms-powerpoint' => 'ppt,pps', Chris@0: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'docx', Chris@0: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => 'xlsx', Chris@0: 'application/vnd.openxmlformats-officedocument.presentationml.presentation' => 'pptx', Chris@0: 'application/vnd.openxmlformats-officedocument.presentationml.slideshow' => 'ppsx', Chris@0: 'application/vnd.oasis.opendocument.spreadsheet' => 'ods', Chris@0: 'application/vnd.oasis.opendocument.text' => 'odt', Chris@0: 'application/vnd.oasis.opendocument.presentation' => 'odp', Chris@0: 'application/x-7z-compressed' => '7z', Chris@0: 'application/x-rar-compressed' => 'rar', Chris@0: 'application/x-tar' => 'tar', Chris@0: 'application/zip' => 'zip', Chris@0: 'application/x-gzip' => 'gz', Chris@0: }.freeze Chris@909: Chris@0: EXTENSIONS = MIME_TYPES.inject({}) do |map, (type, exts)| Chris@0: exts.split(',').each {|ext| map[ext.strip] = type} Chris@0: map Chris@0: end Chris@909: Chris@0: # returns mime type for name or nil if unknown Chris@0: def self.of(name) Chris@0: return nil unless name Chris@0: m = name.to_s.match(/(^|\.)([^\.]+)$/) Chris@0: EXTENSIONS[m[2].downcase] if m Chris@0: end Chris@909: Chris@0: # Returns the css class associated to Chris@0: # the mime type of name Chris@0: def self.css_class_of(name) Chris@0: mime = of(name) Chris@0: mime && mime.gsub('/', '-') Chris@0: end Chris@909: Chris@0: def self.main_mimetype_of(name) Chris@0: mimetype = of(name) Chris@0: mimetype.split('/').first if mimetype Chris@0: end Chris@909: Chris@0: # return true if mime-type for name is type/* Chris@0: # otherwise false Chris@0: def self.is_type?(type, name) Chris@0: main_mimetype = main_mimetype_of(name) Chris@0: type.to_s == main_mimetype Chris@909: end Chris@0: end Chris@0: end