class MimeMagic
Mime type detection
TODO:
Order by popularity, to speed up the MAGIC.find command.
Constants
- EXTENSIONS
- MAGIC
- TYPES
- VERSION
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/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
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
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
Lookup mime type by filename
# File lib/epitools/mimemagic.rb, line 95 def self.by_path(path) by_extension(File.extname(path)) end
Mime type by type string
# File lib/epitools/mimemagic.rb, line 27 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/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
# 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
# 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
Allow comparison with string
# File lib/epitools/mimemagic.rb, line 115 def ==(x) type == x.to_s end
# File lib/epitools/mimemagic.rb, line 64 def audio?; mediatype == 'audio'; end
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
Get mime comment
# File lib/epitools/mimemagic.rb, line 83 def comment (TYPES.key?(type) ? TYPES[type][2] : nil).to_s end
allow comparison with something else
# File lib/epitools/mimemagic.rb, line 125 def eql?(other) self.type == other.type end
Default extension
# File lib/epitools/mimemagic.rb, line 78 def ext extensions.first end
Get string list of file extensions
# File lib/epitools/mimemagic.rb, line 73 def extensions TYPES.key?(type) ? TYPES[type][0] : [] end
allow comparison with hashes
# File lib/epitools/mimemagic.rb, line 120 def hash type.hash end
Mediatype shortcuts
# File lib/epitools/mimemagic.rb, line 63 def image?; mediatype == 'image'; end
Returns true if type is a text format
# File lib/epitools/mimemagic.rb, line 60 def text?; mediatype == 'text' || child_of?('text/plain'); end
Return type as string
# File lib/epitools/mimemagic.rb, line 110 def to_s type end
# File lib/epitools/mimemagic.rb, line 65 def video?; mediatype == 'video'; end