Crates.io | file_type |
lib.rs | file_type |
version | |
source | src |
created_at | 2025-01-02 20:24:35.846982 |
updated_at | 2025-01-08 19:13:18.075195 |
description | File type detector |
homepage | |
repository | https://github.com/theseus-rs/file-type |
max_upload_size | |
id | 1501844 |
Cargo.toml error: | TOML parse error at line 19, column 1 | 19 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
File types are determined by examining a file or bytes against known file signatures and file extensions.
Signature, extension and media type data are provided by:
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"]);
This crate uses #![forbid(unsafe_code)]
to ensure everything is implemented in 100% safe Rust.
Licensed under either of
AND
The PRONOM definitions are provided by The National Archives (UK) under the Open Government Licence.
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.