annotate lib/redmine/mime_type.rb @ 8:0c83d98252d9 yuya

* Add custom repo prefix and proper auth realm, remove auth cache (seems like an unwise feature), pass DB handle around, various other bits of tidying
author Chris Cannam
date Thu, 12 Aug 2010 15:31:37 +0100
parents 513646585e45
children cbce1fd3b1b7
rev   line source
Chris@0 1 # Redmine - project management software
Chris@0 2 # Copyright (C) 2006-2009 Jean-Philippe Lang
Chris@0 3 #
Chris@0 4 # This program is free software; you can redistribute it and/or
Chris@0 5 # modify it under the terms of the GNU General Public License
Chris@0 6 # as published by the Free Software Foundation; either version 2
Chris@0 7 # of the License, or (at your option) any later version.
Chris@0 8 #
Chris@0 9 # This program is distributed in the hope that it will be useful,
Chris@0 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 12 # GNU General Public License for more details.
Chris@0 13 #
Chris@0 14 # You should have received a copy of the GNU General Public License
Chris@0 15 # along with this program; if not, write to the Free Software
Chris@0 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 17
Chris@0 18 module Redmine
Chris@0 19 module MimeType
Chris@0 20
Chris@0 21 MIME_TYPES = {
Chris@0 22 'text/plain' => 'txt,tpl,properties,patch,diff,ini,readme,install,upgrade',
Chris@0 23 'text/css' => 'css',
Chris@0 24 'text/html' => 'html,htm,xhtml',
Chris@0 25 'text/jsp' => 'jsp',
Chris@0 26 'text/x-c' => 'c,cpp,cc,h,hh',
Chris@0 27 'text/x-csharp' => 'cs',
Chris@0 28 'text/x-java' => 'java',
Chris@0 29 'text/x-javascript' => 'js',
Chris@0 30 'text/x-html-template' => 'rhtml',
Chris@0 31 'text/x-perl' => 'pl,pm',
Chris@0 32 'text/x-php' => 'php,php3,php4,php5',
Chris@0 33 'text/x-python' => 'py',
Chris@0 34 'text/x-ruby' => 'rb,rbw,ruby,rake,erb',
Chris@0 35 'text/x-csh' => 'csh',
Chris@0 36 'text/x-sh' => 'sh',
Chris@0 37 'text/xml' => 'xml,xsd,mxml',
Chris@0 38 'text/yaml' => 'yml,yaml',
Chris@0 39 'text/csv' => 'csv',
Chris@0 40 'image/gif' => 'gif',
Chris@0 41 'image/jpeg' => 'jpg,jpeg,jpe',
Chris@0 42 'image/png' => 'png',
Chris@0 43 'image/tiff' => 'tiff,tif',
Chris@0 44 'image/x-ms-bmp' => 'bmp',
Chris@0 45 'image/x-xpixmap' => 'xpm',
Chris@0 46 'application/pdf' => 'pdf',
Chris@0 47 'application/rtf' => 'rtf',
Chris@0 48 'application/msword' => 'doc',
Chris@0 49 'application/vnd.ms-excel' => 'xls',
Chris@0 50 'application/vnd.ms-powerpoint' => 'ppt,pps',
Chris@0 51 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'docx',
Chris@0 52 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => 'xlsx',
Chris@0 53 'application/vnd.openxmlformats-officedocument.presentationml.presentation' => 'pptx',
Chris@0 54 'application/vnd.openxmlformats-officedocument.presentationml.slideshow' => 'ppsx',
Chris@0 55 'application/vnd.oasis.opendocument.spreadsheet' => 'ods',
Chris@0 56 'application/vnd.oasis.opendocument.text' => 'odt',
Chris@0 57 'application/vnd.oasis.opendocument.presentation' => 'odp',
Chris@0 58 'application/x-7z-compressed' => '7z',
Chris@0 59 'application/x-rar-compressed' => 'rar',
Chris@0 60 'application/x-tar' => 'tar',
Chris@0 61 'application/zip' => 'zip',
Chris@0 62 'application/x-gzip' => 'gz',
Chris@0 63 }.freeze
Chris@0 64
Chris@0 65 EXTENSIONS = MIME_TYPES.inject({}) do |map, (type, exts)|
Chris@0 66 exts.split(',').each {|ext| map[ext.strip] = type}
Chris@0 67 map
Chris@0 68 end
Chris@0 69
Chris@0 70 # returns mime type for name or nil if unknown
Chris@0 71 def self.of(name)
Chris@0 72 return nil unless name
Chris@0 73 m = name.to_s.match(/(^|\.)([^\.]+)$/)
Chris@0 74 EXTENSIONS[m[2].downcase] if m
Chris@0 75 end
Chris@0 76
Chris@0 77 # Returns the css class associated to
Chris@0 78 # the mime type of name
Chris@0 79 def self.css_class_of(name)
Chris@0 80 mime = of(name)
Chris@0 81 mime && mime.gsub('/', '-')
Chris@0 82 end
Chris@0 83
Chris@0 84 def self.main_mimetype_of(name)
Chris@0 85 mimetype = of(name)
Chris@0 86 mimetype.split('/').first if mimetype
Chris@0 87 end
Chris@0 88
Chris@0 89 # return true if mime-type for name is type/*
Chris@0 90 # otherwise false
Chris@0 91 def self.is_type?(type, name)
Chris@0 92 main_mimetype = main_mimetype_of(name)
Chris@0 93 type.to_s == main_mimetype
Chris@0 94 end
Chris@0 95 end
Chris@0 96 end