You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mime_type.rb 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2022 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. module Redmine
  19. module MimeType
  20. MIME_TYPES = {
  21. 'text/plain' => 'txt,tpl,properties,patch,diff,ini,readme,install,upgrade,sql',
  22. 'text/css' => 'css',
  23. 'text/html' => 'html,htm,xhtml',
  24. 'text/jsp' => 'jsp',
  25. 'text/x-c' => 'c,cpp,cc,h,hh',
  26. 'text/x-csharp' => 'cs',
  27. 'text/x-java' => 'java',
  28. 'text/x-html-template' => 'rhtml',
  29. 'text/x-perl' => 'pl,pm',
  30. 'text/x-php' => 'php,php3,php4,php5',
  31. 'text/x-python' => 'py',
  32. 'text/x-ruby' => 'rb,rbw,ruby,rake,erb',
  33. 'text/x-csh' => 'csh',
  34. 'text/x-sh' => 'sh',
  35. 'text/x-textile' => 'textile',
  36. 'text/xml' => 'xml,xsd,mxml',
  37. 'text/yaml' => 'yml,yaml',
  38. 'text/csv' => 'csv',
  39. 'text/x-po' => 'po',
  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. 'application/javascript' => 'js',
  46. 'application/pdf' => 'pdf',
  47. 'video/mp4' => 'mp4',
  48. 'video/webm' => 'webm',
  49. }.freeze
  50. EXTENSIONS = MIME_TYPES.inject({}) do |map, (type, exts)|
  51. exts.split(',').each {|ext| map[ext.strip] = type}
  52. map
  53. end
  54. # returns all full mime types for a given (top level) type
  55. def self.by_type(type)
  56. MIME_TYPES.keys.select{|m| m.start_with? "#{type}/"}
  57. end
  58. # returns mime type for name or nil if unknown
  59. def self.of(name)
  60. ext = File.extname(name.to_s)[1..-1]
  61. if ext
  62. ext.downcase!
  63. EXTENSIONS[ext] || MiniMime.lookup_by_extension(ext)&.content_type
  64. end
  65. end
  66. # Returns the css class associated to
  67. # the mime type of name
  68. def self.css_class_of(name)
  69. mimetype = of(name)
  70. mimetype&.tr('/', '-')
  71. end
  72. def self.main_mimetype_of(name)
  73. mimetype = of(name)
  74. mimetype&.split('/')&.first
  75. end
  76. # return true if mime-type for name is type/*
  77. # otherwise false
  78. def self.is_type?(type, name)
  79. main_mimetype = main_mimetype_of(name)
  80. type.to_s == main_mimetype
  81. end
  82. end
  83. end