Skip to content

theseus-rs/file-type

File Type

ci Documentation Code Coverage Benchmarks Latest version License Semantic Versioning

File Type is a library that uses file signatures and file extensions to determine the type of file.

Signature, extension and media type data are provided by:

If you need a format that is not supported, please provide your submission to the PRONOM database and include the reference id when opening a feature request for this project.

Getting Started

Detect the file type from bytes:

use file_type::FileType;

let file_type = FileType::from_bytes(b"\xCA\xFE\xBA\xBE");
assert_eq!(file_type.name(), "Java Class File");
assert_eq!(file_type.media_types(), Vec::<String>::new());
assert_eq!(file_type.extensions(), vec!["class"]);

Detect the file type from a file:

use file_type::FileType;
use std::path::Path;

#[tokio::main]
async fn main() {
    let file_path = Path::new("image.png");
    let file_type = FileType::try_from_file(file_path).await.expect("file type not found");
    assert_eq!(file_type.id(), "fmt/11");
    assert_eq!(file_type.name(), "Portable Network Graphics");
    assert_eq!(file_type.extensions(), vec!["png"]);
    assert_eq!(file_type.media_types(), vec!["image/png"]);
}

Detect the file type from a file synchronously:

use file_type::FileType;
use std::path::Path;

let file_path = Path::new("image.png");
let file_type = FileType::try_from_file_sync(file_path).expect("file type not found");
assert_eq!(file_type.id(), "fmt/11");
assert_eq!(file_type.name(), "Portable Network Graphics");
assert_eq!(file_type.extensions(), vec!["png"]);
assert_eq!(file_type.media_types(), vec!["image/png"]);

Feature flags

Name Description Default?
custom Enables custom file types Yes
httpd Enables Apache HTTPD file types No
linguist Enables Linguist file types No
pronom Enables PRONOM file types Yes
tokio Enables using tokio for async No
wikidata Enables Wikidata file types No

Supported File Types

List of supported file types

Safety

These crates use #![forbid(unsafe_code)] to ensure everything is implemented in 100% safe Rust.

License

Licensed under either of

AND

The PRONOM definitions are provided by The National Archives (UK) under the Open Government Licence.

Alternatives

If this crate does not meet your requirements, you may want to consider the following alternatives:

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

VSCode Development Container
GitHub Codespaces