class

Marcel::Magic

Inherits Reference < Object

Mime type detection

Constants

HTML_MAGIC = [MagicRule.new(0..4096, Regex.new("\\A(?:\\x{FEFF})?[ \\t\\r\\n\\f]*(?:<\\?xml(?:[ \\t\\r\\n\\f]+(?:[^?]|\\?(?!>))*)?\\?>[ \\t\\r\\n\\f]*)?" + "(?:<!--(?:[^-]|-(?!->))*-->[ \\t\\r\\n\\f]*)*(?:<!DOCTYPE[ \\t\\r\\n\\f]+html(?=[ \\t\\r\\n\\f>])|<html(?=[ \\t\\r\\n\\f>]))", :ignore_case))] of MagicRule
XHTML_MAGIC = [MagicRule.new(0..4096, Regex.new((((((((((((((((("\\A" + "(?:\\x{FEFF})?[ \\t\\r\\n\\f]*") + "(?:<\\?xml(?:[ \\t\\r\\n\\f]+(?:[^?]|\\?(?!>))*)?\\?>[ \\t\\r\\n\\f]*)?") + "(?:<!--(?:[^-]|-(?!->))*-->[ \\t\\r\\n\\f]*)*") + "(?:") + "<!DOCTYPE[ \\t\\r\\n\\f]+html(?:[ \\t\\r\\n\\f]+[^>]*)?>[ \\t\\r\\n\\f]*") + "(?:<!--(?:[^-]|-(?!->))*-->[ \\t\\r\\n\\f]*)*") + ")?") + "<html(?=[ \\t\\r\\n\\f>])") + "(?:") + "[ \\t\\r\\n\\f]+") + "(?!xmlns[ \\t\\r\\n\\f]*=)") + "[A-Za-z_:][A-Za-z0-9_.:-]*") + "[ \\t\\r\\n\\f]*=[ \\t\\r\\n\\f]*") + "(?:\"[^\"]*\"|'[^']*')") + ")*") + "[ \\t\\r\\n\\f]+xmlns[ \\t\\r\\n\\f]*=[ \\t\\r\\n\\f]*") + "(?:\"http://www\\.w3\\.org/1999/xhtml\"|'http://www\\.w3\\.org/1999/xhtml')", :extended))] of MagicRule

Constructors

new(type : String)

Mime type by type string

Source

Class methods

add(type : String, extensions : String | Array(String) | Nil = nil, parents : String | Array(String) | Nil = nil, aliases : String | Array(String) | Nil = nil, magic : Array(MagicRule) | Nil = nil)

Add custom mime type. Arguments:

  • type: Mime type
  • extensions: String or list of file extensions
  • parents: String or list of parent mime types
  • aliases: String or list of aliased mime types
  • magic: Mime magic specification
Source
all_by_magic(io : IO | Path | String) : Array(Magic)

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

Source
by_extension(ext : String) : Magic | Nil

Lookup mime type by file extension

Source
by_magic(io : IO | Path | String) : Magic | Nil

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

Source
by_path(path : String) : Magic | Nil

Lookup mime type by filename

Source
by_type(type : String | Nil) : Magic | Nil

Lookup canonical mime type by mime type string, resolving aliases

Source
canonical(type : String | Nil) : String | Nil

Resolve an aliased type string to its canonical type string

Source
canonicalize(type : String, instead_of : String)

Renames a canonical type: the instead_of type's extensions, magic matchers, parents, and aliases are re-registered under type, and the old name becomes an alias of the new. Useful when a historical or de facto type is preferable to the canonical type shipped in the generated tables, without giving up its matchers.

Source
child?(child : String, parent : String) : Bool
Source
io_read(io : IO, length : Int32) : Bytes | Nil
Source
io_seek(io : IO, offset : Int32) : Bool
Source
magic_match(io : IO | Path | String, all : Bool)
Source
magic_match_io(io : IO, matches : Array(MagicRule)) : Bool
Source
remove(type : String)

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, magic, and aliases are removed too.
Source

Instance methods

==(other) : Bool

Returns false (other can only be a Value here).

Source
audio?
Source
canonical

Resolve an aliased type to its canonical type; canonical types return themselves

Source
child_of?(parent : String) : Bool

Returns true if type is child of parent type

Source
comment

Get mime comment

Source
eql?(other) : Bool

Allow comparison with string

Source
extensions

Get string list of file extensions

Source
hash(hasher)

See Object#hash(hasher)

Source
image?

Mediatype shortcuts

Source
mediatype
Source
subtype
Source
text?

Returns true if type is a text format

Source
to_s

Return type as string

Source
type
Source
video?
Source

Nested types