mime-types 3.3.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.
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mime/types'
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'set'
4
+ require 'forwardable'
5
+
6
+ # MIME::Types requires a serializable keyed container that returns an empty Set
7
+ # on a key miss. Hash#default_value cannot be used because, while it traverses
8
+ # the Marshal format correctly, it won't survive any other serialization
9
+ # format (plus, a default of a mutable object resuls in a shared mess).
10
+ # Hash#default_proc cannot be used without a wrapper because it prevents
11
+ # Marshal serialization (and doesn't survive the round-trip).
12
+ class MIME::Types::Container #:nodoc:
13
+ extend Forwardable
14
+
15
+ def initialize(hash = {})
16
+ @container = {}
17
+ merge!(hash)
18
+ end
19
+
20
+ def [](key)
21
+ container[key] || EMPTY_SET
22
+ end
23
+
24
+ def []=(key, value)
25
+ container[key] =
26
+ case value
27
+ when Set
28
+ value
29
+ else
30
+ Set[*value]
31
+ end
32
+ end
33
+
34
+ def merge(other)
35
+ self.class.new(other)
36
+ end
37
+
38
+ def merge!(other)
39
+ tap {
40
+ other = other.kind_of?(MIME::Types::Container) ? other.container : other
41
+ container.merge!(other)
42
+ normalize
43
+ }
44
+ end
45
+
46
+ def to_hash
47
+ container
48
+ end
49
+
50
+ def_delegators :@container,
51
+ :==,
52
+ :count,
53
+ :each,
54
+ :each_value,
55
+ :empty?,
56
+ :flat_map,
57
+ :keys,
58
+ :select,
59
+ :values
60
+
61
+ def add(key, value)
62
+ (container[key] ||= Set.new).add(value)
63
+ end
64
+
65
+ def marshal_dump
66
+ {}.merge(container)
67
+ end
68
+
69
+ def marshal_load(hash)
70
+ @container = hash
71
+ end
72
+
73
+ def encode_with(coder)
74
+ container.each { |k, v| coder[k] = v.to_a }
75
+ end
76
+
77
+ def init_with(coder)
78
+ @container = {}
79
+ coder.map.each { |k, v| container[k] = Set[*v] }
80
+ end
81
+
82
+ protected
83
+
84
+ attr_accessor :container
85
+
86
+ def normalize
87
+ container.each do |k, v|
88
+ next if v.kind_of?(Set)
89
+
90
+ container[k] = Set[*v]
91
+ end
92
+ end
93
+
94
+ EMPTY_SET = Set.new.freeze
95
+ private_constant :EMPTY_SET
96
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mime/types/logger'
4
+
5
+ # The namespace for MIME applications, tools, and libraries.
6
+ module MIME
7
+ ##
8
+ class Types
9
+ # Used to mark a method as deprecated in the mime-types interface.
10
+ def self.deprecated(klass, sym, message = nil, &block) # :nodoc:
11
+ level = case klass
12
+ when Class, Module
13
+ '.'
14
+ else
15
+ klass = klass.class
16
+ '#'
17
+ end
18
+ message = case message
19
+ when :private, :protected
20
+ "and will be #{message}"
21
+ when nil
22
+ 'and will be removed'
23
+ else
24
+ message
25
+ end
26
+ MIME::Types.logger.warn <<-WARNING.chomp
27
+ #{caller[1]}: #{klass}#{level}#{sym} is deprecated #{message}.
28
+ WARNING
29
+ block.call if block
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ ##
4
+ module MIME
5
+ ##
6
+ class Types
7
+ unless private_method_defined?(:load_mode)
8
+ class << self
9
+ private
10
+
11
+ def load_mode
12
+ { columnar: false }
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+ require 'mime/types'
@@ -0,0 +1,146 @@
1
+ # frozen_string_literal: true
2
+
3
+ # -*- ruby encoding: utf-8 -*-
4
+
5
+ ##
6
+ module MIME; end
7
+ ##
8
+ class MIME::Types; end
9
+
10
+ require 'mime/types/data'
11
+
12
+ # This class is responsible for initializing the MIME::Types registry from
13
+ # the data files supplied with the mime-types library.
14
+ #
15
+ # The Loader will use one of the following paths:
16
+ # 1. The +path+ provided in its constructor argument;
17
+ # 2. The value of ENV['RUBY_MIME_TYPES_DATA']; or
18
+ # 3. The value of MIME::Types::Data::PATH.
19
+ #
20
+ # When #load is called, the +path+ will be searched recursively for all YAML
21
+ # (.yml or .yaml) files. By convention, there is one file for each media
22
+ # type (application.yml, audio.yml, etc.), but this is not required.
23
+ class MIME::Types::Loader
24
+ # The path that will be read for the MIME::Types files.
25
+ attr_reader :path
26
+ # The MIME::Types container instance that will be loaded. If not provided
27
+ # at initialization, a new MIME::Types instance will be constructed.
28
+ attr_reader :container
29
+
30
+ # Creates a Loader object that can be used to load MIME::Types registries
31
+ # into memory, using YAML, JSON, or Columnar registry format loaders.
32
+ def initialize(path = nil, container = nil)
33
+ path = path || ENV['RUBY_MIME_TYPES_DATA'] || MIME::Types::Data::PATH
34
+ @container = container || MIME::Types.new
35
+ @path = File.expand_path(path)
36
+ end
37
+
38
+ # Loads a MIME::Types registry from YAML files (<tt>*.yml</tt> or
39
+ # <tt>*.yaml</tt>) recursively found in +path+.
40
+ #
41
+ # It is expected that the YAML objects contained within the registry array
42
+ # will be tagged as <tt>!ruby/object:MIME::Type</tt>.
43
+ #
44
+ # Note that the YAML format is about 2½ times *slower* than the JSON format.
45
+ #
46
+ # NOTE: The purpose of this format is purely for maintenance reasons.
47
+ def load_yaml
48
+ Dir[yaml_path].sort.each do |f|
49
+ container.add(*self.class.load_from_yaml(f), :silent)
50
+ end
51
+ container
52
+ end
53
+
54
+ # Loads a MIME::Types registry from JSON files (<tt>*.json</tt>)
55
+ # recursively found in +path+.
56
+ #
57
+ # It is expected that the JSON objects will be an array of hash objects.
58
+ # The JSON format is the registry format for the MIME types registry
59
+ # shipped with the mime-types library.
60
+ def load_json
61
+ Dir[json_path].sort.each do |f|
62
+ types = self.class.load_from_json(f)
63
+ container.add(*types, :silent)
64
+ end
65
+ container
66
+ end
67
+
68
+ # Loads a MIME::Types registry from columnar files recursively found in
69
+ # +path+.
70
+ def load_columnar
71
+ require 'mime/types/columnar' unless defined?(MIME::Types::Columnar)
72
+ container.extend(MIME::Types::Columnar)
73
+ container.load_base_data(path)
74
+
75
+ container
76
+ end
77
+
78
+ # Loads a MIME::Types registry. Loads from JSON files by default
79
+ # (#load_json).
80
+ #
81
+ # This will load from columnar files (#load_columnar) if <tt>columnar:
82
+ # true</tt> is provided in +options+ and there are columnar files in +path+.
83
+ def load(options = { columnar: false })
84
+ if options[:columnar] && !Dir[columnar_path].empty?
85
+ load_columnar
86
+ else
87
+ load_json
88
+ end
89
+ end
90
+
91
+ class << self
92
+ # Loads the default MIME::Type registry.
93
+ def load(options = { columnar: false })
94
+ new.load(options)
95
+ end
96
+
97
+ # Loads MIME::Types from a single YAML file.
98
+ #
99
+ # It is expected that the YAML objects contained within the registry
100
+ # array will be tagged as <tt>!ruby/object:MIME::Type</tt>.
101
+ #
102
+ # Note that the YAML format is about 2½ times *slower* than the JSON
103
+ # format.
104
+ #
105
+ # NOTE: The purpose of this format is purely for maintenance reasons.
106
+ def load_from_yaml(filename)
107
+ begin
108
+ require 'psych'
109
+ rescue LoadError
110
+ nil
111
+ end
112
+ require 'yaml'
113
+ YAML.load(read_file(filename))
114
+ end
115
+
116
+ # Loads MIME::Types from a single JSON file.
117
+ #
118
+ # It is expected that the JSON objects will be an array of hash objects.
119
+ # The JSON format is the registry format for the MIME types registry
120
+ # shipped with the mime-types library.
121
+ def load_from_json(filename)
122
+ require 'json'
123
+ JSON.parse(read_file(filename)).map { |type| MIME::Type.new(type) }
124
+ end
125
+
126
+ private
127
+
128
+ def read_file(filename)
129
+ File.open(filename, 'r:UTF-8:-', &:read)
130
+ end
131
+ end
132
+
133
+ private
134
+
135
+ def yaml_path
136
+ File.join(path, '*.y{,a}ml')
137
+ end
138
+
139
+ def json_path
140
+ File.join(path, '*.json')
141
+ end
142
+
143
+ def columnar_path
144
+ File.join(path, '*.column')
145
+ end
146
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ # -*- ruby encoding: utf-8 -*-
4
+
5
+ require 'logger'
6
+
7
+ ##
8
+ module MIME
9
+ ##
10
+ class Types
11
+ class << self
12
+ # Configure the MIME::Types logger. This defaults to an instance of a
13
+ # logger that passes messages (unformatted) through to Kernel#warn.
14
+ attr_accessor :logger
15
+ end
16
+
17
+ class WarnLogger < ::Logger #:nodoc:
18
+ class WarnLogDevice < ::Logger::LogDevice #:nodoc:
19
+ def initialize(*)
20
+ end
21
+
22
+ def write(m)
23
+ Kernel.warn(m)
24
+ end
25
+
26
+ def close
27
+ end
28
+ end
29
+
30
+ def initialize(_one, _two = nil, _three = nil)
31
+ super nil
32
+ @logdev = WarnLogDevice.new
33
+ @formatter = ->(_s, _d, _p, m) { m }
34
+ end
35
+ end
36
+
37
+ self.logger = WarnLogger.new(nil)
38
+ end
39
+ end
@@ -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 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
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__) and @__types__) or 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
@@ -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
@@ -0,0 +1 @@
1
+ [{"content-type":"application/smil","encoding":"8bit","extensions":["smi","smil"],"obsolete":true,"use-instead":"application/smil+xml","registered":true},{"content-type":"audio/vnd.qcelp","encoding":"base64","extensions":["qcp"],"obsolete":true,"use-instead":"audio/QCELP","registered":true},{"content-type":"image/bmp","encoding":"base64","extensions":["bmp"],"obsolete":true,"use-instead":"image/x-bmp","registered":false},{"content-type":"application/acad","encoding":"base64","registered":false},{"content-type":"audio/webm","encoding":"base64","extensions":["webm"],"registered":false},{"content-type":"image/pjpeg","docs":"Fixes a bug with IE6 and progressive JPEGs","encoding":"base64","registered":false},{"content-type":"application/1d-interleaved-parityfec","encoding":"base64","registered":true},{"content-type":"audio/1d-interleaved-parityfec","encoding":"base64","registered":true},{"content-type":"application/x-apple-diskimage","encoding":"base64","extensions":["dmg"],"registered":false}]
@@ -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