##// END OF EJS Templates
Use the mime-types gem for getting mime types from filenames (#15790)....
Jean-Philippe Lang -
r12208:e1189c5335b9
parent child
Show More
@@ -1,104 +1,105
1 source 'https://rubygems.org'
1 source 'https://rubygems.org'
2
2
3 gem "rails", "3.2.16"
3 gem "rails", "3.2.16"
4 gem "jquery-rails", "~> 2.0.2"
4 gem "jquery-rails", "~> 2.0.2"
5 gem "coderay", "~> 1.1.0"
5 gem "coderay", "~> 1.1.0"
6 gem "fastercsv", "~> 1.5.0", :platforms => [:mri_18, :mingw_18, :jruby]
6 gem "fastercsv", "~> 1.5.0", :platforms => [:mri_18, :mingw_18, :jruby]
7 gem "builder", "3.0.0"
7 gem "builder", "3.0.0"
8 gem "mime-types"
8
9
9 # Optional gem for LDAP authentication
10 # Optional gem for LDAP authentication
10 group :ldap do
11 group :ldap do
11 gem "net-ldap", "~> 0.3.1"
12 gem "net-ldap", "~> 0.3.1"
12 end
13 end
13
14
14 # Optional gem for OpenID authentication
15 # Optional gem for OpenID authentication
15 group :openid do
16 group :openid do
16 gem "ruby-openid", "~> 2.3.0", :require => "openid"
17 gem "ruby-openid", "~> 2.3.0", :require => "openid"
17 gem "rack-openid"
18 gem "rack-openid"
18 end
19 end
19
20
20 platforms :mri, :mingw do
21 platforms :mri, :mingw do
21 # Optional gem for exporting the gantt to a PNG file, not supported with jruby
22 # Optional gem for exporting the gantt to a PNG file, not supported with jruby
22 group :rmagick do
23 group :rmagick do
23 # RMagick 2 supports ruby 1.9
24 # RMagick 2 supports ruby 1.9
24 # RMagick 1 would be fine for ruby 1.8 but Bundler does not support
25 # RMagick 1 would be fine for ruby 1.8 but Bundler does not support
25 # different requirements for the same gem on different platforms
26 # different requirements for the same gem on different platforms
26 gem "rmagick", ">= 2.0.0"
27 gem "rmagick", ">= 2.0.0"
27 end
28 end
28
29
29 # Optional Markdown support, not for JRuby
30 # Optional Markdown support, not for JRuby
30 group :markdown do
31 group :markdown do
31 # TODO: upgrade to redcarpet 3.x when ruby1.8 support is dropped
32 # TODO: upgrade to redcarpet 3.x when ruby1.8 support is dropped
32 gem "redcarpet", "~> 2.3.0"
33 gem "redcarpet", "~> 2.3.0"
33 end
34 end
34 end
35 end
35
36
36 platforms :jruby do
37 platforms :jruby do
37 # jruby-openssl is bundled with JRuby 1.7.0
38 # jruby-openssl is bundled with JRuby 1.7.0
38 gem "jruby-openssl" if Object.const_defined?(:JRUBY_VERSION) && JRUBY_VERSION < '1.7.0'
39 gem "jruby-openssl" if Object.const_defined?(:JRUBY_VERSION) && JRUBY_VERSION < '1.7.0'
39 gem "activerecord-jdbc-adapter", "~> 1.3.2"
40 gem "activerecord-jdbc-adapter", "~> 1.3.2"
40 end
41 end
41
42
42 # Include database gems for the adapters found in the database
43 # Include database gems for the adapters found in the database
43 # configuration file
44 # configuration file
44 require 'erb'
45 require 'erb'
45 require 'yaml'
46 require 'yaml'
46 database_file = File.join(File.dirname(__FILE__), "config/database.yml")
47 database_file = File.join(File.dirname(__FILE__), "config/database.yml")
47 if File.exist?(database_file)
48 if File.exist?(database_file)
48 database_config = YAML::load(ERB.new(IO.read(database_file)).result)
49 database_config = YAML::load(ERB.new(IO.read(database_file)).result)
49 adapters = database_config.values.map {|c| c['adapter']}.compact.uniq
50 adapters = database_config.values.map {|c| c['adapter']}.compact.uniq
50 if adapters.any?
51 if adapters.any?
51 adapters.each do |adapter|
52 adapters.each do |adapter|
52 case adapter
53 case adapter
53 when 'mysql2'
54 when 'mysql2'
54 gem "mysql2", "~> 0.3.11", :platforms => [:mri, :mingw]
55 gem "mysql2", "~> 0.3.11", :platforms => [:mri, :mingw]
55 gem "activerecord-jdbcmysql-adapter", :platforms => :jruby
56 gem "activerecord-jdbcmysql-adapter", :platforms => :jruby
56 when 'mysql'
57 when 'mysql'
57 gem "mysql", "~> 2.8.1", :platforms => [:mri, :mingw]
58 gem "mysql", "~> 2.8.1", :platforms => [:mri, :mingw]
58 gem "activerecord-jdbcmysql-adapter", :platforms => :jruby
59 gem "activerecord-jdbcmysql-adapter", :platforms => :jruby
59 when /postgresql/
60 when /postgresql/
60 gem "pg", ">= 0.11.0", :platforms => [:mri, :mingw]
61 gem "pg", ">= 0.11.0", :platforms => [:mri, :mingw]
61 gem "activerecord-jdbcpostgresql-adapter", :platforms => :jruby
62 gem "activerecord-jdbcpostgresql-adapter", :platforms => :jruby
62 when /sqlite3/
63 when /sqlite3/
63 gem "sqlite3", :platforms => [:mri, :mingw]
64 gem "sqlite3", :platforms => [:mri, :mingw]
64 gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
65 gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
65 when /sqlserver/
66 when /sqlserver/
66 gem "tiny_tds", "~> 0.5.1", :platforms => [:mri, :mingw]
67 gem "tiny_tds", "~> 0.5.1", :platforms => [:mri, :mingw]
67 gem "activerecord-sqlserver-adapter", :platforms => [:mri, :mingw]
68 gem "activerecord-sqlserver-adapter", :platforms => [:mri, :mingw]
68 else
69 else
69 warn("Unknown database adapter `#{adapter}` found in config/database.yml, use Gemfile.local to load your own database gems")
70 warn("Unknown database adapter `#{adapter}` found in config/database.yml, use Gemfile.local to load your own database gems")
70 end
71 end
71 end
72 end
72 else
73 else
73 warn("No adapter found in config/database.yml, please configure it first")
74 warn("No adapter found in config/database.yml, please configure it first")
74 end
75 end
75 else
76 else
76 warn("Please configure your config/database.yml first")
77 warn("Please configure your config/database.yml first")
77 end
78 end
78
79
79 group :development do
80 group :development do
80 gem "rdoc", ">= 2.4.2"
81 gem "rdoc", ">= 2.4.2"
81 gem "yard"
82 gem "yard"
82 end
83 end
83
84
84 group :test do
85 group :test do
85 gem "shoulda", "~> 3.3.2"
86 gem "shoulda", "~> 3.3.2"
86 gem "mocha", ">= 0.14", :require => 'mocha/api'
87 gem "mocha", ">= 0.14", :require => 'mocha/api'
87 if RUBY_VERSION >= '1.9.3'
88 if RUBY_VERSION >= '1.9.3'
88 gem "capybara", "~> 2.1.0"
89 gem "capybara", "~> 2.1.0"
89 gem "selenium-webdriver"
90 gem "selenium-webdriver"
90 end
91 end
91 end
92 end
92
93
93 local_gemfile = File.join(File.dirname(__FILE__), "Gemfile.local")
94 local_gemfile = File.join(File.dirname(__FILE__), "Gemfile.local")
94 if File.exists?(local_gemfile)
95 if File.exists?(local_gemfile)
95 puts "Loading Gemfile.local ..." if $DEBUG # `ruby -d` or `bundle -v`
96 puts "Loading Gemfile.local ..." if $DEBUG # `ruby -d` or `bundle -v`
96 instance_eval File.read(local_gemfile)
97 instance_eval File.read(local_gemfile)
97 end
98 end
98
99
99 # Load plugins' Gemfiles
100 # Load plugins' Gemfiles
100 Dir.glob File.expand_path("../plugins/*/Gemfile", __FILE__) do |file|
101 Dir.glob File.expand_path("../plugins/*/Gemfile", __FILE__) do |file|
101 puts "Loading #{file} ..." if $DEBUG # `ruby -d` or `bundle -v`
102 puts "Loading #{file} ..." if $DEBUG # `ruby -d` or `bundle -v`
102 #TODO: switch to "eval_gemfile file" when bundler >= 1.2.0 will be required (rails 4)
103 #TODO: switch to "eval_gemfile file" when bundler >= 1.2.0 will be required (rails 4)
103 instance_eval File.read(file), file
104 instance_eval File.read(file), file
104 end
105 end
@@ -1,98 +1,86
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
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
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.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require 'mime/types'
19
18 module Redmine
20 module Redmine
19 module MimeType
21 module MimeType
20
22
21 MIME_TYPES = {
23 MIME_TYPES = {
22 'text/plain' => 'txt,tpl,properties,patch,diff,ini,readme,install,upgrade',
24 'text/plain' => 'txt,tpl,properties,patch,diff,ini,readme,install,upgrade',
23 'text/css' => 'css',
25 'text/css' => 'css',
24 'text/html' => 'html,htm,xhtml',
26 'text/html' => 'html,htm,xhtml',
25 'text/jsp' => 'jsp',
27 'text/jsp' => 'jsp',
26 'text/x-c' => 'c,cpp,cc,h,hh',
28 'text/x-c' => 'c,cpp,cc,h,hh',
27 'text/x-csharp' => 'cs',
29 'text/x-csharp' => 'cs',
28 'text/x-java' => 'java',
30 'text/x-java' => 'java',
29 'text/x-html-template' => 'rhtml',
31 'text/x-html-template' => 'rhtml',
30 'text/x-perl' => 'pl,pm',
32 'text/x-perl' => 'pl,pm',
31 'text/x-php' => 'php,php3,php4,php5',
33 'text/x-php' => 'php,php3,php4,php5',
32 'text/x-python' => 'py',
34 'text/x-python' => 'py',
33 'text/x-ruby' => 'rb,rbw,ruby,rake,erb',
35 'text/x-ruby' => 'rb,rbw,ruby,rake,erb',
34 'text/x-csh' => 'csh',
36 'text/x-csh' => 'csh',
35 'text/x-sh' => 'sh',
37 'text/x-sh' => 'sh',
36 'text/xml' => 'xml,xsd,mxml',
38 'text/xml' => 'xml,xsd,mxml',
37 'text/yaml' => 'yml,yaml',
39 'text/yaml' => 'yml,yaml',
38 'text/csv' => 'csv',
40 'text/csv' => 'csv',
39 'text/x-po' => 'po',
41 'text/x-po' => 'po',
40 'image/gif' => 'gif',
42 'image/gif' => 'gif',
41 'image/jpeg' => 'jpg,jpeg,jpe',
43 'image/jpeg' => 'jpg,jpeg,jpe',
42 'image/png' => 'png',
44 'image/png' => 'png',
43 'image/tiff' => 'tiff,tif',
45 'image/tiff' => 'tiff,tif',
44 'image/x-ms-bmp' => 'bmp',
46 'image/x-ms-bmp' => 'bmp',
45 'image/x-xpixmap' => 'xpm',
46 'image/svg+xml'=> 'svg',
47 'application/javascript' => 'js',
47 'application/javascript' => 'js',
48 'application/pdf' => 'pdf',
48 'application/pdf' => 'pdf',
49 'application/rtf' => 'rtf',
50 'application/msword' => 'doc',
51 'application/vnd.ms-excel' => 'xls',
52 'application/vnd.ms-powerpoint' => 'ppt,pps',
53 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'docx',
54 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => 'xlsx',
55 'application/vnd.openxmlformats-officedocument.presentationml.presentation' => 'pptx',
56 'application/vnd.openxmlformats-officedocument.presentationml.slideshow' => 'ppsx',
57 'application/vnd.oasis.opendocument.spreadsheet' => 'ods',
58 'application/vnd.oasis.opendocument.text' => 'odt',
59 'application/vnd.oasis.opendocument.presentation' => 'odp',
60 'application/x-7z-compressed' => '7z',
61 'application/x-rar-compressed' => 'rar',
62 'application/x-tar' => 'tar',
63 'application/zip' => 'zip',
64 'application/x-gzip' => 'gz',
65 }.freeze
49 }.freeze
66
50
67 EXTENSIONS = MIME_TYPES.inject({}) do |map, (type, exts)|
51 EXTENSIONS = MIME_TYPES.inject({}) do |map, (type, exts)|
68 exts.split(',').each {|ext| map[ext.strip] = type}
52 exts.split(',').each {|ext| map[ext.strip] = type}
69 map
53 map
70 end
54 end
71
55
72 # returns mime type for name or nil if unknown
56 # returns mime type for name or nil if unknown
73 def self.of(name)
57 def self.of(name)
74 return nil unless name
58 return nil unless name
75 m = name.to_s.match(/(^|\.)([^\.]+)$/)
59 m = name.to_s.match(/(^|\.)([^\.]+)$/)
76 EXTENSIONS[m[2].downcase] if m
60 ext = m[2].downcase
61 type = nil
62 type = EXTENSIONS[ext] if m
63 type ||= MIME::Types.find {|type| type.extensions.include?(ext)}.to_s.presence
64 type
77 end
65 end
78
66
79 # Returns the css class associated to
67 # Returns the css class associated to
80 # the mime type of name
68 # the mime type of name
81 def self.css_class_of(name)
69 def self.css_class_of(name)
82 mime = of(name)
70 mime = of(name)
83 mime && mime.gsub('/', '-')
71 mime && mime.gsub('/', '-')
84 end
72 end
85
73
86 def self.main_mimetype_of(name)
74 def self.main_mimetype_of(name)
87 mimetype = of(name)
75 mimetype = of(name)
88 mimetype.split('/').first if mimetype
76 mimetype.split('/').first if mimetype
89 end
77 end
90
78
91 # return true if mime-type for name is type/*
79 # return true if mime-type for name is type/*
92 # otherwise false
80 # otherwise false
93 def self.is_type?(type, name)
81 def self.is_type?(type, name)
94 main_mimetype = main_mimetype_of(name)
82 main_mimetype = main_mimetype_of(name)
95 type.to_s == main_mimetype
83 type.to_s == main_mimetype
96 end
84 end
97 end
85 end
98 end
86 end
@@ -1,61 +1,66
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
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
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.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require File.expand_path('../../../../test_helper', __FILE__)
18 require File.expand_path('../../../../test_helper', __FILE__)
19
19
20 class Redmine::MimeTypeTest < ActiveSupport::TestCase
20 class Redmine::MimeTypeTest < ActiveSupport::TestCase
21
21
22 def test_of
22 def test_of
23 to_test = {'test.unk' => nil,
23 to_test = {'test.unk' => nil,
24 'test.txt' => 'text/plain',
24 'test.txt' => 'text/plain',
25 'test.c' => 'text/x-c',
25 'test.c' => 'text/x-c',
26 }
26 }
27 to_test.each do |name, expected|
27 to_test.each do |name, expected|
28 assert_equal expected, Redmine::MimeType.of(name)
28 assert_equal expected, Redmine::MimeType.of(name)
29 end
29 end
30 end
30 end
31
31
32 def test_css_class_of
32 def test_css_class_of
33 to_test = {'test.unk' => nil,
33 to_test = {'test.unk' => nil,
34 'test.txt' => 'text-plain',
34 'test.txt' => 'text-plain',
35 'test.c' => 'text-x-c',
35 'test.c' => 'text-x-c',
36 }
36 }
37 to_test.each do |name, expected|
37 to_test.each do |name, expected|
38 assert_equal expected, Redmine::MimeType.css_class_of(name)
38 assert_equal expected, Redmine::MimeType.css_class_of(name)
39 end
39 end
40 end
40 end
41
41
42 def test_main_mimetype_of
42 def test_main_mimetype_of
43 to_test = {'test.unk' => nil,
43 to_test = {'test.unk' => nil,
44 'test.txt' => 'text',
44 'test.txt' => 'text',
45 'test.c' => 'text',
45 'test.c' => 'text',
46 }
46 }
47 to_test.each do |name, expected|
47 to_test.each do |name, expected|
48 assert_equal expected, Redmine::MimeType.main_mimetype_of(name)
48 assert_equal expected, Redmine::MimeType.main_mimetype_of(name)
49 end
49 end
50 end
50 end
51
51
52 def test_is_type
52 def test_is_type
53 to_test = {['text', 'test.unk'] => false,
53 to_test = {['text', 'test.unk'] => false,
54 ['text', 'test.txt'] => true,
54 ['text', 'test.txt'] => true,
55 ['text', 'test.c'] => true,
55 ['text', 'test.c'] => true,
56 }
56 }
57 to_test.each do |args, expected|
57 to_test.each do |args, expected|
58 assert_equal expected, Redmine::MimeType.is_type?(*args)
58 assert_equal expected, Redmine::MimeType.is_type?(*args)
59 end
59 end
60 end
60 end
61
62 def test_should_default_to_mime_type_gem
63 assert !Redmine::MimeType::EXTENSIONS.keys.include?("zip")
64 assert_equal "application/zip", Redmine::MimeType.of("file.zip")
65 end
61 end
66 end
General Comments 0
You need to be logged in to leave comments. Login now