To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / lib / redmine / mime_type.rb @ 912:5e80956cc792

History | View | Annotate | Download (3.42 KB)

1 0:513646585e45 Chris
# Redmine - project management software
2 909:cbb26bc654de Chris
# Copyright (C) 2006-2011  Jean-Philippe Lang
3 0:513646585e45 Chris
#
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 909:cbb26bc654de Chris
#
9 0:513646585e45 Chris
# 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 909:cbb26bc654de Chris
#
14 0:513646585e45 Chris
# 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 441:cbce1fd3b1b7 Chris
      'text/x-po' => 'po',
41 0:513646585e45 Chris
      'image/gif' => 'gif',
42
      'image/jpeg' => 'jpg,jpeg,jpe',
43
      'image/png' => 'png',
44
      'image/tiff' => 'tiff,tif',
45
      'image/x-ms-bmp' => 'bmp',
46
      'image/x-xpixmap' => 'xpm',
47
      'application/pdf' => 'pdf',
48
      'application/rtf' => 'rtf',
49
      'application/msword' => 'doc',
50
      'application/vnd.ms-excel' => 'xls',
51
      'application/vnd.ms-powerpoint' => 'ppt,pps',
52
      'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'docx',
53
      'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => 'xlsx',
54
      'application/vnd.openxmlformats-officedocument.presentationml.presentation' => 'pptx',
55
      'application/vnd.openxmlformats-officedocument.presentationml.slideshow' => 'ppsx',
56
      'application/vnd.oasis.opendocument.spreadsheet' => 'ods',
57
      'application/vnd.oasis.opendocument.text' => 'odt',
58
      'application/vnd.oasis.opendocument.presentation' => 'odp',
59
      'application/x-7z-compressed' => '7z',
60
      'application/x-rar-compressed' => 'rar',
61
      'application/x-tar' => 'tar',
62
      'application/zip' => 'zip',
63
      'application/x-gzip' => 'gz',
64
    }.freeze
65 909:cbb26bc654de Chris
66 0:513646585e45 Chris
    EXTENSIONS = MIME_TYPES.inject({}) do |map, (type, exts)|
67
      exts.split(',').each {|ext| map[ext.strip] = type}
68
      map
69
    end
70 909:cbb26bc654de Chris
71 0:513646585e45 Chris
    # returns mime type for name or nil if unknown
72
    def self.of(name)
73
      return nil unless name
74
      m = name.to_s.match(/(^|\.)([^\.]+)$/)
75
      EXTENSIONS[m[2].downcase] if m
76
    end
77 909:cbb26bc654de Chris
78 0:513646585e45 Chris
    # Returns the css class associated to
79
    # the mime type of name
80
    def self.css_class_of(name)
81
      mime = of(name)
82
      mime && mime.gsub('/', '-')
83
    end
84 909:cbb26bc654de Chris
85 0:513646585e45 Chris
    def self.main_mimetype_of(name)
86
      mimetype = of(name)
87
      mimetype.split('/').first if mimetype
88
    end
89 909:cbb26bc654de Chris
90 0:513646585e45 Chris
    # return true if mime-type for name is type/*
91
    # otherwise false
92
    def self.is_type?(type, name)
93
      main_mimetype = main_mimetype_of(name)
94
      type.to_s == main_mimetype
95 909:cbb26bc654de Chris
    end
96 0:513646585e45 Chris
  end
97
end