@@ -1,81 +1,81 | |||||
1 | # redMine - project management software |
|
1 | # redMine - project management software | |
2 | # Copyright (C) 2006-2007 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2007 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 | module Redmine |
|
18 | module Redmine | |
19 | module MimeType |
|
19 | module MimeType | |
20 |
|
20 | |||
21 | MIME_TYPES = { |
|
21 | MIME_TYPES = { | |
22 | 'text/plain' => 'txt,tpl,properties,patch,diff,ini,readme,install,upgrade', |
|
22 | 'text/plain' => 'txt,tpl,properties,patch,diff,ini,readme,install,upgrade', | |
23 | 'text/css' => 'css', |
|
23 | 'text/css' => 'css', | |
24 | 'text/html' => 'html,htm,xhtml', |
|
24 | 'text/html' => 'html,htm,xhtml', | |
25 | 'text/jsp' => 'jsp', |
|
25 | 'text/jsp' => 'jsp', | |
26 | 'text/x-c' => 'c,cpp,cc,h,hh', |
|
26 | 'text/x-c' => 'c,cpp,cc,h,hh', | |
27 | 'text/x-csharp' => 'cs', |
|
27 | 'text/x-csharp' => 'cs', | |
28 | 'text/x-java' => 'java', |
|
28 | 'text/x-java' => 'java', | |
29 | 'text/x-javascript' => 'js', |
|
29 | 'text/x-javascript' => 'js', | |
30 | 'text/x-html-template' => 'rhtml', |
|
30 | 'text/x-html-template' => 'rhtml', | |
31 | 'text/x-perl' => 'pl,pm', |
|
31 | 'text/x-perl' => 'pl,pm', | |
32 | 'text/x-php' => 'php,php3,php4,php5', |
|
32 | 'text/x-php' => 'php,php3,php4,php5', | |
33 | 'text/x-python' => 'py', |
|
33 | 'text/x-python' => 'py', | |
34 | 'text/x-ruby' => 'rb,rbw,ruby,rake', |
|
34 | 'text/x-ruby' => 'rb,rbw,ruby,rake,erb', | |
35 | 'text/x-csh' => 'csh', |
|
35 | 'text/x-csh' => 'csh', | |
36 | 'text/x-sh' => 'sh', |
|
36 | 'text/x-sh' => 'sh', | |
37 | 'text/xml' => 'xml,xsd,mxml', |
|
37 | 'text/xml' => 'xml,xsd,mxml', | |
38 | 'text/yaml' => 'yml,yaml', |
|
38 | 'text/yaml' => 'yml,yaml', | |
39 | 'image/gif' => 'gif', |
|
39 | 'image/gif' => 'gif', | |
40 | 'image/jpeg' => 'jpg,jpeg,jpe', |
|
40 | 'image/jpeg' => 'jpg,jpeg,jpe', | |
41 | 'image/png' => 'png', |
|
41 | 'image/png' => 'png', | |
42 | 'image/tiff' => 'tiff,tif', |
|
42 | 'image/tiff' => 'tiff,tif', | |
43 | 'image/x-ms-bmp' => 'bmp', |
|
43 | 'image/x-ms-bmp' => 'bmp', | |
44 | 'image/x-xpixmap' => 'xpm', |
|
44 | 'image/x-xpixmap' => 'xpm', | |
45 | 'application/pdf' => 'pdf', |
|
45 | 'application/pdf' => 'pdf', | |
46 | 'application/zip' => 'zip', |
|
46 | 'application/zip' => 'zip', | |
47 | 'application/x-gzip' => 'gz', |
|
47 | 'application/x-gzip' => 'gz', | |
48 | }.freeze |
|
48 | }.freeze | |
49 |
|
49 | |||
50 | EXTENSIONS = MIME_TYPES.inject({}) do |map, (type, exts)| |
|
50 | EXTENSIONS = MIME_TYPES.inject({}) do |map, (type, exts)| | |
51 | exts.split(',').each {|ext| map[ext.strip] = type} |
|
51 | exts.split(',').each {|ext| map[ext.strip] = type} | |
52 | map |
|
52 | map | |
53 | end |
|
53 | end | |
54 |
|
54 | |||
55 | # returns mime type for name or nil if unknown |
|
55 | # returns mime type for name or nil if unknown | |
56 | def self.of(name) |
|
56 | def self.of(name) | |
57 | return nil unless name |
|
57 | return nil unless name | |
58 | m = name.to_s.match(/(^|\.)([^\.]+)$/) |
|
58 | m = name.to_s.match(/(^|\.)([^\.]+)$/) | |
59 | EXTENSIONS[m[2].downcase] if m |
|
59 | EXTENSIONS[m[2].downcase] if m | |
60 | end |
|
60 | end | |
61 |
|
61 | |||
62 | # Returns the css class associated to |
|
62 | # Returns the css class associated to | |
63 | # the mime type of name |
|
63 | # the mime type of name | |
64 | def self.css_class_of(name) |
|
64 | def self.css_class_of(name) | |
65 | mime = of(name) |
|
65 | mime = of(name) | |
66 | mime && mime.gsub('/', '-') |
|
66 | mime && mime.gsub('/', '-') | |
67 | end |
|
67 | end | |
68 |
|
68 | |||
69 | def self.main_mimetype_of(name) |
|
69 | def self.main_mimetype_of(name) | |
70 | mimetype = of(name) |
|
70 | mimetype = of(name) | |
71 | mimetype.split('/').first if mimetype |
|
71 | mimetype.split('/').first if mimetype | |
72 | end |
|
72 | end | |
73 |
|
73 | |||
74 | # return true if mime-type for name is type/* |
|
74 | # return true if mime-type for name is type/* | |
75 | # otherwise false |
|
75 | # otherwise false | |
76 | def self.is_type?(type, name) |
|
76 | def self.is_type?(type, name) | |
77 | main_mimetype = main_mimetype_of(name) |
|
77 | main_mimetype = main_mimetype_of(name) | |
78 | type.to_s == main_mimetype |
|
78 | type.to_s == main_mimetype | |
79 | end |
|
79 | end | |
80 | end |
|
80 | end | |
81 | end |
|
81 | end |
General Comments 0
You need to be logged in to leave comments.
Login now