Class: MIME::Types

Inherits:
Object show all
Extended by:
Enumerable
Includes:
Enumerable
Defined in:
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mime-types-3.4.1/lib/mime/types.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mime-types-3.4.1/lib/mime/types.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mime-types-3.4.1/lib/mime/types/full.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mime-types-3.4.1/lib/mime/types/loader.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mime-types-3.4.1/lib/mime/types/logger.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mime-types-3.4.1/lib/mime/types/registry.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mime-types-3.4.1/lib/mime/types/deprecations.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mime-types-data-3.2022.0105/lib/mime/types/data.rb

Overview

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 (require 'mime/types'), 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.provisional?          # => false
puts plaintext == 'text/plain'       # => true
puts MIME::Type.simplified('x-appl/x-zip') # => 'appl/zip'

Defined Under Namespace

Modules: Columnar, Data Classes: Cache, Container, Loader, WarnLogger

Constant Summary collapse

VERSION =

The release version of Ruby MIME::Types

MIME::Type::VERSION

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

as_json, compact_blank, exclude?, excluding, in_order_of, including, index_by, index_with, many?, maximum, minimum, pick, pluck, sole, sum

Constructor Details

#initializeTypes

Creates a new MIME::Types registry.



75
76
77
78
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mime-types-3.4.1/lib/mime/types.rb', line 75

def initialize
  @type_variants = Container.new
  @extension_index = Container.new
end

Class Attribute Details

.loggerObject

Configure the MIME::Types logger. This defaults to an instance of a logger that passes messages (unformatted) through to Kernel#warn.



12
13
14
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mime-types-3.4.1/lib/mime/types/logger.rb', line 12

def logger
  @logger
end

Class Method Details

.[](type_id, complete: false, registered: false) ⇒ Object

MIME::Types#[] against the default MIME::Types registry.



14
15
16
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mime-types-3.4.1/lib/mime/types/registry.rb', line 14

def [](type_id, complete: false, registered: false)
  __types__[type_id, complete: complete, registered: registered]
end

.add(*types) ⇒ Object

MIME::Types#add against the default MIME::Types registry.



39
40
41
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mime-types-3.4.1/lib/mime/types/registry.rb', line 39

def add(*types)
  __types__.add(*types)
end

.countObject

MIME::Types#count against the default MIME::Types registry.



19
20
21
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mime-types-3.4.1/lib/mime/types/registry.rb', line 19

def count
  __types__.count
end

.deprecated(klass, sym, message = nil, &block) ⇒ Object

Used to mark a method as deprecated in the mime-types interface.



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
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mime-types-3.4.1/lib/mime/types/deprecations.rb', line 10

def self.deprecated(klass, sym, message = nil, &block) # :nodoc:
  level =
    case klass
          when Class, Module
            "."
          else
            klass = klass.class
            "#"
    end
  message =
    case message
            when :private, :protected
              "and will be #{message}"
            when nil
              "and will be removed"
            else
              message
    end
  MIME::Types.logger.warn <<-WARNING.chomp.strip
    #{caller(2..2).first}: #{klass}#{level}#{sym} is deprecated #{message}.
  WARNING

  return unless block
  block.call
end

.eachObject

MIME::Types#each against the default MIME::Types registry.



24
25
26
27
28
29
30
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mime-types-3.4.1/lib/mime/types/registry.rb', line 24

def each
  if block_given?
    __types__.each { |t| yield t }
  else
    enum_for(:each)
  end
end

.newObject

:nodoc:



7
8
9
10
11
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mime-types-3.4.1/lib/mime/types/registry.rb', line 7

def new(*) # :nodoc:
  super.tap do |types|
    __instances__.add types
  end
end

.type_for(filename) ⇒ Object Also known as: of

MIME::Types#type_for against the default MIME::Types registry.



33
34
35
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mime-types-3.4.1/lib/mime/types/registry.rb', line 33

def type_for(filename)
  __types__.type_for(filename)
end

Instance Method Details

#[](type_id, complete: false, registered: false) ⇒ Object

Returns a list of MIME::Type objects, which may be empty. The optional flag parameters are :complete (finds only complete MIME::Type objects) and :registered (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. Current definitions sort before obsolete ones;
4. Obsolete definitions with use-instead clauses sort before those
   without;
5. Obsolete definitions use-instead clauses are compared.
6. Sort on name.


125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mime-types-3.4.1/lib/mime/types.rb', line 125

def [](type_id, complete: false, registered: false)
  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, complete, registered).sort { |a, b|
    a.priority_compare(b)
  }
end

#add(*types) ⇒ Object

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 :silent or true which will suppress duplicate MIME type warnings.



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mime-types-3.4.1/lib/mime/types.rb', line 167

def add(*types)
  quiet = ((types.last == :silent) || (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.inject(Set.new, :+).to_a, quiet)
    when Array
      add(*mime_type, quiet)
    else
      add_type(mime_type, quiet)
    end
  end
end

#add_type(type, quiet = false) ⇒ Object

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.



188
189
190
191
192
193
194
195
196
197
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mime-types-3.4.1/lib/mime/types.rb', line 188

def add_type(type, quiet = false)
  if !quiet && @type_variants[type.simplified].include?(type)
    MIME::Types.logger.warn <<-WARNING.chomp.strip
      Type #{type} is already registered as a variant of #{type.simplified}.
    WARNING
  end

  add_type_variant!(type)
  index_extensions!(type)
end

#countObject

Returns the number of known type variants.



81
82
83
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mime-types-3.4.1/lib/mime/types.rb', line 81

def count
  @type_variants.values.inject(0) { |a, e| a + e.size }
end

#eachObject

Iterates through the type variants.



90
91
92
93
94
95
96
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mime-types-3.4.1/lib/mime/types.rb', line 90

def each
  if block_given?
    @type_variants.each_value { |tv| tv.each { |t| yield t } }
  else
    enum_for(:each)
  end
end

#inspectObject

:nodoc:



85
86
87
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mime-types-3.4.1/lib/mime/types.rb', line 85

def inspect # :nodoc:
  "#<#{self.class}: #{count} variants, #{@extension_index.count} extensions>"
end

#ofObject

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]


160
161
162
163
164
165
166
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mime-types-3.4.1/lib/mime/types.rb', line 160

def type_for(filename)
  Array(filename).flat_map { |fn|
    @extension_index[fn.chomp.downcase[/\.?([^.]*?)$/, 1]]
  }.compact.inject(Set.new, :+).sort { |a, b|
    a.priority_compare(b)
  }
end

#type_for(filename) ⇒ Object

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]


153
154
155
156
157
158
159
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mime-types-3.4.1/lib/mime/types.rb', line 153

def type_for(filename)
  Array(filename).flat_map { |fn|
    @extension_index[fn.chomp.downcase[/\.?([^.]*?)$/, 1]]
  }.compact.inject(Set.new, :+).sort { |a, b|
    a.priority_compare(b)
  }
end