summaryrefslogtreecommitdiff
path: root/lib/mime/types.rb
blob: 172d39e5fb4976a7c42a54e4e6511c22ccc63492 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# -*- ruby encoding: utf-8 -*-

require 'mime/type'
require 'mime/types/cache'
require 'mime/types/loader'

# MIME::Types is a registry of MIME types. It is both a class (created with
# MIME::Types.new) and a default registry (loaded automatically or through
# interactions with MIME::Types.[] and MIME::Types.type_for).
#
# == The Default mime-types Registry
#
# The default mime-types registry is loaded automatically when the library
# is required (<tt>require 'mime/types'</tt>), but it may be lazily loaded
# (loaded on first use) with the use of the environment variable
# +RUBY_MIME_TYPES_LAZY_LOAD+ having any value other than +false+. The
# initial startup is about 14× faster (~10 ms vs ~140 ms), but the
# registry will be loaded at some point in the future.
#
# The default mime-types registry can also be loaded from a Marshal cache
# file specific to the version of MIME::Types being loaded. This will be
# handled automatically with the use of a file referred to in the
# environment variable +RUBY_MIME_TYPES_CACHE+. MIME::Types will attempt to
# load the registry from this cache file (MIME::Type::Cache.load); if it
# cannot be loaded (because the file does not exist, there is an error, or
# the data is for a different version of mime-types), the default registry
# will be loaded from the normal JSON version and then the cache file will
# be *written* to the location indicated by +RUBY_MIME_TYPES_CACHE+. Cache
# file loads just over 4½× faster (~30 ms vs ~140 ms).
# loads.
#
# Notes:
# * The loading of the default registry is *not* atomic; when using a
#   multi-threaded environment, it is recommended that lazy loading is not
#   used and mime-types is loaded as early as possible.
# * Cache files should be specified per application in a multiprocess
#   environment and should be initialized during deployment or before
#   forking to minimize the chance that the multiple processes will be
#   trying to write to the same cache file at the same time, or that two
#   applications that are on different versions of mime-types would be
#   thrashing the cache.
# * Unless cache files are preinitialized, the application using the
#   mime-types cache file must have read/write permission to the cache file.
#
# == Usage
#  require 'mime/types'
#
#  plaintext = MIME::Types['text/plain']
#  print plaintext.media_type           # => 'text'
#  print plaintext.sub_type             # => 'plain'
#
#  puts plaintext.extensions.join(" ")  # => 'asc txt c cc h hh cpp'
#
#  puts plaintext.encoding              # => 8bit
#  puts plaintext.binary?               # => false
#  puts plaintext.ascii?                # => true
#  puts plaintext.obsolete?             # => false
#  puts plaintext.registered?           # => true
#  puts plaintext == 'text/plain'       # => true
#  puts MIME::Type.simplified('x-appl/x-zip') # => 'appl/zip'
#
class MIME::Types
  # The release version of Ruby MIME::Types
  VERSION = MIME::Type::VERSION

  include Enumerable

  # The data version.
  attr_reader :data_version

  # Creates a new MIME::Types registry.
  def initialize
    @type_variants    = Container.new
    @extension_index  = Container.new
    @data_version     = VERSION.dup.freeze
  end

  def add_type_variant(mime_type) # :nodoc:
    MIME.deprecated(self, __method__, :private)
    add_type_variant!(mime_type)
  end

  def index_extensions(mime_type) # :nodoc:
    MIME.deprecated(self, __method__, :private)
    index_extensions!(mime_type)
  end

  def defined_types # :nodoc:
    MIME.deprecated(self, __method__)
    @type_variants.values.flatten
  end

  # Returns the number of known type variants.
  def count
    @type_variants.values.reduce(0) { |m, o| m + o.size }
  end

  # Iterates through the type variants.
  def each
    if block_given?
      @type_variants.each_value { |tv| tv.each { |t| yield t } }
    else
      enum_for(:each)
    end
  end

  @__types__  = nil

  # Returns a list of MIME::Type objects, which may be empty. The optional
  # flag parameters are <tt>:complete</tt> (finds only complete MIME::Type
  # objects) and <tt>:registered</tt> (finds only MIME::Types that are
  # registered). It is possible for multiple matches to be returned for
  # either type (in the example below, 'text/plain' returns two values --
  # one for the general case, and one for VMS systems).
  #
  #   puts "\nMIME::Types['text/plain']"
  #   MIME::Types['text/plain'].each { |t| puts t.to_a.join(", ") }
  #
  #   puts "\nMIME::Types[/^image/, complete: true]"
  #   MIME::Types[/^image/, :complete => true].each do |t|
  #     puts t.to_a.join(", ")
  #   end
  #
  # If multiple type definitions are returned, returns them sorted as
  # follows:
  #   1. Complete definitions sort before incomplete ones;
  #   2. IANA-registered definitions sort before LTSW-recorded
  #      definitions.
  #   3. Generic definitions sort before platform-specific ones;
  #   4. Current definitions sort before obsolete ones;
  #   5. Obsolete definitions with use-instead clauses sort before those
  #      without;
  #   6. Obsolete definitions use-instead clauses are compared.
  #   7. Sort on name.
  #
  # An additional flag of :platform (finds only MIME::Types for the current
  # platform) is currently supported but deprecated.
  def [](type_id, flags = {})
    if flags[:platform]
      MIME.deprecated(self, __method__, "using the :platform flag")
    end

    matches = case type_id
              when MIME::Type
                @type_variants[type_id.simplified]
              when Regexp
                match(type_id)
              else
                @type_variants[MIME::Type.simplified(type_id)]
              end

    prune_matches(matches, flags).sort { |a, b| a.priority_compare(b) }
  end

  # Return the list of MIME::Types which belongs to the file based on its
  # filename extension. If there is no extension, the filename will be used
  # as the matching criteria on its own.
  #
  # This will always return a merged, flatten, priority sorted, unique array.
  #
  #   puts MIME::Types.type_for('citydesk.xml')
  #     => [application/xml, text/xml]
  #   puts MIME::Types.type_for('citydesk.gif')
  #     => [image/gif]
  #   puts MIME::Types.type_for(%w(citydesk.xml citydesk.gif))
  #     => [application/xml, image/gif, text/xml]
  #
  # If +platform+ is +true+, then only file types that are specific to the
  # current platform will be returned. This parameter has been deprecated.
  def type_for(filename, platform = false)
    types = Array(filename).flat_map { |fn|
      @extension_index[fn.chomp.downcase[/\.?([^.]*?)$/, 1]]
    }.compact.sort { |a, b| a.priority_compare(b) }.uniq

    if platform
      MIME.deprecated(self, __method__,
                      "using the platform parameter")
      types.select(&:platform?)
    else
      types
    end
  end
  alias_method :of, :type_for

  # Add one or more MIME::Type objects to the set of known types. If the
  # type is already known, a warning will be displayed.
  #
  # The last parameter may be the value <tt>:silent</tt> or +true+ which
  # will suppress duplicate MIME type warnings.
  def add(*types)
    quiet = ((types.last == :silent) or (types.last == true))

    types.each do |mime_type|
      case mime_type
      when true, false, nil, Symbol
        nil
      when MIME::Types
        variants = mime_type.instance_variable_get(:@type_variants)
        add(*variants.values.flatten, quiet)
      when Array
        add(*mime_type, quiet)
      else
        add_type(mime_type, quiet)
      end
    end
  end

  # Add a single MIME::Type object to the set of known types. If the type is
  # already known, a warning will be displayed. The +quiet+ parameter may be
  # a truthy value to suppress that warning.
  def add_type(mime_type, quiet = false)
    if !quiet and @type_variants[mime_type.simplified].include?(mime_type)
      warn('Type %s is already registered as a variant of %s.' %
           [ mime_type, mime_type.simplified ])
    end

    add_type_variant!(mime_type)
    index_extensions!(mime_type)
  end

  class << self
    include Enumerable

    # Load MIME::Types from a v1 file registry.
    #
    # This method has been deprecated.
    def load_from_file(filename)
      MIME.deprecated(self, __method__)
      MIME::Types::Loader.load_from_v1(filename)
    end

    # MIME::Types#[] against the default MIME::Types registry.
    def [](type_id, flags = {})
      __types__[type_id, flags]
    end

    # MIME::Types#count against the default MIME::Types registry.
    def count
      __types__.count
    end

    # MIME::Types#each against the default MIME::Types registry.
    def each
      if block_given?
        __types__.each { |t| yield t }
      else
        enum_for(:each)
      end
    end

    # MIME::Types#type_for against the default MIME::Types registry.
    def type_for(filename, platform = false)
      __types__.type_for(filename, platform)
    end
    alias_method :of, :type_for

    # MIME::Types#add against the default MIME::Types registry.
    def add(*types)
      __types__.add(*types)
    end

    # Returns the currently defined cache file, if any.
    def cache_file
      MIME.deprecated(self, __method__)
      ENV['RUBY_MIME_TYPES_CACHE']
    end

    def add_type_variant(mime_type) # :nodoc:
      __types__.add_type_variant(mime_type)
    end

    def index_extensions(mime_type) # :nodoc:
      __types__.index_extensions(mime_type)
    end

    private
    def lazy_load?
      (lazy = ENV['RUBY_MIME_TYPES_LAZY_LOAD']) && (lazy != 'false')
    end

    def __types__
      (defined?(@__types__) and @__types__) or load_default_mime_types
    end

    def load_default_mime_types
      @__types__ = MIME::Types::Cache.load
      unless @__types__
        @__types__ = MIME::Types::Loader.load
        MIME::Types::Cache.save(@__types__)
      end
      @__types__
    end
  end

  private
  def add_type_variant!(mime_type)
    @type_variants[mime_type.simplified] << mime_type
  end

  def index_extensions!(mime_type)
    mime_type.extensions.each { |ext| @extension_index[ext] << mime_type }
  end

  def prune_matches(matches, flags)
    matches.delete_if { |e| not e.complete? } if flags[:complete]
    matches.delete_if { |e| not e.platform? } if flags[:platform]
    matches.delete_if { |e| not e.registered? } if flags[:registered]
    matches
  end

  def match(pattern)
    @type_variants.select { |k, v| k =~ pattern }.values.flatten
  end

  load_default_mime_types unless lazy_load?
end

# vim: ft=ruby