mime-types 2.99.3 → 3.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/{Code-of-Conduct.rdoc → Code-of-Conduct.md} +29 -30
- data/Contributing.md +132 -0
- data/History.md +269 -0
- data/{Licence.rdoc → Licence.md} +4 -18
- data/Manifest.txt +8 -25
- data/README.rdoc +63 -73
- data/Rakefile +200 -97
- data/lib/mime/type/columnar.rb +30 -63
- data/lib/mime/type.rb +294 -458
- data/lib/mime/types/_columnar.rb +137 -0
- data/lib/mime/types/cache.rb +52 -74
- data/lib/mime/types/columnar.rb +2 -147
- data/lib/mime/types/container.rb +96 -0
- data/lib/mime/types/deprecations.rb +17 -34
- data/lib/mime/types/full.rb +19 -0
- data/lib/mime/types/loader.rb +36 -152
- data/lib/mime/types/logger.rb +7 -5
- data/lib/mime/types/registry.rb +90 -0
- data/lib/mime/types.rb +55 -148
- data/lib/mime-types.rb +2 -2
- data/test/minitest_helper.rb +8 -18
- data/test/test_mime_type.rb +489 -464
- data/test/test_mime_types.rb +139 -91
- data/test/test_mime_types_cache.rb +85 -57
- data/test/test_mime_types_class.rb +120 -100
- data/test/test_mime_types_lazy.rb +30 -28
- data/test/test_mime_types_loader.rb +18 -45
- metadata +92 -77
- data/Contributing.rdoc +0 -170
- data/History-Types.rdoc +0 -454
- data/History.rdoc +0 -590
- data/data/mime-types.json +0 -1
- data/data/mime.content_type.column +0 -1980
- data/data/mime.docs.column +0 -1980
- data/data/mime.encoding.column +0 -1980
- data/data/mime.friendly.column +0 -1980
- data/data/mime.obsolete.column +0 -1980
- data/data/mime.registered.column +0 -1980
- data/data/mime.signature.column +0 -1980
- data/data/mime.use_instead.column +0 -1980
- data/data/mime.xrefs.column +0 -1980
- data/docs/COPYING.txt +0 -339
- data/docs/artistic.txt +0 -127
- data/lib/mime/types/loader_path.rb +0 -15
- data/support/apache_mime_types.rb +0 -108
- data/support/benchmarks/load.rb +0 -64
- data/support/benchmarks/load_allocations.rb +0 -83
- data/support/benchmarks/object_counts.rb +0 -41
- data/support/convert/columnar.rb +0 -88
- data/support/convert.rb +0 -158
- data/support/iana_registry.rb +0 -172
data/lib/mime/types/loader.rb
CHANGED
@@ -1,9 +1,12 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
+
##
|
3
4
|
module MIME; end
|
5
|
+
|
6
|
+
##
|
4
7
|
class MIME::Types; end
|
5
8
|
|
6
|
-
require
|
9
|
+
require "mime/types/data"
|
7
10
|
|
8
11
|
# This class is responsible for initializing the MIME::Types registry from
|
9
12
|
# the data files supplied with the mime-types library.
|
@@ -11,7 +14,7 @@ require 'mime/types/loader_path'
|
|
11
14
|
# The Loader will use one of the following paths:
|
12
15
|
# 1. The +path+ provided in its constructor argument;
|
13
16
|
# 2. The value of ENV['RUBY_MIME_TYPES_DATA']; or
|
14
|
-
# 3. The value of MIME::Types::
|
17
|
+
# 3. The value of MIME::Types::Data::PATH.
|
15
18
|
#
|
16
19
|
# When #load is called, the +path+ will be searched recursively for all YAML
|
17
20
|
# (.yml or .yaml) files. By convention, there is one file for each media
|
@@ -24,15 +27,11 @@ class MIME::Types::Loader
|
|
24
27
|
attr_reader :container
|
25
28
|
|
26
29
|
# Creates a Loader object that can be used to load MIME::Types registries
|
27
|
-
# into memory, using YAML, JSON, or
|
30
|
+
# into memory, using YAML, JSON, or Columnar registry format loaders.
|
28
31
|
def initialize(path = nil, container = nil)
|
29
|
-
path = path || ENV[
|
32
|
+
path = path || ENV["RUBY_MIME_TYPES_DATA"] || MIME::Types::Data::PATH
|
30
33
|
@container = container || MIME::Types.new
|
31
34
|
@path = File.expand_path(path)
|
32
|
-
# begin
|
33
|
-
# require 'mime/lazy_types'
|
34
|
-
# @container.extend(MIME::LazyTypes)
|
35
|
-
# end
|
36
35
|
end
|
37
36
|
|
38
37
|
# Loads a MIME::Types registry from YAML files (<tt>*.yml</tt> or
|
@@ -41,8 +40,7 @@ class MIME::Types::Loader
|
|
41
40
|
# It is expected that the YAML objects contained within the registry array
|
42
41
|
# will be tagged as <tt>!ruby/object:MIME::Type</tt>.
|
43
42
|
#
|
44
|
-
# Note that the YAML format is about 2½ times *slower* than
|
45
|
-
# format or the JSON format.
|
43
|
+
# Note that the YAML format is about 2½ times *slower* than the JSON format.
|
46
44
|
#
|
47
45
|
# NOTE: The purpose of this format is purely for maintenance reasons.
|
48
46
|
def load_yaml
|
@@ -69,7 +67,7 @@ class MIME::Types::Loader
|
|
69
67
|
# Loads a MIME::Types registry from columnar files recursively found in
|
70
68
|
# +path+.
|
71
69
|
def load_columnar
|
72
|
-
require
|
70
|
+
require "mime/types/columnar" unless defined?(MIME::Types::Columnar)
|
73
71
|
container.extend(MIME::Types::Columnar)
|
74
72
|
container.load_base_data(path)
|
75
73
|
|
@@ -81,7 +79,7 @@ class MIME::Types::Loader
|
|
81
79
|
#
|
82
80
|
# This will load from columnar files (#load_columnar) if <tt>columnar:
|
83
81
|
# true</tt> is provided in +options+ and there are columnar files in +path+.
|
84
|
-
def load(options = {
|
82
|
+
def load(options = {columnar: false})
|
85
83
|
if options[:columnar] && !Dir[columnar_path].empty?
|
86
84
|
load_columnar
|
87
85
|
else
|
@@ -89,132 +87,35 @@ class MIME::Types::Loader
|
|
89
87
|
end
|
90
88
|
end
|
91
89
|
|
92
|
-
# Loads a MIME::Types registry from files found in +path+ that are in the
|
93
|
-
# v1 data format. The file search for this will exclude both JSON
|
94
|
-
# (<tt>*.json</tt>) and YAML (<tt>*.yml</tt> or <tt>*.yaml</tt>) files.
|
95
|
-
#
|
96
|
-
# This method has been deprecated and will be removed from mime-types 3.0.
|
97
|
-
def load_v1
|
98
|
-
MIME::Types.deprecated(self.class, __method__)
|
99
|
-
Dir[v1_path].sort.each do |f|
|
100
|
-
next if f =~ /\.(?:ya?ml|json|column)$/
|
101
|
-
container.add(self.class.load_from_v1(f, true), true)
|
102
|
-
end
|
103
|
-
container
|
104
|
-
end
|
105
|
-
|
106
|
-
# Raised when a V1 format file is discovered. This exception will be removed
|
107
|
-
# for mime-types 3.0.
|
108
|
-
BadV1Format = Class.new(Exception)
|
109
|
-
|
110
90
|
class << self
|
111
91
|
# Loads the default MIME::Type registry.
|
112
|
-
def load(options = {
|
92
|
+
def load(options = {columnar: false})
|
113
93
|
new.load(options)
|
114
94
|
end
|
115
95
|
|
116
|
-
# Build the type list from a file in the format:
|
117
|
-
#
|
118
|
-
# [*][!][os:]mt/st[<ws>@ext][<ws>:enc][<ws>'url-list][<ws>=docs]
|
119
|
-
#
|
120
|
-
# == *
|
121
|
-
# An unofficial MIME type. This should be used if and only if the MIME type
|
122
|
-
# is not properly specified (that is, not under either x-type or
|
123
|
-
# vnd.name.type).
|
124
|
-
#
|
125
|
-
# == !
|
126
|
-
# An obsolete MIME type. May be used with an unofficial MIME type.
|
127
|
-
#
|
128
|
-
# == os:
|
129
|
-
# Platform-specific MIME type definition.
|
130
|
-
#
|
131
|
-
# == mt
|
132
|
-
# The media type.
|
133
|
-
#
|
134
|
-
# == st
|
135
|
-
# The media subtype.
|
136
|
-
#
|
137
|
-
# == <ws>@ext
|
138
|
-
# The list of comma-separated extensions.
|
139
|
-
#
|
140
|
-
# == <ws>:enc
|
141
|
-
# The encoding.
|
142
|
-
#
|
143
|
-
# == <ws>'url-list
|
144
|
-
# The list of comma-separated URLs.
|
145
|
-
#
|
146
|
-
# == <ws>=docs
|
147
|
-
# The documentation string.
|
148
|
-
#
|
149
|
-
# That is, everything except the media type and the subtype is optional. The
|
150
|
-
# more information that's available, though, the richer the values that can
|
151
|
-
# be provided.
|
152
|
-
#
|
153
|
-
# This method has been deprecated and will be removed in mime-types 3.0.
|
154
|
-
def load_from_v1(filename, __internal__ = false)
|
155
|
-
MIME::Types.deprecated(self.class, __method__) unless __internal__
|
156
|
-
data = read_file(filename).split($/)
|
157
|
-
mime = MIME::Types.new
|
158
|
-
data.each_with_index { |line, index|
|
159
|
-
item = line.chomp.strip
|
160
|
-
next if item.empty?
|
161
|
-
|
162
|
-
m = V1_FORMAT.match(item)
|
163
|
-
|
164
|
-
unless m
|
165
|
-
MIME::Types.logger.warn <<-EOS
|
166
|
-
#{filename}:#{index + 1}: Parsing error in v1 MIME type definition.
|
167
|
-
=> #{line}
|
168
|
-
EOS
|
169
|
-
fail BadV1Format, line
|
170
|
-
end
|
171
|
-
|
172
|
-
unregistered, obsolete, _, mediatype, subtype, extensions, encoding,
|
173
|
-
urls, docs, _ = *m.captures
|
174
|
-
|
175
|
-
next if mediatype.nil?
|
176
|
-
|
177
|
-
extensions &&= extensions.split(/,/)
|
178
|
-
urls &&= urls.split(/,/)
|
179
|
-
|
180
|
-
if docs.nil?
|
181
|
-
use_instead = nil
|
182
|
-
else
|
183
|
-
use_instead = docs.scan(%r{use-instead:(\S+)}).flatten.first
|
184
|
-
docs = docs.gsub(%r{use-instead:\S+}, '').squeeze(' \t')
|
185
|
-
end
|
186
|
-
|
187
|
-
mime_type = MIME::Type.new("#{mediatype}/#{subtype}") do |t|
|
188
|
-
t.extensions = extensions
|
189
|
-
t.encoding = encoding
|
190
|
-
t.obsolete = obsolete
|
191
|
-
t.registered = false if unregistered
|
192
|
-
t.use_instead = use_instead
|
193
|
-
t.docs = docs
|
194
|
-
end
|
195
|
-
|
196
|
-
mime.add_type(mime_type, true)
|
197
|
-
}
|
198
|
-
mime
|
199
|
-
end
|
200
|
-
|
201
96
|
# Loads MIME::Types from a single YAML file.
|
202
97
|
#
|
203
98
|
# It is expected that the YAML objects contained within the registry
|
204
99
|
# array will be tagged as <tt>!ruby/object:MIME::Type</tt>.
|
205
100
|
#
|
206
|
-
# Note that the YAML format is about 2½ times *slower* than
|
207
|
-
# format
|
101
|
+
# Note that the YAML format is about 2½ times *slower* than the JSON
|
102
|
+
# format.
|
208
103
|
#
|
209
104
|
# NOTE: The purpose of this format is purely for maintenance reasons.
|
210
105
|
def load_from_yaml(filename)
|
211
106
|
begin
|
212
|
-
require
|
107
|
+
require "psych"
|
213
108
|
rescue LoadError
|
214
109
|
nil
|
215
110
|
end
|
216
|
-
|
217
|
-
|
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
|
218
119
|
end
|
219
120
|
|
220
121
|
# Loads MIME::Types from a single JSON file.
|
@@ -223,53 +124,36 @@ class MIME::Types::Loader
|
|
223
124
|
# The JSON format is the registry format for the MIME types registry
|
224
125
|
# shipped with the mime-types library.
|
225
126
|
def load_from_json(filename)
|
226
|
-
require
|
127
|
+
require "json"
|
227
128
|
JSON.parse(read_file(filename)).map { |type| MIME::Type.new(type) }
|
228
129
|
end
|
229
130
|
|
230
131
|
private
|
231
132
|
|
232
133
|
def read_file(filename)
|
233
|
-
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
|
234
143
|
end
|
235
144
|
end
|
236
145
|
|
237
146
|
private
|
238
147
|
|
239
148
|
def yaml_path
|
240
|
-
File.join(path,
|
149
|
+
File.join(path, "*.y{,a}ml")
|
241
150
|
end
|
242
151
|
|
243
152
|
def json_path
|
244
|
-
File.join(path,
|
153
|
+
File.join(path, "*.json")
|
245
154
|
end
|
246
155
|
|
247
156
|
def columnar_path
|
248
|
-
File.join(path,
|
157
|
+
File.join(path, "*.column")
|
249
158
|
end
|
250
|
-
|
251
|
-
def v1_path
|
252
|
-
File.join(path, '*')
|
253
|
-
end
|
254
|
-
|
255
|
-
# The regular expression used to match a v1-format file-based MIME type
|
256
|
-
# definition.
|
257
|
-
#
|
258
|
-
# This constant has been deprecated and will be removed in mime-types 3.0.
|
259
|
-
V1_FORMAT = # :nodoc:
|
260
|
-
%r{\A\s*
|
261
|
-
([*])? # 0: Unregistered?
|
262
|
-
(!)? # 1: Obsolete?
|
263
|
-
(?:(\w+):)? # 2: Platform marker
|
264
|
-
([-\w.+]+)/([-\w.+]*) # 3, 4: Media Type
|
265
|
-
(?:\s+@(\S+))? # 5: Extensions
|
266
|
-
(?:\s+:(base64|7bit|8bit|quoted\-printable))? # 6: Encoding
|
267
|
-
(?:\s+'(\S+))? # 7: URL list
|
268
|
-
(?:\s+=(.+))? # 8: Documentation
|
269
|
-
(?:\s*([#].*)?)? # Comments
|
270
|
-
\s*
|
271
|
-
\z
|
272
|
-
}x
|
273
|
-
|
274
|
-
private_constant :V1_FORMAT if respond_to? :private_constant
|
275
159
|
end
|
data/lib/mime/types/logger.rb
CHANGED
@@ -1,8 +1,10 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "logger"
|
4
4
|
|
5
|
+
##
|
5
6
|
module MIME
|
7
|
+
##
|
6
8
|
class Types
|
7
9
|
class << self
|
8
10
|
# Configure the MIME::Types logger. This defaults to an instance of a
|
@@ -10,8 +12,8 @@ module MIME
|
|
10
12
|
attr_accessor :logger
|
11
13
|
end
|
12
14
|
|
13
|
-
class WarnLogger < ::Logger
|
14
|
-
class WarnLogDevice < ::Logger::LogDevice
|
15
|
+
class WarnLogger < ::Logger # :nodoc:
|
16
|
+
class WarnLogDevice < ::Logger::LogDevice # :nodoc:
|
15
17
|
def initialize(*)
|
16
18
|
end
|
17
19
|
|
@@ -23,7 +25,7 @@ module MIME
|
|
23
25
|
end
|
24
26
|
end
|
25
27
|
|
26
|
-
def initialize(
|
28
|
+
def initialize(_one, _two = nil, _three = nil)
|
27
29
|
super nil
|
28
30
|
@logdev = WarnLogDevice.new
|
29
31
|
@formatter = ->(_s, _d, _p, m) { m }
|
@@ -0,0 +1,90 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class << MIME::Types
|
4
|
+
include Enumerable
|
5
|
+
|
6
|
+
##
|
7
|
+
def new(*) # :nodoc:
|
8
|
+
super.tap do |types|
|
9
|
+
__instances__.add types
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
# MIME::Types#[] against the default MIME::Types registry.
|
14
|
+
def [](type_id, complete: false, registered: false)
|
15
|
+
__types__[type_id, complete: complete, registered: registered]
|
16
|
+
end
|
17
|
+
|
18
|
+
# MIME::Types#count against the default MIME::Types registry.
|
19
|
+
def count
|
20
|
+
__types__.count
|
21
|
+
end
|
22
|
+
|
23
|
+
# MIME::Types#each against the default MIME::Types registry.
|
24
|
+
def each
|
25
|
+
if block_given?
|
26
|
+
__types__.each { |t| yield t }
|
27
|
+
else
|
28
|
+
enum_for(:each)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# MIME::Types#type_for against the default MIME::Types registry.
|
33
|
+
def type_for(filename)
|
34
|
+
__types__.type_for(filename)
|
35
|
+
end
|
36
|
+
alias_method :of, :type_for
|
37
|
+
|
38
|
+
# MIME::Types#add against the default MIME::Types registry.
|
39
|
+
def add(*types)
|
40
|
+
__types__.add(*types)
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def lazy_load?
|
46
|
+
return unless ENV.key?("RUBY_MIME_TYPES_LAZY_LOAD")
|
47
|
+
|
48
|
+
MIME::Types.logger.warn <<-WARNING.chomp.strip
|
49
|
+
Lazy loading ($RUBY_MIME_TYPES_LAZY_LOAD) is deprecated and will be removed.
|
50
|
+
WARNING
|
51
|
+
|
52
|
+
(lazy = ENV["RUBY_MIME_TYPES_LAZY_LOAD"]) && (lazy != "false")
|
53
|
+
end
|
54
|
+
|
55
|
+
def __types__
|
56
|
+
(defined?(@__types__) && @__types__) || load_default_mime_types
|
57
|
+
end
|
58
|
+
|
59
|
+
unless private_method_defined?(:load_mode)
|
60
|
+
def load_mode
|
61
|
+
{columnar: true}
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def load_default_mime_types(mode = load_mode)
|
66
|
+
if (@__types__ = MIME::Types::Cache.load)
|
67
|
+
__instances__.add(@__types__)
|
68
|
+
else
|
69
|
+
@__types__ = MIME::Types::Loader.load(mode)
|
70
|
+
MIME::Types::Cache.save(@__types__)
|
71
|
+
end
|
72
|
+
@__types__
|
73
|
+
end
|
74
|
+
|
75
|
+
def __instances__
|
76
|
+
@__instances__ ||= Set.new
|
77
|
+
end
|
78
|
+
|
79
|
+
def reindex_extensions(type)
|
80
|
+
__instances__.each do |instance|
|
81
|
+
instance.send(:reindex_extensions!, type)
|
82
|
+
end
|
83
|
+
true
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
##
|
88
|
+
class MIME::Types
|
89
|
+
load_default_mime_types(load_mode) unless lazy_load?
|
90
|
+
end
|
data/lib/mime/types.rb
CHANGED
@@ -1,9 +1,13 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
##
|
4
|
+
module MIME
|
5
|
+
##
|
6
|
+
class Types
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
require "mime/type"
|
7
11
|
|
8
12
|
# MIME::Types is a registry of MIME types. It is both a class (created with
|
9
13
|
# MIME::Types.new) and a default registry (loaded automatically or through
|
@@ -57,6 +61,7 @@ require 'mime/types/loader'
|
|
57
61
|
# puts plaintext.ascii? # => true
|
58
62
|
# puts plaintext.obsolete? # => false
|
59
63
|
# puts plaintext.registered? # => true
|
64
|
+
# puts plaintext.provisional? # => false
|
60
65
|
# puts plaintext == 'text/plain' # => true
|
61
66
|
# puts MIME::Type.simplified('x-appl/x-zip') # => 'appl/zip'
|
62
67
|
#
|
@@ -66,37 +71,19 @@ class MIME::Types
|
|
66
71
|
|
67
72
|
include Enumerable
|
68
73
|
|
69
|
-
# The data version.
|
70
|
-
attr_reader :data_version
|
71
|
-
|
72
74
|
# Creates a new MIME::Types registry.
|
73
75
|
def initialize
|
74
|
-
@type_variants
|
75
|
-
@extension_index
|
76
|
-
@data_version = VERSION.dup.freeze
|
77
|
-
end
|
78
|
-
|
79
|
-
# This method is deprecated and will be removed in mime-types 3.0.
|
80
|
-
def add_type_variant(mime_type) # :nodoc:
|
81
|
-
MIME::Types.deprecated(self, __method__, :private)
|
82
|
-
add_type_variant!(mime_type)
|
83
|
-
end
|
84
|
-
|
85
|
-
# This method is deprecated and will be removed in mime-types 3.0.
|
86
|
-
def index_extensions(mime_type) # :nodoc:
|
87
|
-
MIME::Types.deprecated(self, __method__, :private)
|
88
|
-
index_extensions!(mime_type)
|
89
|
-
end
|
90
|
-
|
91
|
-
# This method is deprecated and will be removed in mime-types 3.0.
|
92
|
-
def defined_types # :nodoc:
|
93
|
-
MIME::Types.deprecated(self, __method__)
|
94
|
-
@type_variants.values.flatten
|
76
|
+
@type_variants = Container.new
|
77
|
+
@extension_index = Container.new
|
95
78
|
end
|
96
79
|
|
97
80
|
# Returns the number of known type variants.
|
98
81
|
def count
|
99
|
-
@type_variants.values.
|
82
|
+
@type_variants.values.inject(0) { |a, e| a + e.size }
|
83
|
+
end
|
84
|
+
|
85
|
+
def inspect # :nodoc:
|
86
|
+
"#<#{self.class}: #{count} variants, #{@extension_index.count} extensions>"
|
100
87
|
end
|
101
88
|
|
102
89
|
# Iterates through the type variants.
|
@@ -108,7 +95,7 @@ class MIME::Types
|
|
108
95
|
end
|
109
96
|
end
|
110
97
|
|
111
|
-
@__types__
|
98
|
+
@__types__ = nil
|
112
99
|
|
113
100
|
# Returns a list of MIME::Type objects, which may be empty. The optional
|
114
101
|
# flag parameters are <tt>:complete</tt> (finds only complete MIME::Type
|
@@ -135,23 +122,20 @@ class MIME::Types
|
|
135
122
|
# without;
|
136
123
|
# 5. Obsolete definitions use-instead clauses are compared.
|
137
124
|
# 6. Sort on name.
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
if flags.key?(:platform)
|
142
|
-
MIME::Types.deprecated(self, __method__, 'using the :platform flag')
|
143
|
-
end
|
144
|
-
|
145
|
-
matches = case type_id
|
125
|
+
def [](type_id, complete: false, registered: false)
|
126
|
+
matches =
|
127
|
+
case type_id
|
146
128
|
when MIME::Type
|
147
129
|
@type_variants[type_id.simplified]
|
148
130
|
when Regexp
|
149
131
|
match(type_id)
|
150
132
|
else
|
151
133
|
@type_variants[MIME::Type.simplified(type_id)]
|
152
|
-
|
134
|
+
end
|
153
135
|
|
154
|
-
prune_matches(matches,
|
136
|
+
prune_matches(matches, complete, registered).sort { |a, b|
|
137
|
+
a.priority_compare(b)
|
138
|
+
}
|
155
139
|
end
|
156
140
|
|
157
141
|
# Return the list of MIME::Types which belongs to the file based on its
|
@@ -166,17 +150,12 @@ class MIME::Types
|
|
166
150
|
# => [image/gif]
|
167
151
|
# puts MIME::Types.type_for(%w(citydesk.xml citydesk.gif))
|
168
152
|
# => [application/xml, image/gif, text/xml]
|
169
|
-
|
170
|
-
|
171
|
-
def type_for(filename, platform = :deprecated)
|
172
|
-
types = Array(filename).flat_map { |fn|
|
153
|
+
def type_for(filename)
|
154
|
+
Array(filename).flat_map { |fn|
|
173
155
|
@extension_index[fn.chomp.downcase[/\.?([^.]*?)$/, 1]]
|
174
|
-
}.compact.sort { |a, b|
|
175
|
-
|
176
|
-
|
177
|
-
MIME::Types.deprecated(self, __method__, 'using the platform parameter')
|
178
|
-
end
|
179
|
-
types
|
156
|
+
}.compact.inject(Set.new, :+).sort { |a, b|
|
157
|
+
a.priority_compare(b)
|
158
|
+
}
|
180
159
|
end
|
181
160
|
alias_method :of, :type_for
|
182
161
|
|
@@ -186,7 +165,7 @@ class MIME::Types
|
|
186
165
|
# The last parameter may be the value <tt>:silent</tt> or +true+ which
|
187
166
|
# will suppress duplicate MIME type warnings.
|
188
167
|
def add(*types)
|
189
|
-
quiet = ((types.last == :silent)
|
168
|
+
quiet = ((types.last == :silent) || (types.last == true))
|
190
169
|
|
191
170
|
types.each do |mime_type|
|
192
171
|
case mime_type
|
@@ -194,7 +173,7 @@ class MIME::Types
|
|
194
173
|
nil
|
195
174
|
when MIME::Types
|
196
175
|
variants = mime_type.instance_variable_get(:@type_variants)
|
197
|
-
add(*variants.values.
|
176
|
+
add(*variants.values.inject(Set.new, :+).to_a, quiet)
|
198
177
|
when Array
|
199
178
|
add(*mime_type, quiet)
|
200
179
|
else
|
@@ -207,120 +186,48 @@ class MIME::Types
|
|
207
186
|
# already known, a warning will be displayed. The +quiet+ parameter may be a
|
208
187
|
# truthy value to suppress that warning.
|
209
188
|
def add_type(type, quiet = false)
|
210
|
-
if !quiet
|
211
|
-
MIME::Types.logger.warn <<-
|
212
|
-
Type #{type} is already registered as a variant of #{type.simplified}.
|
213
|
-
|
189
|
+
if !quiet && @type_variants[type.simplified].include?(type)
|
190
|
+
MIME::Types.logger.warn <<-WARNING.chomp.strip
|
191
|
+
Type #{type} is already registered as a variant of #{type.simplified}.
|
192
|
+
WARNING
|
214
193
|
end
|
215
194
|
|
216
195
|
add_type_variant!(type)
|
217
196
|
index_extensions!(type)
|
218
197
|
end
|
219
198
|
|
220
|
-
|
221
|
-
include Enumerable
|
222
|
-
|
223
|
-
# Load MIME::Types from a v1 file registry.
|
224
|
-
#
|
225
|
-
# This method has been deprecated and will be removed in mime-types 3.0.
|
226
|
-
def load_from_file(filename)
|
227
|
-
MIME::Types.deprecated(self, __method__)
|
228
|
-
MIME::Types::Loader.load_from_v1(filename)
|
229
|
-
end
|
230
|
-
|
231
|
-
# MIME::Types#[] against the default MIME::Types registry.
|
232
|
-
def [](type_id, flags = {})
|
233
|
-
__types__[type_id, flags]
|
234
|
-
end
|
235
|
-
|
236
|
-
# MIME::Types#count against the default MIME::Types registry.
|
237
|
-
def count
|
238
|
-
__types__.count
|
239
|
-
end
|
240
|
-
|
241
|
-
# MIME::Types#each against the default MIME::Types registry.
|
242
|
-
def each
|
243
|
-
if block_given?
|
244
|
-
__types__.each { |t| yield t }
|
245
|
-
else
|
246
|
-
enum_for(:each)
|
247
|
-
end
|
248
|
-
end
|
249
|
-
|
250
|
-
# MIME::Types#type_for against the default MIME::Types registry.
|
251
|
-
def type_for(filename, platform = :deprecated)
|
252
|
-
__types__.type_for(filename, platform)
|
253
|
-
end
|
254
|
-
alias_method :of, :type_for
|
255
|
-
|
256
|
-
# MIME::Types#add against the default MIME::Types registry.
|
257
|
-
def add(*types)
|
258
|
-
__types__.add(*types)
|
259
|
-
end
|
260
|
-
|
261
|
-
# Returns the currently defined cache file, if any.
|
262
|
-
#
|
263
|
-
# This method has been deprecated and will be removed in mime-types 3.0.
|
264
|
-
def cache_file
|
265
|
-
MIME::Types.deprecated(self, __method__)
|
266
|
-
ENV['RUBY_MIME_TYPES_CACHE']
|
267
|
-
end
|
268
|
-
|
269
|
-
def add_type_variant(mime_type) # :nodoc:
|
270
|
-
__types__.add_type_variant(mime_type)
|
271
|
-
end
|
272
|
-
|
273
|
-
def index_extensions(mime_type) # :nodoc:
|
274
|
-
__types__.index_extensions(mime_type)
|
275
|
-
end
|
276
|
-
|
277
|
-
private
|
278
|
-
|
279
|
-
def lazy_load?
|
280
|
-
(lazy = ENV['RUBY_MIME_TYPES_LAZY_LOAD']) && (lazy != 'false')
|
281
|
-
end
|
282
|
-
|
283
|
-
def __types__
|
284
|
-
(defined?(@__types__) and @__types__) or load_default_mime_types
|
285
|
-
end
|
286
|
-
|
287
|
-
unless private_method_defined?(:load_mode)
|
288
|
-
def load_mode
|
289
|
-
{}
|
290
|
-
end
|
291
|
-
end
|
199
|
+
private
|
292
200
|
|
293
|
-
|
294
|
-
|
295
|
-
unless @__types__
|
296
|
-
@__types__ = MIME::Types::Loader.load(mode)
|
297
|
-
MIME::Types::Cache.save(@__types__)
|
298
|
-
end
|
299
|
-
@__types__
|
300
|
-
end
|
201
|
+
def add_type_variant!(mime_type)
|
202
|
+
@type_variants.add(mime_type.simplified, mime_type)
|
301
203
|
end
|
302
204
|
|
303
|
-
|
205
|
+
def reindex_extensions!(mime_type)
|
206
|
+
return unless @type_variants[mime_type.simplified].include?(mime_type)
|
304
207
|
|
305
|
-
|
306
|
-
@type_variants[mime_type.simplified] << mime_type
|
208
|
+
index_extensions!(mime_type)
|
307
209
|
end
|
308
210
|
|
309
211
|
def index_extensions!(mime_type)
|
310
|
-
mime_type.extensions.each { |ext| @extension_index
|
212
|
+
mime_type.extensions.each { |ext| @extension_index.add(ext, mime_type) }
|
311
213
|
end
|
312
214
|
|
313
|
-
def prune_matches(matches,
|
314
|
-
matches.delete_if { |e| !e.complete? } if
|
315
|
-
matches.delete_if { |e| !e.registered? } if
|
215
|
+
def prune_matches(matches, complete, registered)
|
216
|
+
matches.delete_if { |e| !e.complete? } if complete
|
217
|
+
matches.delete_if { |e| !e.registered? } if registered
|
316
218
|
matches
|
317
219
|
end
|
318
220
|
|
319
221
|
def match(pattern)
|
320
|
-
@type_variants.select { |k, _|
|
222
|
+
@type_variants.select { |k, _|
|
223
|
+
k =~ pattern
|
224
|
+
}.values.inject(Set.new, :+)
|
321
225
|
end
|
322
|
-
|
323
|
-
load_default_mime_types(load_mode) unless lazy_load?
|
324
226
|
end
|
325
227
|
|
326
|
-
|
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
@@ -1,3 +1,3 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "mime/types"
|