Crate tree_magic_mini[][src]

tree_magic_mini is a Rust crate that determines the MIME type a given file or byte stream.

This is a fork of the tree_magic crate by Allison Hancock. It includes the following changes:

  • Updated dependencies.
  • Reduced copying and memory allocation, for a slight increase in speed and decrease in memory use.
  • Reduced API surface. Some previously public APIs are now internal.
  • Removed the optional cli feature and tmagic binary.

About tree_magic

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 vs application/x-zip-compressed)
  • Can delegate different file types to different “checkers”, reducing false positives by choosing a different method of attack.

Example

// Load a GIF file
let input: &[u8] = include_bytes!("../tests/image/gif");

// Find the MIME type of the GIF
let result = tree_magic_mini::from_u8(input);
assert_eq!(result, "image/gif");

// Check if the MIME and the file are a match
let result = tree_magic_mini::match_u8("image/gif", input);
assert_eq!(result, true);

Functions

from_filepath

Gets the type of a file from a filepath.

from_u8

Gets the type of a file from a byte stream.

match_filepath

Check if the given filepath matches the given MIME type.

match_u8

Checks if the given bytestream matches the given MIME type.