mime-types 2.3 → 2.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -97,7 +97,11 @@ class MIME::Types
97
97
 
98
98
  # Iterates through the type variants.
99
99
  def each
100
- @type_variants.values.each { |tv| tv.each { |t| yield t } }
100
+ if block_given?
101
+ @type_variants.each_value { |tv| tv.each { |t| yield t } }
102
+ else
103
+ enum_for(:each)
104
+ end
101
105
  end
102
106
 
103
107
  @__types__ = nil
@@ -165,7 +169,7 @@ class MIME::Types
165
169
  # current platform will be returned. This parameter has been deprecated.
166
170
  def type_for(filename, platform = false)
167
171
  types = Array(filename).flat_map { |fn|
168
- @extension_index[File.basename(fn.chomp.downcase).gsub(/.*\./o, '')]
172
+ @extension_index[fn.chomp.downcase[/\.?([^.]*?)$/, 1]]
169
173
  }.compact.sort { |a, b| a.priority_compare(b) }.uniq
170
174
 
171
175
  if platform
@@ -206,8 +210,8 @@ class MIME::Types
206
210
  # a truthy value to suppress that warning.
207
211
  def add_type(mime_type, quiet = false)
208
212
  if !quiet and @type_variants[mime_type.simplified].include?(mime_type)
209
- warn("Type %s is already registered as a variant of %s." % [
210
- mime_type, mime_type.simplified ])
213
+ warn('Type %s is already registered as a variant of %s.' %
214
+ [ mime_type, mime_type.simplified ])
211
215
  end
212
216
 
213
217
  add_type_variant!(mime_type)
@@ -237,7 +241,11 @@ class MIME::Types
237
241
 
238
242
  # MIME::Types#each against the default MIME::Types registry.
239
243
  def each
240
- __types__.each {|t| yield t }
244
+ if block_given?
245
+ __types__.each { |t| yield t }
246
+ else
247
+ enum_for(:each)
248
+ end
241
249
  end
242
250
 
243
251
  # MIME::Types#type_for against the default MIME::Types registry.
@@ -11,10 +11,8 @@ require 'mime/types/loader_path'
11
11
  # 3. The value of MIME::Types::Loader::PATH.
12
12
  #
13
13
  # When #load is called, the +path+ will be searched recursively for all YAML
14
- # (.yml or .yaml) files. By convention, there is one file for each media type
15
- # (application.yml, audio.yml, etc.), but this is not required.
16
- #
17
- #
14
+ # (.yml or .yaml) files. By convention, there is one file for each media
15
+ # type (application.yml, audio.yml, etc.), but this is not required.
18
16
  class MIME::Types::Loader
19
17
  # The path that will be read for the MIME::Types files.
20
18
  attr_reader :path
@@ -81,6 +79,8 @@ class MIME::Types::Loader
81
79
  container
82
80
  end
83
81
 
82
+ BadV1Format = Class.new(Exception)
83
+
84
84
  class << self
85
85
  # Loads the default MIME::Type registry.
86
86
  def load
@@ -124,36 +124,27 @@ class MIME::Types::Loader
124
124
  # more information that's available, though, the richer the values that can
125
125
  # be provided.
126
126
  def load_from_v1(filename)
127
+ MIME.deprecated(self.class, __method__)
127
128
  data = read_file(filename).split($/)
128
129
  mime = MIME::Types.new
129
130
  data.each_with_index { |line, index|
130
131
  item = line.chomp.strip
131
132
  next if item.empty?
132
133
 
133
- begin
134
- m = MIME::Types::Loader::V1_FORMAT.match(item).captures
135
- rescue Exception
134
+ m = MIME::Types::Loader::V1_FORMAT.match(item)
135
+
136
+ unless m
136
137
  warn <<-EOS
137
- #{filename}:#{index}: Parsing error in v1 MIME type definition.
138
+ #{filename}:#{index + 1}: Parsing error in v1 MIME type definition.
138
139
  => #{line}
139
140
  EOS
140
- raise
141
+ raise BadV1Format, line
141
142
  end
142
143
 
143
144
  unregistered, obsolete, platform, mediatype, subtype, extensions,
144
- encoding, urls, docs, comment = *m
145
+ encoding, urls, docs, comment = *m.captures
145
146
 
146
- if mediatype.nil?
147
- if comment.nil?
148
- warn <<-EOS
149
- #{filename}:#{index}: Parsing error in v1 MIME type definition (no media type).
150
- => #{line}
151
- EOS
152
- raise
153
- end
154
-
155
- next
156
- end
147
+ next if mediatype.nil?
157
148
 
158
149
  extensions &&= extensions.split(/,/)
159
150
  urls &&= urls.split(/,/)
@@ -0,0 +1,55 @@
1
+ # -*- ruby encoding: utf-8 -*-
2
+
3
+ require 'benchmark'
4
+
5
+ module Benchmarks
6
+ class Load
7
+ def self.report(load_path, repeats)
8
+ new(load_path, repeats.to_i).report
9
+ end
10
+
11
+ def initialize(load_path, repeats = nil)
12
+ @cache_file = File.expand_path('../cache.mtc', __FILE__)
13
+ @repeats = repeats.to_i
14
+ @repeats = 50 if repeats <= 0
15
+ @load_path = load_path
16
+ end
17
+
18
+ def reload_mime_types(repeats = 1, force_load = false)
19
+ repeats.times {
20
+ Object.send(:remove_const, :MIME) if defined? ::MIME
21
+ $LOADED_FEATURES.delete_if { |n| n =~ /#{@load_path}/ }
22
+ require 'mime/types'
23
+ ::MIME::Types.send(:__types__) if force_load
24
+ }
25
+ end
26
+
27
+ def report
28
+ remove_cache
29
+
30
+ Benchmark.bm(17) do |mark|
31
+ mark.report("Normal:") { reload_mime_types(@repeats) }
32
+
33
+ ENV['RUBY_MIME_TYPES_LAZY_LOAD'] = 'yes'
34
+ mark.report("Lazy:") { reload_mime_types(@repeats) }
35
+ mark.report("Lazy+Load:") { reload_mime_types(@repeats, true) }
36
+
37
+ ENV.delete('RUBY_MIME_TYPES_LAZY_LOAD')
38
+
39
+ ENV['RUBY_MIME_TYPES_CACHE'] = @cache_file
40
+ reload_mime_types
41
+
42
+ mark.report("Cached:") { reload_mime_types(@repeats) }
43
+ ENV['RUBY_MIME_TYPES_LAZY_LOAD'] = 'yes'
44
+ mark.report("Lazy Cached:") { reload_mime_types(@repeats) }
45
+ mark.report("Lazy Cached Load:") { reload_mime_types(@repeats, true) }
46
+ end
47
+ ensure
48
+ remove_cache
49
+ end
50
+
51
+ def remove_cache
52
+ File.unlink(@cache_file) if File.exist?(@cache_file)
53
+ end
54
+ end
55
+ end
@@ -6,28 +6,36 @@ require 'fileutils'
6
6
 
7
7
  class Convert
8
8
  class << self
9
+ # Create a Convert instance that converts from YAML.
9
10
  def from_yaml(path = nil)
10
11
  new(path: path, from: :yaml)
11
12
  end
12
13
 
14
+ # Create a Convert instance that converts from JSON.
13
15
  def from_json(path = nil)
14
16
  new(path: path, from: :json)
15
17
  end
16
18
 
19
+ # Create a Convert instance that converts from the mime-types 1.x file
20
+ # format.
17
21
  def from_v1(path = nil)
18
22
  new(path: path, from: :v1)
19
23
  end
20
24
 
25
+ # Converts from YAML to JSON. Defaults to converting to a single file.
21
26
  def from_yaml_to_json(args)
27
+ mf = args.multiple_files || "single"
22
28
  from_yaml(yaml_path(args.source)).
23
29
  to_json(destination: json_path(args.destination),
24
- multiple_files: multiple_files(args.multiple_files))
30
+ multiple_files: multiple_files(mf))
25
31
  end
26
32
 
33
+ # Converts from JSON to YAML. Defaults to converting to multiple files.
27
34
  def from_json_to_yaml(args)
35
+ mf = args.multiple_files || "multiple"
28
36
  from_json(json_path(args.source)).
29
37
  to_yaml(destination: yaml_path(args.destination),
30
- multiple_files: multiple_files(args.multiple_files))
38
+ multiple_files: multiple_files(mf))
31
39
  end
32
40
 
33
41
  private :new
@@ -50,8 +58,8 @@ class Convert
50
58
  end
51
59
 
52
60
  def multiple_files(flag)
53
- case flag
54
- when "true", "yes"
61
+ case flag.to_s.downcase
62
+ when "true", "yes", "multiple"
55
63
  true
56
64
  else
57
65
  false
@@ -73,11 +81,13 @@ class Convert
73
81
  load_from(options[:from])
74
82
  end
75
83
 
84
+ # Convert the data to JSON.
76
85
  def to_json(options = {})
77
86
  raise ArgumentError, 'destination is required' unless options[:destination]
78
87
  write_types(options.merge(format: :json))
79
88
  end
80
89
 
90
+ # Convert the data to YAML.
81
91
  def to_yaml(options = {})
82
92
  raise ArgumentError, 'destination is required' unless options[:destination]
83
93
  write_types(options.merge(format: :yaml))
@@ -0,0 +1,9 @@
1
+ !application.smil @smi,smil :8bit 'IANA,RFC4536 =use-instead:application/smil+xml
2
+ !audio/vnd.qcelp @qcp 'IANA,RFC3625 =use-instead:audio/QCELP
3
+ *!image/bmp @bmp =use-instead:image/x-bmp
4
+ *application/acad 'LTSW
5
+ *audio/webm @webm '{WebM=http://www.webmproject.org/code/specs/container/}
6
+ *image/pjpeg :base64 =Fixes a bug with IE6 and progressive JPEGs
7
+ application/1d-interleaved-parityfec 'IANA,RFC6015
8
+ audio/1d-interleaved-parityfec 'IANA,RFC6015
9
+ mac:application/x-apple-diskimage @dmg
@@ -8,11 +8,28 @@ class TestMIMEType < Minitest::Test
8
8
  MIME::Type.new(content_type) { |mt| yield mt if block_given? }
9
9
  end
10
10
 
11
+ def make_javascript
12
+ make('application/javascript') do |js|
13
+ js.friendly('en' => 'JavaScript')
14
+ js.xrefs = {
15
+ 'rfc' => %w(rfc4239 rfc4239),
16
+ 'template' => %w(application/javascript)
17
+ }
18
+ js.encoding = '8bit'
19
+ js.extensions = %w(js sj)
20
+ js.references = %w(IANA RFC4329 {application/javascript=http://www.iana.org/assignments/media-types/application/javascript})
21
+ js.registered = true
22
+
23
+ yield js if block_given?
24
+ end
25
+ end
26
+
11
27
  def make_yaml_mime_type
12
28
  make('text/x-yaml') do |yaml|
13
29
  yaml.extensions = %w(yaml yml)
14
30
  yaml.encoding = '8bit'
15
31
  yaml.system = 'd9d172f608'
32
+ yaml.friendly('en' => 'YAML Structured Document')
16
33
  end
17
34
  end
18
35
 
@@ -74,6 +91,16 @@ class TestMIMEType < Minitest::Test
74
91
  assert_nil(MIME::Type.simplified('text'))
75
92
  end
76
93
 
94
+ def test_class_i18n_key
95
+ assert_equal('text.plain', MIME::Type.i18n_key('text/plain'))
96
+ assert_equal('image.jpeg', MIME::Type.i18n_key('image/jpeg'))
97
+ assert_equal('application.msword', MIME::Type.i18n_key('application/x-msword'))
98
+ assert_equal('text.vcard', MIME::Type.i18n_key('text/vCard'))
99
+ assert_equal('application.pkcs7-mime', MIME::Type.i18n_key('application/pkcs7-mime'))
100
+ assert_equal('xyz.abc', MIME::Type.i18n_key('x-xyz/abc'))
101
+ assert_nil(MIME::Type.i18n_key('text'))
102
+ end
103
+
77
104
  def test_CMP # '<=>'
78
105
  assert(MIME::Type.new('text/plain') == MIME::Type.new('text/plain'))
79
106
  assert(MIME::Type.new('text/plain') != MIME::Type.new('image/jpeg'))
@@ -204,7 +231,7 @@ class TestMIMEType < Minitest::Test
204
231
  assert_equal(%w(yaml), yaml.extensions)
205
232
 
206
233
  yaml.extensions = %w(yz yaml yz yml)
207
- assert_equal(%w(yaml yml yz), yaml.extensions)
234
+ assert_equal(%w(yz yaml yml), yaml.extensions)
208
235
  end
209
236
 
210
237
  def test_like_eh
@@ -461,6 +488,40 @@ class TestMIMEType < Minitest::Test
461
488
  assert_equal(%W(IANA), yaml.references)
462
489
  end
463
490
 
491
+ def test_xrefs
492
+ assert_equal(
493
+ {
494
+ 'rfc' => %w(rfc4239),
495
+ 'template' => %w(application/javascript)
496
+ },
497
+ make_javascript.xrefs
498
+ )
499
+ end
500
+
501
+ def test_xref_urls
502
+ js = make_javascript do |j|
503
+ j.xrefs = j.xrefs.merge({
504
+ 'draft' => [ 'RFC-ietf-appsawg-json-merge-patch-07' ],
505
+ 'person' => [ 'David_Singer' ],
506
+ 'rfc-errata' => [ '3245' ],
507
+ 'uri' => [ 'http://exmple.org' ],
508
+ 'text' => [ 'text' ]
509
+ })
510
+ end
511
+ assert_equal(
512
+ [
513
+ "http://www.iana.org/go/rfc4239",
514
+ "http://www.iana.org/assignments/media-types/application/javascript",
515
+ "http://www.iana.org/go/draft-ietf-appsawg-json-merge-patch-07",
516
+ "http://www.iana.org/assignments/media-types/media-types.xhtml#David_Singer",
517
+ "http://www.rfc-editor.org/errata_search.php?eid=3245",
518
+ "http://exmple.org",
519
+ "text"
520
+ ],
521
+ js.xref_urls
522
+ )
523
+ end
524
+
464
525
  def test_url
465
526
  assert_deprecated("MIME::Type#url", "and has been renamed to #references") do
466
527
  assert_empty(make_yaml_mime_type.url)
@@ -507,6 +568,29 @@ class TestMIMEType < Minitest::Test
507
568
  assert_equal('t/2', t.use_instead)
508
569
  end
509
570
 
571
+ def test_preferred_extension
572
+ assert_equal('zip', @applzip.preferred_extension)
573
+ end
574
+
575
+ def test_friendly_read
576
+ yaml = make_yaml_mime_type
577
+ assert_equal('YAML Structured Document', yaml.friendly)
578
+ assert_equal('YAML Structured Document', yaml.friendly('en'))
579
+ assert_equal(nil, yaml.friendly('fr'))
580
+ end
581
+
582
+ def test_friendly_set
583
+ assert_equal({ 'en' => 'Zip' }, @applzip.friendly([ 'en', 'Zip' ]))
584
+ assert_equal({ 'en' => 'Zip Archive' }, @applzip.friendly('en' => 'Zip Archive'))
585
+ end
586
+
587
+ def test_i18n_key
588
+ assert_equal('text.plain', make('text/plain').i18n_key)
589
+ assert_equal('application.vnd-3gpp-bsf-xml',
590
+ make('application/vnd.3gpp.bsf+xml').i18n_key)
591
+ assert_equal('application.msword', make('application/x-msword').i18n_key)
592
+ end
593
+
510
594
  def test_deprecated_constant
511
595
  assert_output(nil, /MIME::InvalidContentType/) do
512
596
  assert_same(MIME::InvalidContentType, MIME::Type::InvalidContentType)
@@ -514,5 +598,6 @@ class TestMIMEType < Minitest::Test
514
598
  assert_silent do
515
599
  assert_same(MIME::InvalidContentType, MIME::Type::InvalidContentType)
516
600
  end
601
+ assert_raises(NameError) { MIME::Foo }
517
602
  end
518
603
  end
@@ -16,6 +16,8 @@ class TestMIMETypes < Minitest::Test
16
16
 
17
17
  def test_enumerable
18
18
  assert(@mime_types.any? {|type| type.content_type == 'text/plain'})
19
+ assert_kind_of(Enumerator, @mime_types.each)
20
+ assert_equal(6, @mime_types.each.count)
19
21
  end
20
22
 
21
23
  def test_index_with_mime_type
@@ -10,6 +10,8 @@ class TestMIMETypesQueryClassMethods < Minitest::Test
10
10
 
11
11
  def test_enumerable
12
12
  assert(MIME::Types.any? {|type| type.content_type == 'text/plain'})
13
+ assert_kind_of(Enumerator, MIME::Types.each)
14
+ assert(MIME::Types.each.count > 999)
13
15
  end
14
16
 
15
17
  def test_load_from_file
@@ -5,8 +5,9 @@ require 'minitest_helper'
5
5
 
6
6
  class TestMIMETypesLoader < Minitest::Test
7
7
  def setup
8
- @path = File.expand_path('../fixture', __FILE__)
9
- @loader = MIME::Types::Loader.new(@path)
8
+ @path = File.expand_path('../fixture', __FILE__)
9
+ @loader = MIME::Types::Loader.new(@path)
10
+ @bad_path = File.expand_path('../bad-fixtures', __FILE__)
10
11
  end
11
12
 
12
13
  def assert_correctly_loaded(types)
@@ -39,4 +40,12 @@ class TestMIMETypesLoader < Minitest::Test
39
40
  def test_load_v1
40
41
  assert_correctly_loaded(@loader.load_v1)
41
42
  end
43
+
44
+ def test_malformed_v1
45
+ assert_output(nil, /1: Parsing error in v1 MIME type definition/) {
46
+ assert_raises(MIME::Types::Loader::BadV1Format) {
47
+ MIME::Types::Loader.load_from_v1(File.join(@bad_path, 'malformed'))
48
+ }
49
+ }
50
+ end
42
51
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mime-types
3
3
  version: !ruby/object:Gem::Version
4
- version: '2.3'
4
+ version: 2.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Austin Ziegler
@@ -36,7 +36,7 @@ cert_chain:
36
36
  SHkzay9FVEZoaSs3cElVV2xGbzBpbXJkeUxoZCtKdzNib1ZqM0NtdnloY3dt
37
37
  cG9NMEs5bApBT21yVWlFbFVxTE9aQT09Ci0tLS0tRU5EIENFUlRJRklDQVRF
38
38
  LS0tLS0K
39
- date: 2014-05-23 00:00:00.000000000 Z
39
+ date: 2014-10-07 00:00:00.000000000 Z
40
40
  dependencies:
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: minitest
@@ -192,64 +192,35 @@ dependencies:
192
192
  - - ~>
193
193
  - !ruby/object:Gem::Version
194
194
  version: '3.12'
195
- description: ! 'The mime-types library provides a library and registry for information
196
- about
197
-
198
- MIME content type definitions. It can be used to determine defined filename
199
-
200
- extensions for MIME types, or to use filename extensions to look up the likely
201
-
202
- MIME type definitions.
203
-
204
-
205
- MIME content types are used in MIME-compliant communications, as in e-mail or
206
-
207
- HTTP traffic, to indicate the type of content which is transmitted. The
208
-
209
- mime-types library provides the ability for detailed information about MIME
210
-
211
- entities (provided as an enumerable collection of MIME::Type objects) to be
212
-
213
- determined and used programmatically. There are many types defined by RFCs and
214
-
215
- vendors, so the list is long but by definition incomplete; don''t hesitate to to
216
-
217
- add additional type definitions (see Contributing.rdoc). The primary sources
218
-
219
- for MIME type definitions found in mime-types is the IANA collection of
220
-
221
- registrations (see below for the link), RFCs, and W3C recommendations.
222
-
223
-
224
- This is release 2.2, mostly changing how the MIME type registry is updated from
225
-
226
- the IANA registry (the format of which was incompatibly changed shortly before
227
-
228
- this release) and taking advantage of the extra data available from IANA
229
-
230
- registry in the form of MIME::Type#xrefs. In addition, the {LTSW
231
-
232
- list}[http://www.ltsw.se/knbase/internet/mime.htp] has been dropped as a
233
-
234
- supported list.
235
-
236
-
237
- As a reminder, mime-types 2.x is no longer compatible with Ruby 1.8 and
238
-
239
- mime-types 1.x is only being maintained for security issues. No new MIME types
240
-
241
- or features will be added.
242
-
243
-
244
- mime-types (previously called MIME::Types for Ruby) was originally based on
245
-
246
- MIME::Types for Perl by Mark Overmeer, copyright 2001 - 2009. It is built to
247
-
248
- conform to the MIME types of RFCs 2045 and 2231. It tracks the {IANA Media
249
-
250
- Types registry}[https://www.iana.org/assignments/media-types/media-types.xhtml]
251
-
252
- with some types added by the users of mime-types.'
195
+ description: ! "The mime-types library provides a library and registry for information
196
+ about\nMIME content type definitions. It can be used to determine defined filename\nextensions
197
+ for MIME types, or to use filename extensions to look up the likely\nMIME type definitions.\n\nMIME
198
+ content types are used in MIME-compliant communications, as in e-mail or\nHTTP traffic,
199
+ to indicate the type of content which is transmitted. The\nmime-types library provides
200
+ the ability for detailed information about MIME\nentities (provided as an enumerable
201
+ collection of MIME::Type objects) to be\ndetermined and used programmatically. There
202
+ are many types defined by RFCs and\nvendors, so the list is long but by definition
203
+ incomplete; don't hesitate to to\nadd additional type definitions (see Contributing.rdoc).
204
+ The primary sources\nfor MIME type definitions found in mime-types is the IANA collection
205
+ of\nregistrations (see below for the link), RFCs, and W3C recommendations.\n\nThis
206
+ is release 2.4.1 (2.4 was pulled), fixing a bug in observed use of the\nmime-types
207
+ library where extensions were not previously sorted, such that\n\n MIME::Types.of('image.jpg').first.extensions.first\n\nreturned
208
+ a value of +jpeg+ in mime-types 1, but +jpe+ in mime-types 2. This was\nintroduced
209
+ because extensions were sorted during assignment\n(MIME::Type#extensions=). This
210
+ behaviour has been reverted to protect clients\nthat work as noted above. The preferred
211
+ way to express this is the new method:\n\n MIME::Type.of('image.jpg').first.preferred_extension\n\nŁukasz
212
+ Śliwa created the\n{friendly_mime}[https://github.com/lukaszsliwa/friendly_mime]
213
+ gem, which offers\nfriendly descriptive names for MIME types. This functionality
214
+ and\nEnglish-language data has been added to mime-types as MIME::Type#friendly.
215
+ To\nmake it easy for internationalization, MIME::Type#i18n_key has been added,\nwhich
216
+ will return a key suitable for use with the\n{I18n}[https://github.com/svenfuchs/i18n]
217
+ library.\n\nAs a reminder, mime-types 2.x is no longer compatible with Ruby 1.8
218
+ and\nmime-types 1.x is only being maintained for security issues. No new MIME types\nor
219
+ features will be added.\n\nmime-types (previously called MIME::Types for Ruby) was
220
+ originally based on\nMIME::Types for Perl by Mark Overmeer, copyright 2001 - 2009.
221
+ It is built to\nconform to the MIME types of RFCs 2045 and 2231. It tracks the {IANA
222
+ Media\nTypes registry}[https://www.iana.org/assignments/media-types/media-types.xhtml]\nwith
223
+ some types added by the users of mime-types."
253
224
  email:
254
225
  - halostatue@gmail.com
255
226
  executables: []
@@ -289,9 +260,10 @@ files:
289
260
  - lib/mime/types/loader.rb
290
261
  - lib/mime/types/loader_path.rb
291
262
  - support/apache_mime_types.rb
292
- - support/benchmarker.rb
263
+ - support/benchmarks/load.rb
293
264
  - support/convert.rb
294
265
  - support/iana_registry.rb
266
+ - test/bad-fixtures/malformed
295
267
  - test/fixture/json.json
296
268
  - test/fixture/old-data
297
269
  - test/fixture/yaml.yaml
@@ -326,7 +298,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
326
298
  version: '0'
327
299
  requirements: []
328
300
  rubyforge_project:
329
- rubygems_version: 2.2.1
301
+ rubygems_version: 2.4.2
330
302
  signing_key:
331
303
  specification_version: 4
332
304
  summary: The mime-types library provides a library and registry for information about