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