mime-types 2.99.3 → 3.3.1

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 +143 -0
  4. data/History.md +240 -0
  5. data/{Licence.rdoc → Licence.md} +4 -18
  6. data/Manifest.txt +8 -25
  7. data/README.rdoc +62 -73
  8. data/Rakefile +175 -58
  9. data/lib/mime-types.rb +1 -1
  10. data/lib/mime/type.rb +213 -424
  11. data/lib/mime/type/columnar.rb +29 -62
  12. data/lib/mime/types.rb +46 -141
  13. data/lib/mime/types/_columnar.rb +136 -0
  14. data/lib/mime/types/cache.rb +51 -73
  15. data/lib/mime/types/columnar.rb +2 -147
  16. data/lib/mime/types/container.rb +96 -0
  17. data/lib/mime/types/deprecations.rb +4 -25
  18. data/lib/mime/types/full.rb +19 -0
  19. data/lib/mime/types/loader.rb +12 -141
  20. data/lib/mime/types/logger.rb +5 -1
  21. data/lib/mime/types/registry.rb +90 -0
  22. data/test/minitest_helper.rb +5 -13
  23. data/test/test_mime_type.rb +470 -456
  24. data/test/test_mime_types.rb +135 -87
  25. data/test/test_mime_types_cache.rb +82 -54
  26. data/test/test_mime_types_class.rb +118 -98
  27. data/test/test_mime_types_lazy.rb +26 -24
  28. data/test/test_mime_types_loader.rb +6 -33
  29. metadata +107 -64
  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.rb +0 -158
  51. data/support/convert/columnar.rb +0 -88
  52. data/support/iana_registry.rb +0 -172
@@ -1,15 +0,0 @@
1
- # -*- ruby encoding: utf-8 -*-
2
-
3
- class MIME::Types::Loader
4
- # The path that will be used for loading the MIME::Types data. The default
5
- # location is __FILE__/../../../../data, which is where the data lives
6
- # in the gem installation of the mime-types library.
7
- #
8
- # The MIME::Types::Loader will load all JSON or columnar files contained in
9
- # this path.
10
- #
11
- # System repackagers note: this is the constant that you would change if
12
- # you repackage mime-types for your system. It is recommended that the
13
- # path be something like /usr/share/ruby/mime-types/.
14
- PATH = File.expand_path('../../../../data', __FILE__)
15
- end
@@ -1,108 +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::Type
15
- public_constant :UNREGISTERED_RE
16
- end
17
-
18
- class MIME::Types
19
- def self.deprecated(*_args, &_block)
20
- # We are an internal tool. Silence deprecation warnings.
21
- end
22
- end
23
-
24
- class ApacheMIMETypes
25
- DEFAULTS = {
26
- url: %q(http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types),
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 Apache MIME type list.'
35
- puts "\t#{url}"
36
- data = open(url) { |f| f.read }.split($/)
37
- data.delete_if { |line| line =~ /\A#/ }
38
-
39
- conf = MIME::Types::Container.new
40
-
41
- data.each do |line|
42
- type = line.split(/\t+/)
43
- key = type.first.split(%r{/}).first.gsub(MIME::Type::UNREGISTERED_RE, '')
44
- conf[key] << type
45
- end
46
-
47
- conf.each do |type, types|
48
- next if type == 'example'
49
-
50
- new(type: type, registry: types, to: dest) do |parser|
51
- puts "Extracting #{parser.type}/*."
52
- parser.parse
53
- parser.save
54
- end
55
- end
56
- end
57
-
58
- attr_reader :type
59
-
60
- def initialize(options = {})
61
- @registry = options.fetch(:registry)
62
- @to = Pathname(options.fetch(:to)).expand_path
63
- @type = options.fetch(:type)
64
- @name = "#{@type}.yaml"
65
- @file = @to.join(@name)
66
- @types = mime_types_for(@file)
67
-
68
- yield self if block_given?
69
- end
70
-
71
- def parse
72
- @registry.each do |record|
73
- content_type = record.first
74
- extensions = record.last.split(/\s+/)
75
-
76
- types = @types.select { |t|
77
- (t.content_type.downcase == content_type.downcase)
78
- }
79
-
80
- if types.empty?
81
- MIME::Type.new(content_type) do |mt|
82
- mt.extensions = extensions
83
- mt.registered = false
84
- @types << mt
85
- end
86
- else
87
- types.each { |mt|
88
- mt.extensions = (mt.extensions + extensions)
89
- }
90
- end
91
- end
92
- end
93
-
94
- def save
95
- @to.mkpath
96
- File.open(@file, 'wb') { |f| f.puts @types.map.to_a.sort.to_yaml }
97
- end
98
-
99
- private
100
-
101
- def mime_types_for(file)
102
- if file.exist?
103
- MIME::Types::Loader.load_from_yaml(file)
104
- else
105
- MIME::Types.new
106
- end
107
- end
108
- end
@@ -1,64 +0,0 @@
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, options = {})
19
- force_load = options.fetch(:force_load, false)
20
- columnar = options.fetch(:columnar, false)
21
-
22
- repeats.times {
23
- Object.send(:remove_const, :MIME) if defined? ::MIME
24
- $LOADED_FEATURES.delete_if { |n| n =~ /#{@load_path}/ }
25
-
26
- if columnar
27
- require 'mime/types/columnar'
28
- else
29
- require 'mime/types'
30
- end
31
- ::MIME::Types.send(:__types__) if force_load
32
- }
33
- end
34
-
35
- def report
36
- remove_cache
37
-
38
- Benchmark.bm(17) do |mark|
39
- mark.report('Normal:') { reload_mime_types(@repeats) }
40
- mark.report('Columnar:') { reload_mime_types(@repeats, columnar: true) }
41
-
42
- ENV['RUBY_MIME_TYPES_LAZY_LOAD'] = 'yes'
43
- mark.report('Lazy:') { reload_mime_types(@repeats) }
44
- mark.report('Lazy+Load:') { reload_mime_types(@repeats, force_load: true) }
45
-
46
- ENV.delete('RUBY_MIME_TYPES_LAZY_LOAD')
47
-
48
- ENV['RUBY_MIME_TYPES_CACHE'] = @cache_file
49
- reload_mime_types
50
-
51
- mark.report('Cached:') { reload_mime_types(@repeats) }
52
- ENV['RUBY_MIME_TYPES_LAZY_LOAD'] = 'yes'
53
- mark.report('Lazy Cached:') { reload_mime_types(@repeats) }
54
- mark.report('Lazy Cached Load:') { reload_mime_types(@repeats, force_load: true) }
55
- end
56
- ensure
57
- remove_cache
58
- end
59
-
60
- def remove_cache
61
- File.unlink(@cache_file) if File.exist?(@cache_file)
62
- end
63
- end
64
- end
@@ -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,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