infer

Build Status crates version documentation

Small crate to infer file and MIME type by checking the magic number signature.

Adaptation of filetype Go package ported to Rust.

Does not require magic file database (i.e. /etc/magic).

Features

Documentation

https://docs.rs/infer

Installation

This crate works with Cargo and is on crates.io. Add it to your Cargo.toml like so:

toml [dependencies] infer = "0.1"

Examples

Get the type of a buffer

```rust use infer::Infer;

let v = vec![0xFF, 0xD8, 0xFF, 0xAA]; let info = Infer::new();

asserteq!("image/jpeg", info.get(&v).unwrap().mime); asserteq!("jpg", info.get(&v).unwrap().ext); ```

Check path

```rust use infer::Infer;

let info = Infer::new(); let res = info.getfrompath("testdata/sample.jpg");

assert!(res.isok()); let o = res.unwrap(); assert!(o.issome()); let typ = o.unwrap();

asserteq!("image/jpeg", typ.mime); asserteq!("jpg", typ.ext); ```

Check for specific type

rust let v = vec![0xFF, 0xD8, 0xFF, 0xAA]; assert!(infer::image::is_jpeg(&v));

Check for specific type class

Note individual matcher functions do not require init

rust let v = vec![0xFF, 0xD8, 0xFF, 0xAA]; let info = infer::Infer::new(); assert!(info.is_image(&v));

Adds a custom file type matcher

```rust fn custom_matcher(buf: &[u8]) -> bool { return buf.len() >= 3 && buf[0] == 0x10 && buf[1] == 0x11 && buf[2] == 0x12; }

let mut info = infer::Infer::new(); info.add("custom/foo", "foo", custom_matcher);

let v = vec![0x10, 0x11, 0x12, 0x13]; let res = info.get(&v).unwrap();

asserteq!("custom/foo", res.mime); asserteq!("foo", res.ext); ```

Supported types

Image

Video

Audio

Archive

Documents

Font

Application

Known Issues

License

MIT