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