Skip to content

rsomonte/ufile-core

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ufile-core

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.

Overview

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 infer crate 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.

Where I've Used It

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 utilizes ufile-core to identify files on your local system. You can find this project on GitHub: ufile-cli.
  • ufile-wasm: This WebAssembly module brings ufile-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.

Untitled diagram _ Mermaid Chart-2025-07-30-011157

Installation

Add to your Cargo.toml:

[dependencies]
ufile-core = { git = "https://github.com/rsomonte/ufile-core.git", branch = "main" }

Data Types

FileInfo

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>,
}

FileProcessingError

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),
}

Performance Considerations

  • 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

Simple Usage

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);
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages