mime-types 2.99.3 → 3.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +5 -5
  2. data/{Code-of-Conduct.rdoc → Code-of-Conduct.md} +19 -20
  3. data/Contributing.md +139 -0
  4. data/History.md +208 -0
  5. data/{Licence.rdoc → Licence.md} +4 -18
  6. data/Manifest.txt +8 -25
  7. data/README.rdoc +63 -67
  8. data/Rakefile +144 -55
  9. data/lib/mime/type/columnar.rb +29 -62
  10. data/lib/mime/type.rb +192 -417
  11. data/lib/mime/types/_columnar.rb +137 -0
  12. data/lib/mime/types/cache.rb +51 -73
  13. data/lib/mime/types/columnar.rb +2 -147
  14. data/lib/mime/types/container.rb +94 -0
  15. data/lib/mime/types/deprecations.rb +5 -24
  16. data/lib/mime/types/full.rb +19 -0
  17. data/lib/mime/types/loader.rb +12 -141
  18. data/lib/mime/types/logger.rb +4 -0
  19. data/lib/mime/types/registry.rb +90 -0
  20. data/lib/mime/types.rb +43 -139
  21. data/lib/mime-types.rb +1 -1
  22. data/test/minitest_helper.rb +6 -12
  23. data/test/test_mime_type.rb +473 -455
  24. data/test/test_mime_types.rb +136 -86
  25. data/test/test_mime_types_cache.rb +83 -53
  26. data/test/test_mime_types_class.rb +119 -97
  27. data/test/test_mime_types_lazy.rb +27 -23
  28. data/test/test_mime_types_loader.rb +7 -32
  29. metadata +102 -62
  30. data/Contributing.rdoc +0 -170
  31. data/History-Types.rdoc +0 -454
  32. data/History.rdoc +0 -590
  33. data/data/mime-types.json +0 -1
  34. data/data/mime.content_type.column +0 -1980
  35. data/data/mime.docs.column +0 -1980
  36. data/data/mime.encoding.column +0 -1980
  37. data/data/mime.friendly.column +0 -1980
  38. data/data/mime.obsolete.column +0 -1980
  39. data/data/mime.registered.column +0 -1980
  40. data/data/mime.signature.column +0 -1980
  41. data/data/mime.use_instead.column +0 -1980
  42. data/data/mime.xrefs.column +0 -1980
  43. data/docs/COPYING.txt +0 -339
  44. data/docs/artistic.txt +0 -127
  45. data/lib/mime/types/loader_path.rb +0 -15
  46. data/support/apache_mime_types.rb +0 -108
  47. data/support/benchmarks/load.rb +0 -64
  48. data/support/benchmarks/load_allocations.rb +0 -83
  49. data/support/benchmarks/object_counts.rb +0 -41
  50. data/support/convert/columnar.rb +0 -88
  51. data/support/convert.rb +0 -158
  52. data/support/iana_registry.rb +0 -172
@@ -1,83 +0,0 @@
1
- # -*- ruby encoding: utf-8 -*-
2
-
3
- if RUBY_VERSION < '2.1'
4
- $stderr.puts "Cannot count allocations on #{RUBY_VERSION}."
5
- exit 1
6
- end
7
-
8
- begin
9
- require 'allocation_tracer'
10
- rescue LoadError
11
- $stderr.puts "Allocation tracking requires the gem 'allocation_tracer'."
12
- exit 1
13
- end
14
-
15
- module Benchmarks
16
- class LoadAllocations
17
- def self.report(columnar: false, top_x: nil, mime_types_only: false)
18
- new(columnar: columnar, top_x: top_x, mime_types_only: mime_types_only).
19
- report
20
- end
21
-
22
- def initialize(columnar: false, top_x: nil, mime_types_only: false)
23
- @columnar = columnar
24
- @mime_types_only = !!mime_types_only
25
-
26
- @top_x = top_x
27
-
28
- return unless @top_x
29
- @top_x = top_x.to_i
30
- @top_x = 10 if @top_x <= 0
31
- end
32
-
33
- def report
34
- collect
35
- report_top_x if @top_x
36
- puts "TOTAL Allocations: #{@count}"
37
- end
38
-
39
- private
40
-
41
- def report_top_x
42
- table = @allocations.sort_by { |_, v| v.first }.reverse.first(@top_x)
43
- table.map! { |(location, allocs)|
44
- next if @mime_types_only and location.first !~ %r{mime-types/lib}
45
- [ location.join(':').gsub(%r{^#{Dir.pwd}/}, ''), *allocs ]
46
- }.compact!
47
-
48
- head = (ObjectSpace::AllocationTracer.header - [ :line ]).map {|h|
49
- h.to_s.split(/_/).map(&:capitalize).join(' ')
50
- }
51
- table.unshift head
52
-
53
- max_widths = [].tap do |mw|
54
- table.map { |row| row.lazy.map(&:to_s).map(&:length).to_a }.tap do |w|
55
- w.first.each_index do |i|
56
- mw << w.lazy.map { |r| r[i] }.max
57
- end
58
- end
59
- end
60
-
61
- pattern = [ '%%-%ds' ]
62
- pattern << ([ '%% %ds' ] * (max_widths.length - 1))
63
- pattern = pattern.join("\t") % max_widths
64
- table.each { |row| puts pattern % row }
65
- puts
66
- end
67
-
68
- def collect
69
- if @columnar
70
- @allocations = ObjectSpace::AllocationTracer.trace do
71
- require 'mime/types/columnar'
72
- end
73
- else
74
- @allocations = ObjectSpace::AllocationTracer.trace do
75
- require 'mime/types'
76
- end
77
- end
78
-
79
- @count = ObjectSpace::AllocationTracer.allocated_count_table.values.
80
- inject(:+)
81
- end
82
- end
83
- end
@@ -1,41 +0,0 @@
1
- # -*- ruby encoding: utf-8 -*-
2
-
3
- module Benchmarks
4
- class ObjectCounts
5
- def self.report(columnar: false)
6
- new(columnar: columnar).report
7
- end
8
-
9
- def initialize(columnar: false)
10
- @columnar = columnar
11
- end
12
-
13
- def report
14
- collect
15
- @before.keys.grep(/T_/).map { |key|
16
- [ key, @after[key] - @before[key] ]
17
- }.sort_by { |_, delta| -delta }.each { |key, delta|
18
- puts '%10s +%6d' % [ key, delta ]
19
- }
20
- end
21
-
22
- private
23
-
24
- def collect
25
- @before = count_objects
26
-
27
- if @columnar
28
- require 'mime/types/columnar'
29
- else
30
- require 'mime/types'
31
- end
32
-
33
- @after = count_objects
34
- end
35
-
36
- def count_objects
37
- GC.start
38
- ObjectSpace.count_objects
39
- end
40
- end
41
- end
@@ -1,88 +0,0 @@
1
- require 'convert'
2
-
3
- class Convert::Columnar < Convert
4
- class << self
5
- # Converts from YAML to Columnar format. This *always* converts to multiple
6
- # files.
7
- def from_yaml_to_columnar(args)
8
- from_yaml(yaml_path(args.source)).
9
- to_columnar(destination: columnar_path(args.destination))
10
- end
11
-
12
- private
13
-
14
- def columnar_path(path)
15
- path_or_default(path, 'data')
16
- end
17
- end
18
-
19
- # Convert the data to multiple text files.
20
- def to_columnar(options = {})
21
- root = options[:destination] or require_destination!
22
- @root = must_be_directory!(root)
23
- @data = @loader.container.sort.map(&:to_h)
24
-
25
- column_file('content_type') do |type|
26
- [ type['content-type'], Array(type['extensions']).join(' ') ].
27
- flatten.join(' ').strip
28
- end
29
-
30
- required_file('encoding')
31
- optional_file('docs')
32
- bool_file('obsolete')
33
- bool_file('registered')
34
- bool_file('signature')
35
- dict_file('xrefs')
36
- dict_file('friendly')
37
- optional_file('use_instead', 'use-instead')
38
- end
39
-
40
- def column_file(name, &block)
41
- File.open(File.join(@root, "mime.#{name}.column"), 'wb') do |f|
42
- f.puts @data.map(&block)
43
- end
44
- end
45
-
46
- def bool_file(name, *fields)
47
- fields = [ name ] if fields.empty?
48
- column_file(name) do |type|
49
- fields.map { |field|
50
- type[field] ? 1 : 0
51
- }.join(' ')
52
- end
53
- end
54
-
55
- def required_file(name, field = name)
56
- column_file(name) { |type| type[field] }
57
- end
58
-
59
- def optional_file(name, field = name)
60
- column_file(name) { |type| opt(type[field]) }
61
- end
62
-
63
- def array_file(name, field = name)
64
- column_file(name) { |type| arr(type[field]) }
65
- end
66
-
67
- def dict_file(name, field = name)
68
- column_file(name) { |type| dict(type[field]) }
69
- end
70
-
71
- def opt(value)
72
- value || '-'
73
- end
74
-
75
- def arr(value)
76
- Array(opt(value)).join('|')
77
- end
78
-
79
- def dict(value)
80
- if value
81
- value.sort.map { |k, v|
82
- [ k, Array(v).compact.join('^') ].join('^')
83
- }.join('|')
84
- else
85
- '-'
86
- end
87
- end
88
- end
data/support/convert.rb DELETED
@@ -1,158 +0,0 @@
1
- # -*- ruby encoding: utf-8 -*-
2
-
3
- $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
4
- ENV['RUBY_MIME_TYPES_LAZY_LOAD'] = 'true'
5
- require 'mime/types'
6
- require 'fileutils'
7
- require 'json'
8
-
9
- class MIME::Types
10
- def self.deprecated(*_args, &_block)
11
- # We are an internal tool. Silence deprecation warnings.
12
- end
13
- end
14
-
15
- class Convert
16
- class << self
17
- # Create a Convert instance that converts from YAML.
18
- def from_yaml(path = nil)
19
- new(path: path, from: :yaml)
20
- end
21
-
22
- # Create a Convert instance that converts from JSON.
23
- def from_json(path = nil)
24
- new(path: path, from: :json)
25
- end
26
-
27
- # Create a Convert instance that converts from the mime-types 1.x file
28
- # format.
29
- def from_v1(path = nil)
30
- new(path: path, from: :v1)
31
- end
32
-
33
- # Converts from YAML to JSON. Defaults to converting to a single file.
34
- def from_yaml_to_json(args)
35
- from_yaml(yaml_path(args.source)).
36
- to_json(
37
- destination: json_path(args.destination),
38
- multiple_files: multiple_files(args.multiple_files || 'single')
39
- )
40
- end
41
-
42
- # Converts from JSON to YAML. Defaults to converting to multiple files.
43
- def from_json_to_yaml(args)
44
- from_json(json_path(args.source)).
45
- to_yaml(
46
- destination: yaml_path(args.destination),
47
- multiple_files: multiple_files(args.multiple_files || 'multiple')
48
- )
49
- end
50
-
51
- private :new
52
-
53
- private
54
-
55
- def yaml_path(path)
56
- path_or_default(path, 'type-lists'.freeze)
57
- end
58
-
59
- def json_path(path)
60
- path_or_default(path, 'data'.freeze)
61
- end
62
-
63
- def path_or_default(path, default)
64
- if path.nil? or path.empty?
65
- default
66
- else
67
- path
68
- end
69
- end
70
-
71
- def multiple_files(flag)
72
- case flag.to_s.downcase
73
- when 'true', 'yes', 'multiple'
74
- true
75
- else
76
- false
77
- end
78
- end
79
- end
80
-
81
- def initialize(options = {})
82
- if options[:path].nil? or options[:path].empty?
83
- fail ArgumentError, ':path is required'
84
- elsif options[:from].nil? or options[:from].empty?
85
- fail ArgumentError, ':from is required'
86
- end
87
-
88
- @loader = MIME::Types::Loader.new(options[:path])
89
- load_from(options[:from])
90
- end
91
-
92
- # Convert the data to JSON.
93
- def to_json(options = {})
94
- options[:destination] or require_destination!
95
- write_types(options.merge(format: :json))
96
- end
97
-
98
- # Convert the data to YAML.
99
- def to_yaml(options = {})
100
- options[:destination] or require_destination!
101
- write_types(options.merge(format: :yaml))
102
- end
103
-
104
- private
105
-
106
- def load_from(source_type)
107
- method = :"load_#{source_type}"
108
- @loader.send(method)
109
- end
110
-
111
- def write_types(options)
112
- if options[:multiple_files]
113
- write_multiple_files(options)
114
- else
115
- write_one_file(options)
116
- end
117
- end
118
-
119
- def write_one_file(options)
120
- d = options[:destination]
121
- d = File.join(d, "mime-types.#{options[:format]}") if File.directory?(d)
122
-
123
- File.open(d, 'wb') { |f|
124
- f.puts convert(@loader.container.map.sort, options[:format])
125
- }
126
- end
127
-
128
- def write_multiple_files(options)
129
- d = options[:destination]
130
- must_be_directory!(d)
131
-
132
- media_types = MIME::Types.map(&:media_type).uniq
133
- media_types.each { |media_type|
134
- n = File.join(d, "#{media_type}.#{options[:format]}")
135
- t = @loader.container.select { |e| e.media_type == media_type }
136
- File.open(n, 'wb') { |f|
137
- f.puts convert(t.sort, options[:format])
138
- }
139
- }
140
- end
141
-
142
- def convert(data, format)
143
- data.send(:"to_#{format}")
144
- end
145
-
146
- def require_destination!
147
- fail ArgumentError, 'Destination path is required.'
148
- end
149
-
150
- def must_be_directory!(path)
151
- if File.exist?(path) and !File.directory?(path)
152
- fail ArgumentError, 'Cannot write multiple files to a file.'
153
- end
154
-
155
- FileUtils.mkdir_p(path) unless File.exist?(path)
156
- path
157
- end
158
- end
@@ -1,172 +0,0 @@
1
- # -*- ruby encoding: utf-8 -*-
2
-
3
- $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
4
-
5
- require 'open-uri'
6
- require 'nokogiri'
7
- require 'cgi'
8
- require 'pathname'
9
- require 'yaml'
10
-
11
- ENV['RUBY_MIME_TYPES_LAZY_LOAD'] = 'yes'
12
- require 'mime/types'
13
-
14
- class MIME::Types
15
- def self.deprecated(*_args, &_block)
16
- # We are an internal tool. Silence deprecation warnings.
17
- end
18
- end
19
-
20
- class MIME::Type
21
- public_constant :MEDIA_TYPE_RE if respond_to? :public_constant
22
- end
23
-
24
- class IANARegistry
25
- DEFAULTS = {
26
- url: %q(https://www.iana.org/assignments/media-types/media-types.xml),
27
- to: Pathname(__FILE__).join('../../type-lists')
28
- }.freeze.each_value(&:freeze)
29
-
30
- def self.download(options = {})
31
- dest = Pathname(options[:to] || DEFAULTS[:to]).expand_path
32
- url = options.fetch(:url, DEFAULTS[:url])
33
-
34
- puts 'Downloading IANA MIME type assignments.'
35
- puts "\t#{url}"
36
- xml = Nokogiri::XML(open(url) { |f| f.read })
37
-
38
- xml.css('registry registry').each do |registry|
39
- next if registry.at_css('title').text == 'example'
40
- new(registry: registry, to: dest) do |parser|
41
- puts "Extracting #{parser.type}/*."
42
- parser.parse
43
- parser.save
44
- end
45
- end
46
- end
47
-
48
- attr_reader :type
49
-
50
- def initialize(options = {})
51
- @registry = options.fetch(:registry)
52
- @to = Pathname(options.fetch(:to)).expand_path
53
- @type = @registry.at_css('title').text
54
- @name = "#{@type}.yaml"
55
- @file = @to.join(@name)
56
- @types = mime_types_for(@file)
57
-
58
- yield self if block_given?
59
- end
60
-
61
- ASSIGNMENT_FILE_REF = '{%s=http://www.iana.org/assignments/media-types/%s}'
62
-
63
- def parse
64
- @registry.css('record').each do |record|
65
- subtype = record.at_css('name').text
66
- obsolete = record.at_css('obsolete').text rescue nil
67
- use_instead = record.at_css('deprecated').text rescue nil
68
-
69
- if subtype =~ /OBSOLETE|DEPRECATE/i
70
- use_instead ||= $1 if subtype =~ /in favou?r of (#{MIME::Type::MEDIA_TYPE_RE})/
71
- obsolete = true
72
- end
73
-
74
- subtype, notes = subtype.split(/ /, 2)
75
-
76
- refs, xrefs = parse_refs_and_files(
77
- record.css('xref'),
78
- record.css('file'),
79
- subtype
80
- )
81
-
82
- xrefs['notes'] << notes if notes
83
-
84
- content_type = [ @type, subtype ].join('/')
85
-
86
- types = @types.select { |t|
87
- (t.content_type.downcase == content_type.downcase)
88
- }
89
-
90
- if types.empty?
91
- MIME::Type.new(content_type) do |mt|
92
- mt.xrefs = xrefs
93
- mt.registered = true
94
- mt.obsolete = obsolete if obsolete
95
- mt.use_instead = use_instead if use_instead
96
- @types << mt
97
- end
98
- else
99
- types.each { |mt|
100
- mt.registered = true
101
- mt.xrefs = xrefs
102
- mt.obsolete = obsolete if obsolete
103
- mt.use_instead = use_instead if use_instead
104
- }
105
- end
106
- end
107
- end
108
-
109
- def save
110
- @to.mkpath
111
- File.open(@file, 'wb') { |f| f.puts @types.map.to_a.sort.uniq.to_yaml }
112
- end
113
-
114
- private
115
-
116
- def mime_types_for(file)
117
- if file.exist?
118
- MIME::Types::Loader.load_from_yaml(file)
119
- else
120
- MIME::Types.new
121
- end
122
- end
123
-
124
- def parse_refs_and_files(refs, files, subtype)
125
- xr = MIME::Types::Container.new
126
- r = []
127
-
128
- refs.each do |xref|
129
- type = xref['type']
130
- data = xref['data']
131
-
132
- next if data.nil? || data.empty?
133
-
134
- r << ref_from_type(type, data)
135
-
136
- xr[type] << data
137
- end
138
-
139
- files.each do |file|
140
- file_name = if file.text == subtype
141
- [ @type, subtype ].join('/')
142
- else
143
- file.text
144
- end
145
-
146
- if file['type'] == 'template'
147
- r << (ASSIGNMENT_FILE_REF % [ file_name, file_name ])
148
- end
149
-
150
- xr[file['type']] << file_name
151
- end
152
-
153
- [ r, xr ]
154
- end
155
-
156
- def ref_from_type(type, data)
157
- case type
158
- when 'person'
159
- "[#{data}]"
160
- when 'rfc'
161
- data.upcase
162
- when 'draft'
163
- "DRAFT:#{data.sub(/^RFC-/, 'draft-')}"
164
- when 'rfc-errata'
165
- "{RFC Errata #{data}=http://www.rfc-editor.org/errata_search.php?eid=#{data}}"
166
- when 'uri'
167
- "{#{data}}"
168
- else # 'text' or something else
169
- data
170
- end
171
- end
172
- end