A high-performance, platform-agnostic Rust library for file type identification based on magic numbers and binary signatures. This is the core library that powers various file identification frontends.
ufile-core provides reliable file type detection through:
- Magic number analysis: Custom database of file signatures for accurate detection
- Multi-file processing: Efficiently process multiple files and directories
- Fallback detection: Integration with the
infercrate for additional coverage - Cross-platform: Works on all platforms supported by Rust
This library is designed to be integrated into CLI tools, web applications (via WASM), desktop applications, and other software requiring file type identification.
ufile-core forms the foundational logic for my file identification projects:
ufile-cli: I've developed a native command-line interface (CLI) tool that directly utilizesufile-coreto identify files on your local system. You can find this project on GitHub: ufile-cli.ufile-wasm: This WebAssembly module bringsufile-core's capabilities to the web. I've integrated it into my personal website,rsomonte.github.io, to enable client-side file type detection directly in the browser. The source for this integration is housed within my website's repository: rsomonte.github.io.
Both the CLI and Wasm frontends integrate ufile-core as a direct Git dependency, ensuring they always use the exact same, version-controlled identification logic.
Add to your Cargo.toml:
[dependencies]
ufile-core = { git = "https://github.com/rsomonte/ufile-core.git", branch = "main" }The main result type containing file information:
pub struct FileInfo {
/// The path to the file
pub path: PathBuf,
/// Human-readable description of the file type
pub description: String,
/// Whether this entry represents a directory
pub is_directory: bool,
/// File size in bytes (None for directories)
pub size: Option<u64>,
}Comprehensive error handling for file operations:
pub enum FileProcessingError {
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[error("Path does not exist: {0}")]
PathNotFound(PathBuf),
#[error("Directory traversal error: {0}")]
WalkDir(#[from] walkdir::Error),
}- Memory efficient: Processes files without loading entire contents into memory when possible
- Lazy evaluation: Only reads file data when necessary for identification
- Batch processing: Efficiently handles multiple files with minimal system calls
- Early detection: Magic number matching stops at first successful identification
use ufile_core::*;
// Identify a single file from bytes
let bytes = std::fs::read("myfile.png")?;
let info = identify_from_bytes(&bytes);
println!("Type: {}", info.map_or("Unknown", |i| &i.description));
// Identify multiple files from bytes
let files: Vec<Vec<u8>> = vec![ /* ...file contents... */ ];
let results = identify_many_bytes(files.iter().map(|v| v.as_slice()));
for info in results {
println!("Type: {}", info.description);
}