9 releases
new 0.2.1 | Jan 8, 2025 |
---|---|
0.2.0 | Jan 7, 2025 |
0.1.0 | Jan 5, 2025 |
0.0.5 | Jan 4, 2025 |
#309 in Filesystem
573 downloads per month
Used in 4 crates
(via rsql_drivers)
770KB
3K
SLoC
file_type
Getting Started
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:
Example
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"]);
Supported File Types
Safety
This crate uses #![forbid(unsafe_code)]
to ensure everything is implemented in 100% safe Rust.
License
Licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or https://opensource.org/licenses/MIT)
AND
The PRONOM definitions are provided by The National Archives (UK) under the Open Government Licence.
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.
Dependencies
~6–12MB
~128K SLoC