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 23 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 mime type by file extension
# File lib/mimemagic.rb, line 65 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 78 def self.by_magic(io) mime = unless io.respond_to?(:seek) && io.respond_to?(:read) str = io.respond_to?(:read) ? io.read : io.to_s str = str.force_encoding(Encoding::BINARY) if str.respond_to? :force_encoding MAGIC.find {|type, matches| magic_match_str(str, matches) } else io.binmode io.set_encoding(Encoding::BINARY) if io.respond_to?(:set_encoding) MAGIC.find {|type, matches| magic_match_io(io, matches) } end mime && new(mime[0]) end
Lookup mime type by filename
# File lib/mimemagic.rb, line 72 def self.by_path(path) by_extension(File.extname(path)) end
# File lib/mimemagic.rb, line 108 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 9 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 35 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 112 def self.magic_match_io(io, matches) matches.any? do |offset, value, children| match = if Range === offset io.seek(offset.begin) x = io.read(offset.end - offset.begin + value.bytesize) x && x.include?(value) else io.seek(offset) io.read(value.bytesize) == value end match && (!children || magic_match_io(io, children)) end end
# File lib/mimemagic.rb, line 127 def self.magic_match_str(str, matches) matches.any? do |offset, value, children| match = if Range === offset x = str[offset.begin, offset.end - offset.begin + value.bytesize] x && x.include?(value) else str[offset, value.bytesize] == value end match && (!children || magic_match_str(str, children)) end end
Public Instance Methods
# File lib/mimemagic.rb, line 46 def audio?; mediatype == 'audio'; end
Returns true if type is child of parent type
# File lib/mimemagic.rb, line 50 def child_of?(parent) MimeMagic.child?(type, parent) end
Get mime comment
# File lib/mimemagic.rb, line 60 def comment (TYPES.key?(type) ? TYPES[type][2] : nil).to_s end
Allow comparison with string
# File lib/mimemagic.rb, line 98 def eql?(other) type == other.to_s end
Get string list of file extensions
# File lib/mimemagic.rb, line 55 def extensions TYPES.key?(type) ? TYPES[type][0] : [] end
# File lib/mimemagic.rb, line 102 def hash type.hash end
Mediatype shortcuts
# File lib/mimemagic.rb, line 45 def image?; mediatype == 'image'; end
Returns true if type is a text format
# File lib/mimemagic.rb, line 42 def text?; mediatype == 'text' || child_of?('text/plain'); end
Return type as string
# File lib/mimemagic.rb, line 93 def to_s type end
# File lib/mimemagic.rb, line 47 def video?; mediatype == 'video'; end