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