Class: MIME::Types::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/mime/types/loader.rb,
lib/mime/types/loader_path.rb

Overview

This class is responsible for initializing the MIME::Types registry from the data files supplied with the mime-types library.

The Loader will use one of the following paths:

  1. The path provided in its constructor argument;

  2. The value of ENV; or

  3. The value of MIME::Types::Loader::PATH.

When #load is called, the path will be searched recursively for all YAML (.yml or .yaml) files. By convention, there is one file for each media type (application.yml, audio.yml, etc.), but this is not required.

Constant Summary collapse

BadV1Format =

Raised when a V1 format file is discovered. This exception will be removed for mime-types 3.0.

Class.new(Exception)
PATH =

The path that will be used for loading the MIME::Types data. The default location is __FILE__/../../../../data, which is where the data lives in the gem installation of the mime-types library.

The MIME::Types::Loader will load all JSON or columnar files contained in this path.

System repackagers note: this is the constant that you would change if you repackage mime-types for your system. It is recommended that the path be something like /usr/share/ruby/mime-types/.

File.expand_path('../../../../data', __FILE__)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil, container = nil) ⇒ Loader

Creates a Loader object that can be used to load MIME::Types registries into memory, using YAML, JSON, or v1 registry format loaders.



28
29
30
31
32
33
34
35
36
# File 'lib/mime/types/loader.rb', line 28

def initialize(path = nil, container = nil)
  path = path || ENV['RUBY_MIME_TYPES_DATA'] || MIME::Types::Loader::PATH
  @container = container || MIME::Types.new
  @path = File.expand_path(path)
  # begin
  #   require 'mime/lazy_types'
  #   @container.extend(MIME::LazyTypes)
  # end
end

Instance Attribute Details

#containerObject (readonly)

The MIME::Types container instance that will be loaded. If not provided at initialization, a new MIME::Types instance will be constructed.



24
25
26
# File 'lib/mime/types/loader.rb', line 24

def container
  @container
end

#pathObject (readonly)

The path that will be read for the MIME::Types files.



21
22
23
# File 'lib/mime/types/loader.rb', line 21

def path
  @path
end

Class Method Details

.load(options = { columnar: false }) ⇒ Object

Loads the default MIME::Type registry.



112
113
114
# File 'lib/mime/types/loader.rb', line 112

def load(options = { columnar: false })
  new.load(options)
end

.load_from_json(filename) ⇒ Object

Loads MIME::Types from a single JSON file.

It is expected that the JSON objects will be an array of hash objects. The JSON format is the registry format for the MIME types registry shipped with the mime-types library.



225
226
227
228
# File 'lib/mime/types/loader.rb', line 225

def load_from_json(filename)
  require 'json'
  JSON.parse(read_file(filename)).map { |type| MIME::Type.new(type) }
end

.load_from_v1(filename, __internal__ = false) ⇒ Object

Build the type list from a file in the format:

