class MimeMagic
Mime type detection
Generated from freedesktop.org.xml
Constants
- VERSION
MimeMagic version string @api public
Attributes
Public Class Methods
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
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
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
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
Lookup mime type by filename
# File lib/mimemagic.rb, line 74 def self.by_path(path) by_extension(File.extname(path)) end
# 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
Mime type by type string
# File lib/mimemagic.rb, line 11 def initialize(type) @type = type @mediatype, @subtype = type.split('/', 2) end
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
# 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
# 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
# File lib/mimemagic.rb, line 48 def audio?; mediatype == 'audio'; end
Returns true if type is child of parent type
# File lib/mimemagic.rb, line 52 def child_of?(parent) MimeMagic.child?(type, parent) end
Get mime comment
# File lib/mimemagic.rb, line 62 def comment (TYPES.key?(type) ? TYPES[type][2] : nil).to_s end
Allow comparison with string
# File lib/mimemagic.rb, line 97 def eql?(other) type == other.to_s end
Get string list of file extensions
# File lib/mimemagic.rb, line 57 def extensions TYPES.key?(type) ? TYPES[type][0] : [] end
# File lib/mimemagic.rb, line 101 def hash type.hash end
Mediatype shortcuts
# File lib/mimemagic.rb, line 47 def image?; mediatype == 'image'; end
Returns true if type is a text format
# File lib/mimemagic.rb, line 44 def text?; mediatype == 'text' || child_of?('text/plain'); end
Return type as string
# File lib/mimemagic.rb, line 92 def to_s type end
# File lib/mimemagic.rb, line 49 def video?; mediatype == 'video'; end