class MimeMagic

Mime type detection

TODO:

Order by popularity, to speed up the MAGIC.find command.

Constants

EXTENSIONS
MAGIC
TYPES
VERSION

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/epitools/mimemagic.rb, line 41
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
by_extension(ext) click to toggle source

Lookup mime type by file extension

# File lib/epitools/mimemagic.rb, line 88
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/epitools/mimemagic.rb, line 101
def self.by_magic(io)
  if !(io.respond_to?(:seek) && io.respond_to?(:read))
    io = StringIO.new(io.to_s, 'rb:binary')
  end
  mime = MAGIC.find {|type, matches| magic_match(io, matches) }
  mime && new(mime[0])
end
by_path(path) click to toggle source

Lookup mime type by filename

# File lib/epitools/mimemagic.rb, line 95
def self.by_path(path)
  by_extension(File.extname(path))
end
new(type) click to toggle source

Mime type by type string

# File lib/epitools/mimemagic.rb, line 27
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/epitools/mimemagic.rb, line 53
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

child?(child, parent) click to toggle source
# File lib/epitools/mimemagic.rb, line 131
def self.child?(child, parent)
  child == parent || TYPES.key?(child) && TYPES[child][1].any? {|p| child?(p, parent) }
end
magic_match(io, matches) click to toggle source
# File lib/epitools/mimemagic.rb, line 135
def self.magic_match(io, matches)
  matches.any? do |offset, value, children|
    match = if Range === offset
              io.seek(offset.begin)
              io.read(offset.end - offset.begin + value.length).include?(value)
            else
              io.seek(offset)
              value == io.read(value.length)
            end
    match && (!children || magic_match(io, children))
  end
rescue
  false
end

Public Instance Methods

==(x) click to toggle source

Allow comparison with string

# File lib/epitools/mimemagic.rb, line 115
def ==(x)
  type == x.to_s
end
audio?() click to toggle source
# File lib/epitools/mimemagic.rb, line 64
def audio?; mediatype == 'audio'; end
child_of?(parent) click to toggle source

Returns true if type is child of parent type

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

Get mime comment

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

allow comparison with something else

# File lib/epitools/mimemagic.rb, line 125
def eql?(other)
  self.type == other.type
end
ext() click to toggle source

Default extension

# File lib/epitools/mimemagic.rb, line 78
def ext
  extensions.first
end
extensions() click to toggle source

Get string list of file extensions

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

allow comparison with hashes

# File lib/epitools/mimemagic.rb, line 120
def hash
  type.hash
end
image?() click to toggle source

Mediatype shortcuts

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

Returns true if type is a text format

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

Return type as string

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