[*][!][os:]mt/st[<ws>@ext][<ws>:enc][<ws>'url-list][<ws>=docs]

*

An unofficial MIME type. This should be used if and only if the MIME type is not properly specified (that is, not under either x-type or vnd.name.type).

!

An obsolete MIME type. May be used with an unofficial MIME type.

os:

Platform-specific MIME type definition.

mt

The media type.

st

The media subtype.

<ws>@ext

The list of comma-separated extensions.

<ws>:enc

The encoding.

<ws>‘url-list

The list of comma-separated URLs.

<ws>=docs

The documentation string.

That is, everything except the media type and the subtype is optional. The more information that’s available, though, the richer the values that can be provided.

This method has been deprecated and will be removed in mime-types 3.0.



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/mime/types/loader.rb', line 154

def load_from_v1(filename, __internal__ = false)
  MIME::Types.deprecated(self.class, __method__) unless __internal__
  data = read_file(filename).split($/)
  mime = MIME::Types.new
  data.each_with_index { |line, index|
    item = line.chomp.strip
    next if item.empty?

    m = V1_FORMAT.match(item)

    unless m
      MIME::Types.logger.warn <<-EOS
#{filename}:#{index + 1}: Parsing error in v1 MIME type definition.
=> #{line}
      EOS
      fail BadV1Format, line
    end

    unregistered, obsolete, _, mediatype, subtype, extensions, encoding,
      urls, docs, _ = *m.captures

    next if mediatype.nil?

    extensions &&= extensions.split(/,/)
    urls &&= urls.split(/,/)

    if docs.nil?
      use_instead = nil
    else
      use_instead = docs.scan(%r{use-instead:(\S+)}).flatten.first
      docs = docs.gsub(%r{use-instead:\S+}, '').squeeze(' \t')
    end

    mime_type = MIME::Type.new("#{mediatype}/#{subtype}") do |t|
      t.extensions  = extensions
      t.encoding    = encoding
      t.obsolete    = obsolete
      t.registered  = false if unregistered
      t.use_instead = use_instead
      t.docs        = docs
    end

    mime.add_type(mime_type, true)
  }
  mime
end

.load_from_yaml(filename) ⇒ Object

Loads MIME::Types from a single YAML file.

It is expected that the YAML objects contained within the registry array will be tagged as !ruby/object:MIME::Type.

Note that the YAML format is about 2½ times slower than either the v1 format or the JSON format.

NOTE: The purpose of this format is purely for maintenance reasons.



210
211
212
213
214
215
216
217
218
# File 'lib/mime/types/loader.rb', line 210

def load_from_yaml(filename)
  begin
    require 'psych'
  rescue LoadError
    nil
  end
  require 'yaml'
  YAML.load(read_file(filename))
end

Instance Method Details

#load(options = { columnar: false }) ⇒ Object

Loads a MIME::Types registry. Loads from JSON files by default (#load_json).

This will load from columnar files (#load_columnar) if columnar: true is provided in options and there are columnar files in path.



84
85
86
87
88
89
90
# File 'lib/mime/types/loader.rb', line 84

def load(options = { columnar: false })
  if options[:columnar] && !Dir[columnar_path].empty?
    load_columnar
  else
    load_json
  end
end

#load_columnarObject

Loads a MIME::Types registry from columnar files recursively found in path.



71
72
73
74
75
76
77
# File 'lib/mime/types/loader.rb', line 71

def load_columnar
  require 'mime/types/columnar' unless defined?(MIME::Types::Columnar)
  container.extend(MIME::Types::Columnar)
  container.load_base_data(path)

  container
end

#load_jsonObject

Loads a MIME::Types registry from JSON files (*.json) recursively found in path.

It is expected that the JSON objects will be an array of hash objects. The JSON format is the registry format for the MIME types registry shipped with the mime-types library.



61
62
63
64
65
66
67
# File 'lib/mime/types/loader.rb', line 61

def load_json
  Dir[json_path].sort.each do |f|
    types = self.class.load_from_json(f)
    container.add(*types, :silent)
  end
  container
end

#load_v1Object

Loads a MIME::Types registry from files found in path that are in the v1 data format. The file search for this will exclude both JSON (*.json) and YAML (*.yml or *.yaml) files.

This method has been deprecated and will be removed from mime-types 3.0.



97
98
99
100
101
102
103
104
# File 'lib/mime/types/loader.rb', line 97

def load_v1
  MIME::Types.deprecated(self.class, __method__)
  Dir[v1_path].sort.each do |f|
    next if f =~ /\.(?:ya?ml|json|column)$/
    container.add(self.class.load_from_v1(f, true), true)
  end
  container
end

#load_yamlObject

Loads a MIME::Types registry from YAML files (*.yml or *.yaml) recursively found in path.

It is expected that the YAML objects contained within the registry array will be tagged as !ruby/object:MIME::Type.

Note that the YAML format is about 2½ times slower than either the v1 format or the JSON format.

NOTE: The purpose of this format is purely for maintenance reasons.



48
49
50
51
52
53
# File 'lib/mime/types/loader.rb', line 48

def load_yaml
  Dir[yaml_path].sort.each do |f|
    container.add(*self.class.load_from_yaml(f), :silent)
  end
  container
end