mime-types 3.3.1 → 3.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Code-of-Conduct.md +10 -10
- data/Contributing.md +82 -93
- data/History.md +158 -138
- data/Licence.md +3 -3
- data/README.rdoc +1 -0
- data/Rakefile +131 -145
- data/lib/mime/type/columnar.rb +3 -3
- data/lib/mime/type.rb +141 -94
- data/lib/mime/types/_columnar.rb +20 -19
- data/lib/mime/types/cache.rb +8 -8
- data/lib/mime/types/columnar.rb +1 -1
- data/lib/mime/types/container.rb +14 -14
- data/lib/mime/types/deprecations.rb +15 -11
- data/lib/mime/types/full.rb +2 -2
- data/lib/mime/types/loader.rb +28 -15
- data/lib/mime/types/logger.rb +3 -5
- data/lib/mime/types/registry.rb +7 -7
- data/lib/mime/types.rb +18 -16
- data/lib/mime-types.rb +1 -1
- data/test/minitest_helper.rb +7 -9
- data/test/test_mime_type.rb +272 -261
- data/test/test_mime_types.rb +72 -72
- data/test/test_mime_types_cache.rb +38 -38
- data/test/test_mime_types_class.rb +59 -59
- data/test/test_mime_types_lazy.rb +16 -16
- data/test/test_mime_types_loader.rb +14 -14
- metadata +15 -43
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "mime/types/logger"
|
4
4
|
|
5
5
|
# The namespace for MIME applications, tools, and libraries.
|
6
6
|
module MIME
|
@@ -8,25 +8,29 @@ module MIME
|
|
8
8
|
class Types
|
9
9
|
# Used to mark a method as deprecated in the mime-types interface.
|
10
10
|
def self.deprecated(klass, sym, message = nil, &block) # :nodoc:
|
11
|
-
level =
|
11
|
+
level =
|
12
|
+
case klass
|
12
13
|
when Class, Module
|
13
|
-
|
14
|
+
"."
|
14
15
|
else
|
15
16
|
klass = klass.class
|
16
|
-
|
17
|
-
|
18
|
-
message =
|
17
|
+
"#"
|
18
|
+
end
|
19
|
+
message =
|
20
|
+
case message
|
19
21
|
when :private, :protected
|
20
22
|
"and will be #{message}"
|
21
23
|
when nil
|
22
|
-
|
24
|
+
"and will be removed"
|
23
25
|
else
|
24
26
|
message
|
25
|
-
|
26
|
-
MIME::Types.logger.warn
|
27
|
-
#{caller
|
27
|
+
end
|
28
|
+
MIME::Types.logger.warn <<~WARNING.chomp
|
29
|
+
#{caller(2..2).first}: #{klass}#{level}#{sym} is deprecated #{message}.
|
28
30
|
WARNING
|
29
|
-
|
31
|
+
|
32
|
+
return unless block
|
33
|
+
block.call
|
30
34
|
end
|
31
35
|
end
|
32
36
|
end
|
data/lib/mime/types/full.rb
CHANGED
data/lib/mime/types/loader.rb
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
# -*- ruby encoding: utf-8 -*-
|
4
|
-
|
5
3
|
##
|
6
4
|
module MIME; end
|
5
|
+
|
7
6
|
##
|
8
7
|
class MIME::Types; end
|
9
8
|
|
10
|
-
require
|
9
|
+
require "mime/types/data"
|
11
10
|
|
12
11
|
# This class is responsible for initializing the MIME::Types registry from
|
13
12
|
# the data files supplied with the mime-types library.
|
@@ -30,7 +29,7 @@ class MIME::Types::Loader
|
|
30
29
|
# Creates a Loader object that can be used to load MIME::Types registries
|
31
30
|
# into memory, using YAML, JSON, or Columnar registry format loaders.
|
32
31
|
def initialize(path = nil, container = nil)
|
33
|
-
path = path || ENV[
|
32
|
+
path = path || ENV["RUBY_MIME_TYPES_DATA"] || MIME::Types::Data::PATH
|
34
33
|
@container = container || MIME::Types.new
|
35
34
|
@path = File.expand_path(path)
|
36
35
|
end
|
@@ -68,7 +67,7 @@ class MIME::Types::Loader
|
|
68
67
|
# Loads a MIME::Types registry from columnar files recursively found in
|
69
68
|
# +path+.
|
70
69
|
def load_columnar
|
71
|
-
require
|
70
|
+
require "mime/types/columnar" unless defined?(MIME::Types::Columnar)
|
72
71
|
container.extend(MIME::Types::Columnar)
|
73
72
|
container.load_base_data(path)
|
74
73
|
|
@@ -80,7 +79,7 @@ class MIME::Types::Loader
|
|
80
79
|
#
|
81
80
|
# This will load from columnar files (#load_columnar) if <tt>columnar:
|
82
81
|
# true</tt> is provided in +options+ and there are columnar files in +path+.
|
83
|
-
def load(options = {
|
82
|
+
def load(options = {columnar: false})
|
84
83
|
if options[:columnar] && !Dir[columnar_path].empty?
|
85
84
|
load_columnar
|
86
85
|
else
|
@@ -90,7 +89,7 @@ class MIME::Types::Loader
|
|
90
89
|
|
91
90
|
class << self
|
92
91
|
# Loads the default MIME::Type registry.
|
93
|
-
def load(options = {
|
92
|
+
def load(options = {columnar: false})
|
94
93
|
new.load(options)
|
95
94
|
end
|
96
95
|
|
@@ -105,12 +104,18 @@ class MIME::Types::Loader
|
|
105
104
|
# NOTE: The purpose of this format is purely for maintenance reasons.
|
106
105
|
def load_from_yaml(filename)
|
107
106
|
begin
|
108
|
-
require
|
107
|
+
require "psych"
|
109
108
|
rescue LoadError
|
110
109
|
nil
|
111
110
|
end
|
112
|
-
|
113
|
-
|
111
|
+
|
112
|
+
require "yaml"
|
113
|
+
|
114
|
+
if old_yaml?
|
115
|
+
YAML.safe_load(read_file(filename), [MIME::Type])
|
116
|
+
else
|
117
|
+
YAML.safe_load(read_file(filename), permitted_classes: [MIME::Type])
|
118
|
+
end
|
114
119
|
end
|
115
120
|
|
116
121
|
# Loads MIME::Types from a single JSON file.
|
@@ -119,28 +124,36 @@ class MIME::Types::Loader
|
|
119
124
|
# The JSON format is the registry format for the MIME types registry
|
120
125
|
# shipped with the mime-types library.
|
121
126
|
def load_from_json(filename)
|
122
|
-
require
|
127
|
+
require "json"
|
123
128
|
JSON.parse(read_file(filename)).map { |type| MIME::Type.new(type) }
|
124
129
|
end
|
125
130
|
|
126
131
|
private
|
127
132
|
|
128
133
|
def read_file(filename)
|
129
|
-
File.open(filename,
|
134
|
+
File.open(filename, "r:UTF-8:-", &:read)
|
135
|
+
end
|
136
|
+
|
137
|
+
def old_yaml?
|
138
|
+
@old_yaml ||=
|
139
|
+
begin
|
140
|
+
require "rubygems/version"
|
141
|
+
Gem::Version.new(YAML::VERSION) < Gem::Version.new("3.1")
|
142
|
+
end
|
130
143
|
end
|
131
144
|
end
|
132
145
|
|
133
146
|
private
|
134
147
|
|
135
148
|
def yaml_path
|
136
|
-
File.join(path,
|
149
|
+
File.join(path, "*.y{,a}ml")
|
137
150
|
end
|
138
151
|
|
139
152
|
def json_path
|
140
|
-
File.join(path,
|
153
|
+
File.join(path, "*.json")
|
141
154
|
end
|
142
155
|
|
143
156
|
def columnar_path
|
144
|
-
File.join(path,
|
157
|
+
File.join(path, "*.column")
|
145
158
|
end
|
146
159
|
end
|
data/lib/mime/types/logger.rb
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
require 'logger'
|
3
|
+
require "logger"
|
6
4
|
|
7
5
|
##
|
8
6
|
module MIME
|
@@ -14,8 +12,8 @@ module MIME
|
|
14
12
|
attr_accessor :logger
|
15
13
|
end
|
16
14
|
|
17
|
-
class WarnLogger < ::Logger
|
18
|
-
class WarnLogDevice < ::Logger::LogDevice
|
15
|
+
class WarnLogger < ::Logger # :nodoc:
|
16
|
+
class WarnLogDevice < ::Logger::LogDevice # :nodoc:
|
19
17
|
def initialize(*)
|
20
18
|
end
|
21
19
|
|
data/lib/mime/types/registry.rb
CHANGED
@@ -33,7 +33,7 @@ class << MIME::Types
|
|
33
33
|
def type_for(filename)
|
34
34
|
__types__.type_for(filename)
|
35
35
|
end
|
36
|
-
|
36
|
+
alias_method :of, :type_for
|
37
37
|
|
38
38
|
# MIME::Types#add against the default MIME::Types registry.
|
39
39
|
def add(*types)
|
@@ -43,22 +43,22 @@ class << MIME::Types
|
|
43
43
|
private
|
44
44
|
|
45
45
|
def lazy_load?
|
46
|
-
return unless ENV.key?(
|
46
|
+
return unless ENV.key?("RUBY_MIME_TYPES_LAZY_LOAD")
|
47
47
|
|
48
|
-
MIME::Types.logger.warn
|
49
|
-
Lazy loading ($RUBY_MIME_TYPES_LAZY_LOAD) is deprecated and will be removed.
|
48
|
+
MIME::Types.logger.warn <<~WARNING.chomp
|
49
|
+
Lazy loading ($RUBY_MIME_TYPES_LAZY_LOAD) is deprecated and will be removed.
|
50
50
|
WARNING
|
51
51
|
|
52
|
-
(lazy = ENV[
|
52
|
+
(lazy = ENV["RUBY_MIME_TYPES_LAZY_LOAD"]) && (lazy != "false")
|
53
53
|
end
|
54
54
|
|
55
55
|
def __types__
|
56
|
-
(defined?(@__types__)
|
56
|
+
(defined?(@__types__) && @__types__) || load_default_mime_types
|
57
57
|
end
|
58
58
|
|
59
59
|
unless private_method_defined?(:load_mode)
|
60
60
|
def load_mode
|
61
|
-
{
|
61
|
+
{columnar: true}
|
62
62
|
end
|
63
63
|
end
|
64
64
|
|
data/lib/mime/types.rb
CHANGED
@@ -7,7 +7,7 @@ module MIME
|
|
7
7
|
end
|
8
8
|
end
|
9
9
|
|
10
|
-
require
|
10
|
+
require "mime/type"
|
11
11
|
|
12
12
|
# MIME::Types is a registry of MIME types. It is both a class (created with
|
13
13
|
# MIME::Types.new) and a default registry (loaded automatically or through
|
@@ -61,6 +61,7 @@ require 'mime/type'
|
|
61
61
|
# puts plaintext.ascii? # => true
|
62
62
|
# puts plaintext.obsolete? # => false
|
63
63
|
# puts plaintext.registered? # => true
|
64
|
+
# puts plaintext.provisional? # => false
|
64
65
|
# puts plaintext == 'text/plain' # => true
|
65
66
|
# puts MIME::Type.simplified('x-appl/x-zip') # => 'appl/zip'
|
66
67
|
#
|
@@ -72,8 +73,8 @@ class MIME::Types
|
|
72
73
|
|
73
74
|
# Creates a new MIME::Types registry.
|
74
75
|
def initialize
|
75
|
-
@type_variants
|
76
|
-
@extension_index
|
76
|
+
@type_variants = Container.new
|
77
|
+
@extension_index = Container.new
|
77
78
|
end
|
78
79
|
|
79
80
|
# Returns the number of known type variants.
|
@@ -122,14 +123,15 @@ class MIME::Types
|
|
122
123
|
# 5. Obsolete definitions use-instead clauses are compared.
|
123
124
|
# 6. Sort on name.
|
124
125
|
def [](type_id, complete: false, registered: false)
|
125
|
-
matches =
|
126
|
+
matches =
|
127
|
+
case type_id
|
126
128
|
when MIME::Type
|
127
129
|
@type_variants[type_id.simplified]
|
128
130
|
when Regexp
|
129
131
|
match(type_id)
|
130
132
|
else
|
131
133
|
@type_variants[MIME::Type.simplified(type_id)]
|
132
|
-
|
134
|
+
end
|
133
135
|
|
134
136
|
prune_matches(matches, complete, registered).sort { |a, b|
|
135
137
|
a.priority_compare(b)
|
@@ -155,7 +157,7 @@ class MIME::Types
|
|
155
157
|
a.priority_compare(b)
|
156
158
|
}
|
157
159
|
end
|
158
|
-
|
160
|
+
alias_method :of, :type_for
|
159
161
|
|
160
162
|
# Add one or more MIME::Type objects to the set of known types. If the
|
161
163
|
# type is already known, a warning will be displayed.
|
@@ -163,7 +165,7 @@ class MIME::Types
|
|
163
165
|
# The last parameter may be the value <tt>:silent</tt> or +true+ which
|
164
166
|
# will suppress duplicate MIME type warnings.
|
165
167
|
def add(*types)
|
166
|
-
quiet = ((types.last == :silent)
|
168
|
+
quiet = ((types.last == :silent) || (types.last == true))
|
167
169
|
|
168
170
|
types.each do |mime_type|
|
169
171
|
case mime_type
|
@@ -184,9 +186,9 @@ class MIME::Types
|
|
184
186
|
# already known, a warning will be displayed. The +quiet+ parameter may be a
|
185
187
|
# truthy value to suppress that warning.
|
186
188
|
def add_type(type, quiet = false)
|
187
|
-
if !quiet
|
188
|
-
MIME::Types.logger.warn
|
189
|
-
Type #{type} is already registered as a variant of #{type.simplified}.
|
189
|
+
if !quiet && @type_variants[type.simplified].include?(type)
|
190
|
+
MIME::Types.logger.warn <<~WARNING
|
191
|
+
Type #{type} is already registered as a variant of #{type.simplified}.
|
190
192
|
WARNING
|
191
193
|
end
|
192
194
|
|
@@ -223,9 +225,9 @@ Type #{type} is already registered as a variant of #{type.simplified}.
|
|
223
225
|
end
|
224
226
|
end
|
225
227
|
|
226
|
-
require
|
227
|
-
require
|
228
|
-
require
|
229
|
-
require
|
230
|
-
require
|
231
|
-
require
|
228
|
+
require "mime/types/cache"
|
229
|
+
require "mime/types/container"
|
230
|
+
require "mime/types/loader"
|
231
|
+
require "mime/types/logger"
|
232
|
+
require "mime/types/_columnar"
|
233
|
+
require "mime/types/registry"
|
data/lib/mime-types.rb
CHANGED
data/test/minitest_helper.rb
CHANGED
@@ -1,13 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require "mime/type"
|
4
|
+
require "fileutils"
|
5
5
|
|
6
|
-
gem
|
7
|
-
require
|
8
|
-
require
|
9
|
-
require
|
10
|
-
require 'minitest-bonus-assertions'
|
11
|
-
require 'minitest/hooks'
|
6
|
+
gem "minitest"
|
7
|
+
require "minitest/focus"
|
8
|
+
require "minitest-bonus-assertions"
|
9
|
+
require "minitest/hooks"
|
12
10
|
|
13
|
-
ENV[
|
11
|
+
ENV["RUBY_MIME_TYPES_LAZY_LOAD"] = "yes"
|