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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2023 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/webp' => 'webp',
  45. 'image/x-ms-bmp' => 'bmp',
  46. 'application/javascript' => 'js',
  47. 'application/pdf' => 'pdf',
  48. 'video/mp4' => 'mp4',
  49. 'video/webm' => 'webm',
  50. }.freeze
  51. EXTENSIONS = MIME_TYPES.inject({}) do |map, (type, exts)|
  52. exts.split(',').each {|ext| map[ext.strip] = type}
  53. map
  54. end
  55. # returns all full mime types for a given (top level) type
  56. def self.by_type(type)
  57. MIME_TYPES.keys.select{|m| m.start_with? "#{type}/"}
  58. end
  59. # returns mime type for name or nil if unknown
  60. def self.of(name)
  61. ext = File.extname(name.to_s)[1..-1]
  62. if ext
  63. ext.downcase!
  64. EXTENSIONS[ext] || MiniMime.lookup_by_extension(ext)&.content_type
  65. end
  66. end
  67. # Returns the css class associated to
  68. # the mime type of name
  69. def self.css_class_of(name)
  70. mimetype = of(name)
  71. mimetype&.tr('/', '-')
  72. end
  73. def self.main_mimetype_of(name)
  74. mimetype = of(name)
  75. mimetype&.split('/')&.first
  76. end
  77. # return true if mime-type for name is type/*
  78. # otherwise false
  79. def self.is_type?(type, name)
  80. main_mimetype = main_mimetype_of(name)
  81. type.to_s == main_mimetype
  82. end
  83. end
  84. end