File: lib/redmine/mime_type.rb

Overview
Module Structure
Code

Overview

Module Structure

  module: <Toplevel Module>
  module: Redmine#18
has properties
module method: of / 1 #72
module method: css_class_of / 1 #80
module method: main_mimetype_of / 1 #85
module method: is_type? / 2 #92
  module: MimeType#19
has properties
constant: MIME_TYPES #21
constant: EXTENSIONS #66

Code

   1  # Redmine - project management software
   2  # Copyright (C) 2006-2011  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        'text/x-po' => 'po',
  41        '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 
  66      EXTENSIONS = MIME_TYPES.inject({}) do |map, (type, exts)|
  67        exts.split(',').each {|ext| map[ext.strip] = type}
  68        map
  69      end
  70 
  71      # 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 
  78      # 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 
  85      def self.main_mimetype_of(name)
  86        mimetype = of(name)
  87        mimetype.split('/').first if mimetype
  88      end
  89 
  90      # 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      end
  96    end
  97  end