[−][src]Crate tree_magic
tree_magic
is a Rust crate that determines the MIME type a given file or byte stream.
About
tree_magic
is designed to be more efficient and to have less false positives compared
to the old approach used by libmagic
, or old-fashioned file extension comparisons.
Instead, this loads all known MIME types into a tree based on subclasses. Then, instead
of checking against every file type, tree_magic
will traverse down the tree and
only check the files that make sense to check.
Features
- Very fast perfomance (~150ns to check one file against one type, between 5,000ns and 100,000ns to find a MIME type.)
- Check if a file is a certain type.
- Handles aliases (ex:
application/zip
vsapplication/x-zip-compressed
) - Uses system FreeDesktop.org magic files on Linux systems, and built-in magic file on Windows and macOS.
- Can delegate different file types to different "checkers", reducing false positives by choosing a different method of attack.
Feature flags
cli
: Enable building of tmagic
binary
staticmime
: Change output of all from_*
functions from String
to &'static str
.
Disables ability to load system magic files. Slightly faster.
Example
extern crate tree_magic; // Load a GIF file let input: &[u8] = include_bytes!("../tests/image/gif"); // Find the MIME type of the GIF let result = tree_magic::from_u8(input); assert_eq!(result, "image/gif"); // Check if the MIME and the file are a match let result = tree_magic::match_u8("image/gif", input); assert_eq!(result, true);
Structs
TYPE | The TypeStruct autogenerated at library init, and used by the library. |
TypeStruct | Information about currently loaded MIME types |
Enums
Cache | Cache used for each checker for each file |
Functions
from_filepath | Gets the type of a file from a filepath. |
from_filepath_node | Gets the type of a file from a filepath, starting at a certain node in the type graph. |
from_u8_node | Gets the type of a file from a raw bytestream, starting at a certain node in the type graph. |
from_u8 | Gets the type of a file from a byte stream. |
is_alias | Determines if a MIME is an alias of another MIME |
match_filepath | Check if the given filepath matches the given MIME type. |
match_u8 | Checks if the given bytestream matches the given MIME type. |