OSDN Git Service

c85ebdcb9e0e160cd400a5ced126f19024ffa78e
[redminele/redminele.git] / redmine / lib / redmine / mime_type.rb
1 # Redmine - project management software
2 # Copyright (C) 2006-2009  Jean-Philippe Lang
3 #
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
9 # 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
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
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17
18 module Redmine
19   module MimeType
20
21     MIME_TYPES = {
22       'text/plain' => 'txt,tpl,properties,patch,diff,ini,readme,install,upgrade',
23       'text/css' => 'css',
24       'text/html' => 'html,htm,xhtml',
25       'text/jsp' => 'jsp',
26       'text/x-c' => 'c,cpp,cc,h,hh',
27       'text/x-csharp' => 'cs',
28       'text/x-java' => 'java',
29       'text/x-javascript' => 'js',
30       'text/x-html-template' => 'rhtml',
31       'text/x-perl' => 'pl,pm',
32       'text/x-php' => 'php,php3,php4,php5',
33       'text/x-python' => 'py',
34       'text/x-ruby' => 'rb,rbw,ruby,rake,erb',
35       'text/x-csh' => 'csh',
36       'text/x-sh' => 'sh',
37       'text/xml' => 'xml,xsd,mxml',
38       'text/yaml' => 'yml,yaml',
39       'text/csv' => 'csv',
40       'image/gif' => 'gif',
41       'image/jpeg' => 'jpg,jpeg,jpe',
42       'image/png' => 'png',
43       'image/tiff' => 'tiff,tif',
44       'image/x-ms-bmp' => 'bmp',
45       'image/x-xpixmap' => 'xpm',
46       'application/pdf' => 'pdf',
47       'application/rtf' => 'rtf',
48       'application/msword' => 'doc',
49       'application/vnd.ms-excel' => 'xls',
50       'application/vnd.ms-powerpoint' => 'ppt,pps',
51       'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'docx',
52       'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => 'xlsx',
53       'application/vnd.openxmlformats-officedocument.presentationml.presentation' => 'pptx',
54       'application/vnd.openxmlformats-officedocument.presentationml.slideshow' => 'ppsx',
55       'application/vnd.oasis.opendocument.spreadsheet' => 'ods',
56       'application/vnd.oasis.opendocument.text' => 'odt',
57       'application/vnd.oasis.opendocument.presentation' => 'odp',
58       'application/x-7z-compressed' => '7z',
59       'application/x-rar-compressed' => 'rar',
60       'application/x-tar' => 'tar',
61       'application/zip' => 'zip',
62       'application/x-gzip' => 'gz',
63     }.freeze
64     
65     EXTENSIONS = MIME_TYPES.inject({}) do |map, (type, exts)|
66       exts.split(',').each {|ext| map[ext.strip] = type}
67       map
68     end
69     
70     # returns mime type for name or nil if unknown
71     def self.of(name)
72       return nil unless name
73       m = name.to_s.match(/(^|\.)([^\.]+)$/)
74       EXTENSIONS[m[2].downcase] if m
75     end
76     
77     # Returns the css class associated to
78     # the mime type of name
79     def self.css_class_of(name)
80       mime = of(name)
81       mime && mime.gsub('/', '-')
82     end
83     
84     def self.main_mimetype_of(name)
85       mimetype = of(name)
86       mimetype.split('/').first if mimetype
87     end
88     
89     # return true if mime-type for name is type/*
90     # otherwise false
91     def self.is_type?(type, name)
92       main_mimetype = main_mimetype_of(name)
93       type.to_s == main_mimetype
94     end  
95   end
96 end