comparison .svn/pristine/1c/1c28b35075b5e9d9a0869f79fc45dbaf27ac97ad.svn-base @ 1517:dffacf8a6908 redmine-2.5

Update to Redmine SVN revision 13367 on 2.5-stable branch
author Chris Cannam
date Tue, 09 Sep 2014 09:29:00 +0100
parents
children
comparison
equal deleted inserted replaced
1516:b450a9d58aed 1517:dffacf8a6908
1 # Redmine - project management software
2 # Copyright (C) 2006-2014 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 require 'mime/types'
19
20 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 'text/x-po' => 'po',
42 '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 'application/javascript' => 'js',
48 'application/pdf' => 'pdf',
49 }.freeze
50
51 EXTENSIONS = MIME_TYPES.inject({}) do |map, (type, exts)|
52 exts.split(',').each {|ext| map[ext.strip] = type}
53 map
54 end
55
56 # returns mime type for name or nil if unknown
57 def self.of(name)
58 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 end
69
70 # 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
77 def self.main_mimetype_of(name)
78 mimetype = of(name)
79 mimetype.split('/').first if mimetype
80 end
81
82 # 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 end
88 end
89 end