class MimeMagic

Mime type detection

Generated from freedesktop.org.xml

Constants

VERSION

MimeMagic version string @api public

Attributes

mediatype[R]
subtype[R]
type[R]

Public Class Methods

add(type, options) click to toggle source

Add custom mime type. Arguments:

  • type: Mime type

  • options: Options hash

Option keys:

  • :extensions: String list or single string of file extensions

  • :parents: String list or single string of parent mime types

  • :magic: Mime magic specification

  • :comment: Comment string

# File lib/mimemagic.rb, line 25
def self.add(type, options)
  extensions = [options[:extensions]].flatten.compact
  TYPES[type] = [extensions,
                [options[:parents]].flatten.compact,
                options[:comment]]
  extensions.each {|ext| EXTENSIONS[ext] = type }
  MAGIC.unshift [type, options[:magic]] if options[:magic]
end
all_by_magic(io) click to toggle source

Lookup all mime types by magic content analysis. This is a slower operation.

# File lib/mimemagic.rb, line 87
def self.all_by_magic(io)
  magic_match(io, :select).map { |mime| new(mime[0]) }
end
by_extension(ext) click to toggle source

Lookup mime type by file extension

# File lib/mimemagic.rb, line 67
def self.by_extension(ext)
  ext = ext.to_s.downcase
  mime = ext[0..0] == '.' ? EXTENSIONS[ext[1..-1]] : EXTENSIONS[ext]
  mime && new(mime)
end
by_magic(io) click to toggle source

Lookup mime type by magic content analysis. This is a slow operation.

# File lib/mimemagic.rb, line 80
def self.by_magic(io)
  mime = magic_match(io, :find)
  mime && new(mime[0])
end
by_path(path) click to toggle source

Lookup mime type by filename

# File lib/mimemagic.rb, line 74
def self.by_path(path)
  by_extension(File.extname(path))
end
child?(child, parent) click to toggle source
# File lib/mimemagic.rb, line 107
def self.child?(child, parent)
  child == parent || TYPES.key?(child) && TYPES[child][1].any? {|p| child?(p, parent) }
end
new(type) click to toggle source

Mime type by type string

# File lib/mimemagic.rb, line 11
def initialize(type)
  @type = type
  @mediatype, @subtype = type.split('/', 2)
end
remove(type) click to toggle source

Removes a mime type from the dictionary. You might want to do this if you're seeing impossible conflicts (for instance, application/x-gmc-link).

  • type: The mime type to remove. All associated extensions and magic are removed too.

# File lib/mimemagic.rb, line 37
def self.remove(type)
  EXTENSIONS.delete_if {|ext, t| t == type }
  MAGIC.delete_if {|t, m| t == type }
  TYPES.delete(type)
end

Private Class Methods

magic_match(io, method) click to toggle source
# File lib/mimemagic.rb, line 111
def self.magic_match(io, method)
  return magic_match(StringIO.new(io.to_s), method) unless io.respond_to?(:read)

  io.binmode if io.respond_to?(:binmode)
  io.set_encoding(Encoding::BINARY) if io.respond_to?(:set_encoding)
  buffer = "".force_encoding(Encoding::BINARY)

  MAGIC.send(method) { |type, matches| magic_match_io(io, matches, buffer) }
end
magic_match_io(io, matches, buffer) click to toggle source
# File lib/mimemagic.rb, line 121
def self.magic_match_io(io, matches, buffer)
  matches.any? do |offset, value, children|
    match =
      if Range === offset
        io.read(offset.begin, buffer)
        x = io.read(offset.end - offset.begin + value.bytesize, buffer)
        x && x.include?(value)
      else
        io.read(offset, buffer)
        io.read(value.bytesize, buffer) == value
      end
    io.rewind
    match && (!children || magic_match_io(io, children, buffer))
  end
end

Public Instance Methods

==(other)
Alias for: eql?
audio?() click to toggle source
# File lib/mimemagic.rb, line 48
def audio?; mediatype == 'audio'; end
child_of?(parent) click to toggle source

Returns true if type is child of parent type

# File lib/mimemagic.rb, line 52
def child_of?(parent)
  MimeMagic.child?(type, parent)
end
comment() click to toggle source

Get mime comment

# File lib/mimemagic.rb, line 62
def comment
  (TYPES.key?(type) ? TYPES[type][2] : nil).to_s
end
eql?(other) click to toggle source

Allow comparison with string

# File lib/mimemagic.rb, line 97
def eql?(other)
  type == other.to_s
end
Also aliased as: ==
extensions() click to toggle source

Get string list of file extensions

# File lib/mimemagic.rb, line 57
def extensions
  TYPES.key?(type) ? TYPES[type][0] : []
end
hash() click to toggle source
# File lib/mimemagic.rb, line 101
def hash
  type.hash
end
image?() click to toggle source

Mediatype shortcuts

# File lib/mimemagic.rb, line 47
def image?; mediatype == 'image'; end
text?() click to toggle source

Returns true if type is a text format

# File lib/mimemagic.rb, line 44
def text?; mediatype == 'text' || child_of?('text/plain'); end
to_s() click to toggle source

Return type as string

# File lib/mimemagic.rb, line 92
def to_s
  type
end
video?() click to toggle source
# File lib/mimemagic.rb, line 49
def video?; mediatype == 'video'; end