To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / lib / redmine / mime_type.rb @ 1568:bc47b68a9487
History | View | Annotate | Download (2.75 KB)
| 1 | 0:513646585e45 | Chris | # Redmine - project management software
|
|---|---|---|---|
| 2 | 1494:e248c7af89ec | Chris | # Copyright (C) 2006-2014 Jean-Philippe Lang
|
| 3 | 0:513646585e45 | Chris | #
|
| 4 | # This program is free software; you can redistribute it and/or
|
||
| 5 | # modify it under the terms of the GNU General Public License
|
||
| 6 | # as published by the Free Software Foundation; either version 2
|
||
| 7 | # of the License, or (at your option) any later version.
|
||
| 8 | 909:cbb26bc654de | Chris | #
|
| 9 | 0:513646585e45 | Chris | # This program is distributed in the hope that it will be useful,
|
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
| 12 | # GNU General Public License for more details.
|
||
| 13 | 909:cbb26bc654de | Chris | #
|
| 14 | 0:513646585e45 | Chris | # You should have received a copy of the GNU General Public License
|
| 15 | # along with this program; if not, write to the Free Software
|
||
| 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||
| 17 | |||
| 18 | 1517:dffacf8a6908 | Chris | require 'mime/types'
|
| 19 | |||
| 20 | 0:513646585e45 | Chris | module Redmine |
| 21 | module MimeType |
||
| 22 | |||
| 23 | MIME_TYPES = {
|
||
| 24 | 'text/plain' => 'txt,tpl,properties,patch,diff,ini,readme,install,upgrade', |
||
| 25 | 'text/css' => 'css', |
||
| 26 | 'text/html' => 'html,htm,xhtml', |
||
| 27 | 'text/jsp' => 'jsp', |
||
| 28 | 'text/x-c' => 'c,cpp,cc,h,hh', |
||
| 29 | 'text/x-csharp' => 'cs', |
||
| 30 | 'text/x-java' => 'java', |
||
| 31 | 'text/x-html-template' => 'rhtml', |
||
| 32 | 'text/x-perl' => 'pl,pm', |
||
| 33 | 'text/x-php' => 'php,php3,php4,php5', |
||
| 34 | 'text/x-python' => 'py', |
||
| 35 | 'text/x-ruby' => 'rb,rbw,ruby,rake,erb', |
||
| 36 | 'text/x-csh' => 'csh', |
||
| 37 | 'text/x-sh' => 'sh', |
||
| 38 | 'text/xml' => 'xml,xsd,mxml', |
||
| 39 | 'text/yaml' => 'yml,yaml', |
||
| 40 | 'text/csv' => 'csv', |
||
| 41 | 441:cbce1fd3b1b7 | Chris | 'text/x-po' => 'po', |
| 42 | 0:513646585e45 | Chris | 'image/gif' => 'gif', |
| 43 | 'image/jpeg' => 'jpg,jpeg,jpe', |
||
| 44 | 'image/png' => 'png', |
||
| 45 | 'image/tiff' => 'tiff,tif', |
||
| 46 | 'image/x-ms-bmp' => 'bmp', |
||
| 47 | 1115:433d4f72a19b | Chris | 'application/javascript' => 'js', |
| 48 | 0:513646585e45 | Chris | 'application/pdf' => 'pdf', |
| 49 | }.freeze |
||
| 50 | 909:cbb26bc654de | Chris | |
| 51 | 0:513646585e45 | Chris | EXTENSIONS = MIME_TYPES.inject({}) do |map, (type, exts)| |
| 52 | exts.split(',').each {|ext| map[ext.strip] = type}
|
||
| 53 | map |
||
| 54 | end
|
||
| 55 | 909:cbb26bc654de | Chris | |
| 56 | 0:513646585e45 | Chris | # returns mime type for name or nil if unknown
|
| 57 | def self.of(name) |
||
| 58 | 1517:dffacf8a6908 | Chris | return nil unless name.present? |
| 59 | if m = name.to_s.match(/(^|\.)([^\.]+)$/) |
||
| 60 | extension = m[2].downcase
|
||
| 61 | @known_types ||= Hash.new do |h, ext| |
||
| 62 | type = EXTENSIONS[ext]
|
||
| 63 | type ||= MIME::Types.type_for(ext).first.to_s.presence |
||
| 64 | h[ext] = type |
||
| 65 | end
|
||
| 66 | @known_types[extension]
|
||
| 67 | end
|
||
| 68 | 0:513646585e45 | Chris | end
|
| 69 | 909:cbb26bc654de | Chris | |
| 70 | 0:513646585e45 | Chris | # Returns the css class associated to
|
| 71 | # the mime type of name
|
||
| 72 | def self.css_class_of(name) |
||
| 73 | mime = of(name) |
||
| 74 | mime && mime.gsub('/', '-') |
||
| 75 | end
|
||
| 76 | 909:cbb26bc654de | Chris | |
| 77 | 0:513646585e45 | Chris | def self.main_mimetype_of(name) |
| 78 | mimetype = of(name) |
||
| 79 | mimetype.split('/').first if mimetype |
||
| 80 | end
|
||
| 81 | 909:cbb26bc654de | Chris | |
| 82 | 0:513646585e45 | Chris | # return true if mime-type for name is type/*
|
| 83 | # otherwise false
|
||
| 84 | def self.is_type?(type, name) |
||
| 85 | main_mimetype = main_mimetype_of(name) |
||
| 86 | type.to_s == main_mimetype |
||
| 87 | 909:cbb26bc654de | Chris | end
|
| 88 | 0:513646585e45 | Chris | end
|
| 89 | end |