Qubit MIME
MIME type detection utilities for Rust services based on filename glob rules and content magic rules.
Overview
Qubit MIME is a repository-backed MIME type detector for Rust. It uses the freedesktop shared MIME-info data model: canonical MIME type names, aliases, localized comments, filename globs, content magic rules, and super-type relationships.
The public surface is organized into three layers:
MimeDetector: the top-level detector trait. Use it when code should work with any detector implementation.detector: detector implementations and shared detector logic, includingMimeDetectorCore,MimeDetectorBackend,RepositoryMimeDetector, andFileCommandMimeDetector.MediaStreamClassifier: the top-level media stream classifier trait. Theclassifiermodule providesFfprobeCommandMediaStreamClassifier,MediaStreamClassifierBackend, andFileBasedMediaStreamClassifierfor implementing stream-backed or file-backed classifiers with less duplicated entry-point code.MimeRepository: the lower-level repository returningMimeTypemetadata and all matching candidates when callers need richer inspection.
Design Goals
- Freedesktop data model: follow shared MIME-info names, aliases, glob rules, and magic rules.
- Practical defaults: ship with an embedded freedesktop MIME database.
- Filename and content detection: support glob-based and magic-based detection, independently or together.
- Detector and classifier hierarchy: keep MIME detection and media stream refinement separate with Rust ownership and error handling.
- Predictable conflict resolution: prefer higher glob weights, longer glob patterns, and higher magic priorities.
- Rust-friendly API: use borrowed repositories, concrete errors, and
standard
Read + Seekbased detection. - Small dependency surface: keep runtime dependencies focused and stable.
Features
Filename Detection
- Literal, extension, and general glob matching.
- Case-insensitive matching by default, with support for case-sensitive globs.
- Conflict resolution by glob weight and pattern length.
- Path-safe detection that only uses the final filename component.
Content Magic Detection
- Freedesktop magic value types:
string,byte,host16,host32,big16,big32,little16, andlittle32. - Offset ranges such as
0and0:1024. - Optional masks for binary magic values.
- Nested magic matchers.
- Conflict resolution by magic priority.
Repository Metadata
- Canonical names and aliases.
- Localized comments and descriptions.
- Preferred and complete filename extension lookup.
- Super-type metadata parsed from
sub-class-ofentries. - Maximum byte count required by magic rules.
Detection Entrypoints
- Top-level trait:
MimeDetector. - Repository detector:
RepositoryMimeDetector. - System command detector:
FileCommandMimeDetector. - Filename only:
detect_by_filename. - Content only:
detect_by_content. - Combined filename and bytes:
detectordetect_bytes. - Combined filename and reader:
detect_reader. - Local file path:
detect_file.
Media Stream Classification
- Top-level trait:
MediaStreamClassifier. - Stream result enum:
MediaStreamType. - FFprobe-backed implementation:
FfprobeCommandMediaStreamClassifier. - Precise refinement for ambiguous media types such as WebM and Ogg when a
classifier is configured on
MimeDetectorCore.
Installation
Add this to your Cargo.toml:
[]
= "0.2"
Quick Start
Detect from filename and content
use ;
Use the Rust-style MimeDetector trait
BoxMimeDetector and ArcMimeDetector provide explicit boxed and shared
detector containers selected from MimeConfig and MimeDetectorRegistry. Code
that only needs MIME names can depend on the trait instead of a concrete
detector.
use ;
Configure global defaults with Config
MimeConfig::default() returns a snapshot of the current global default MIME
configuration. Use MimeConfig::reload_default() to replace it from an
rs-config Config, or MimeConfig::reload_default_from_env() to load from
Config::from_env().
use Config;
use ;
Select detectors with registry and fallbacks
BoxMimeDetector::from_config() and ArcMimeDetector::from_config() use the
process-wide default MimeDetectorRegistry. The configured default detector is tried
first. If that provider is unknown, unavailable, or fails to initialize, the
configured fallback chain is tried in order. Set the default selector to auto
to choose the highest-priority available provider from the registry.
The default registry starts with the built-in providers returned by
MimeDetectorRegistry::builtin(). Extension crates can make their providers
available to all default detector constructors by calling
MimeDetectorRegistry::register_default(provider) during application startup.
After registration succeeds, every later call to BoxMimeDetector::from_config(),
BoxMimeDetector::from_name(), ArcMimeDetector::from_config(), or
ArcMimeDetector::from_name() sees the provider through the same process-wide
registry.
MimeDetectorRegistry::default_registry() returns a snapshot clone of the
current process-wide registry. Mutating that snapshot does not update the global
registry. Use register_default() when a provider should become globally
visible, and use an explicit MimeDetectorRegistry::builtin() or
MimeDetectorRegistry::new() when a caller needs isolation, tests a custom
provider, or wants to restrict which providers can be selected.
Global registration is process-local and should normally happen once, before
creating detectors from configuration. Duplicate provider ids or aliases are
reported as MimeError::DuplicateDetectorName; poisoned global registry locks
are reported as MimeError::DetectorBackend.
Built-in detector selectors:
| Selector | Aliases | Behavior |
|---|---|---|
repository |
repo, repository-mime-detector |
Uses the embedded freedesktop MIME repository |
file |
file-command, file-command-mime-detector |
Uses the repository for filenames and file --mime-type --brief for local content |
auto |
- | Chooses available providers by priority, then provider id |
use Config;
use ;
Use an explicit registry when you need custom providers:
use ;
Registry selection uses these error categories:
| Error | Meaning |
|---|---|
DuplicateDetectorName |
A provider id or alias conflicts with an existing provider |
UnknownDetector |
No registered provider matches the requested selector |
DetectorUnavailable |
The provider exists but its backend is not available in this process environment |
NoAvailableDetector |
Every default or fallback candidate failed |
DetectorBackend |
A provider backend failed while initializing or detecting |
Configuration keys
MimeConfig::from_config() accepts both logical keys and environment-style keys.
Environment variables use the environment-style names. List values can be arrays
or scalar strings split on , and ;; empty items are ignored. Ambiguous MIME
mapping values are split on ; as extension:video-mime,audio-mime.
| Setting | Logical key | Environment key | Default |
|---|---|---|---|
| Default MIME detector | mime.detector.default |
QUBIT_MIME_DETECTOR_DEFAULT |
repository |
| MIME detector fallbacks | mime.detector.fallbacks |
QUBIT_MIME_DETECTOR_FALLBACKS |
empty |
| Media stream classifier | mime.media.stream.classifier.default |
QUBIT_MEDIA_STREAM_CLASSIFIER_DEFAULT |
ffprobe |
| Precise detection enabled | mime.enable.precise.detection |
QUBIT_MIME_ENABLE_PRECISE_DETECTION |
true |
| Precise detection patterns | mime.precise.detection.patterns |
QUBIT_MIME_PRECISE_DETECTION_PATTERNS |
webm,ogg |
| Ambiguous MIME mapping | mime.ambiguous.mime.mapping |
QUBIT_MIME_AMBIGUOUS_MIME_MAPPING |
webm:video/webm,audio/webm;ogg:video/ogg,audio/ogg |
Detect a filesystem path
use ;
Use the system file command detector
FileCommandMimeDetector uses the embedded repository for filename candidates
and file --mime-type --brief for content detection.
use Duration;
use CommandRunner;
use ;
Classify media streams with FFprobe
FfprobeCommandMediaStreamClassifier classifies a media file as no media,
audio-only, video-only, or video with audio.
use Path;
use ;
Detect from a seekable reader
detect_reader reads up to the repository's required magic byte count and then
restores the original stream position.
use ;
use ;
Combined Detection Strategy
Combined detection accepts both a filename and content bytes. The
MimeDetectionPolicy value makes the filename/content resolution strategy
explicit at each call site.
use ;
Use MimeDetectionPolicy::PreferFilename when filenames come from a trusted
source and you want less I/O. Use MimeDetectionPolicy::VerifyContent when
uploaded or user-controlled filenames may be wrong or misleading.
Repository Metadata
The default detector exposes the parsed repository. Use it when you need metadata instead of just a MIME name.
use ;
MimeRepository::detect_by_filename and MimeRepository::detect_by_content
return all best candidates instead of a single string:
use ;
Custom Repository
Use MimeRepository::from_xml when you need a small test repository, a product
specific MIME database, or a database generated from another source.
use ;
Low-Level Rule Types
The low-level types are useful in tests and integrations that need to inspect or validate MIME rules directly.
use ;
API Reference
MimeDetector
| Method | Description |
|---|---|
MimeDetectorRegistry::builtin() |
Create an isolated registry with only built-in detector providers |
MimeDetectorRegistry::default_registry() |
Snapshot the process-wide default detector registry |
MimeDetectorRegistry::register_default(provider) |
Register an external detector provider globally |
MimeDetectorRegistry::register(provider) |
Register an external detector provider in an explicit registry |
MimeDetectorRegistry::create(name, config) |
Create one detector by provider id or alias |
MimeDetectorRegistry::create_default(config) |
Resolve the configured default, auto, and fallback chain |
MimeDetectorProvider |
Factory trait for pluggable detector implementations |
BoxMimeDetector::from_config(config) |
Select a boxed detector from the default registry |
BoxMimeDetector::from_registry(registry, config) |
Select a boxed detector from an explicit registry |
BoxMimeDetector::from_name(name) |
Select a boxed detector from the default registry by implementation name |
ArcMimeDetector::from_config(config) |
Select a shared detector from the default registry |
ArcMimeDetector::from_registry(registry, config) |
Select a shared detector from an explicit registry |
ArcMimeDetector::from_name(name) |
Select a shared detector from the default registry by implementation name |
detect_by_filename(filename) |
Detect one MIME name from filename |
detect_by_content(bytes) |
Detect one MIME name from content bytes |
detect(bytes, filename, policy) |
Detect from bytes and optional filename |
RepositoryMimeDetector
| Method | Description |
|---|---|
new() |
Create a detector backed by the embedded freedesktop repository |
with_repository(repository) |
Create a detector borrowing an explicit repository |
with_repository_and_config(repository, config) |
Create a detector borrowing an explicit repository and MIME configuration |
repository() |
Borrow the underlying repository |
detect_by_filename(filename) |
Return the first MIME name matched by filename |
detect_by_content(bytes) |
Return the first MIME name matched by content magic |
detect_bytes(bytes, filename, policy) |
Detect from bytes and optional filename |
detect_reader(reader, filename, policy) |
Detect from a Read + Seek reader and restore its position |
detect_file(file, policy) |
Open and detect a local file path |
FileCommandMimeDetector
| Method | Description |
|---|---|
new() |
Create a detector backed by the embedded repository and the system file command |
with_repository(repository) |
Create a detector borrowing an explicit repository |
with_repository_and_runner(repository, runner) |
Create a detector with an explicit qubit_command::CommandRunner |
with_repository_runner_and_config(repository, runner, config) |
Create a detector with explicit repository, runner, and MIME configuration |
command_runner() |
Borrow the runner used for command execution |
set_command_runner(runner) |
Replace the runner used for command execution |
is_available() |
Check whether the file command can be executed |
detect_file_by_content(file) |
Detect a local file using command output only |
detect_file(file, policy) |
Detect a local file by filename and command-backed content inspection |
detect_reader(reader, filename, policy) |
Detect a seekable reader through the file-backed path |
MediaStreamClassifier
| Method | Description |
|---|---|
BoxMediaStreamClassifier::default() |
Select the configured/default boxed classifier |
BoxMediaStreamClassifier::from_name(name) |
Select a boxed classifier by implementation name |
BoxMediaStreamClassifier::from_config(config) |
Select a boxed classifier from explicit MIME configuration |
ArcMediaStreamClassifier::default() |
Select the configured/default shared classifier |
ArcMediaStreamClassifier::from_name(name) |
Select a shared classifier by implementation name |
ArcMediaStreamClassifier::from_config(config) |
Select a shared classifier from explicit MIME configuration |
classify_file(file) |
Classify a local media file |
classify_reader(reader) |
Classify media content from a reader |
classify_content(bytes) |
Classify in-memory media content |
FfprobeCommandMediaStreamClassifier
| Method | Description |
|---|---|
new() |
Create an FFprobe-backed classifier |
is_available() |
Check whether ffprobe can be executed |
classify_stream_listing(output) |
Classify parsed FFprobe codec_type output |
set_working_directory(directory) |
Set the command working directory |
MimeRepository
| Method | Description |
|---|---|
from_xml(xml) |
Parse a freedesktop shared MIME-info XML document |
empty() |
Create an empty repository |
all() |
Return all parsed MIME types in database order |
get(name) |
Resolve a canonical MIME name or alias |
max_test_bytes() |
Return the maximum byte prefix needed by magic rules |
detect_by_filename(filename) |
Return best filename candidates |
detect_by_content(bytes) |
Return best content candidates |
detect(filename, bytes, policy) |
Merge filename and content detection |
Metadata and Rule Types
| Type | Purpose |
|---|---|
MimeType |
Metadata and rules for one MIME type |
MimeTypeBuilder |
Builder for standalone MimeType values |
MimeGlob |
Filename glob rule with weight and case-sensitivity |
MimeMagic |
Priority-ranked collection of magic matchers |
MimeMagicMatcher |
One magic matcher with offset, value, mask, and children |
MagicValueType |
Freedesktop magic value type enum |
MediaStreamType |
Audio/video stream classification result |
MimeConfig |
Precise detection and ambiguous media mapping configuration |
MimeError |
Error type for XML parsing, rule validation, and I/O |
MimeConfig
| Method | Description |
|---|---|
from_config(config) |
Parse MIME configuration from a qubit_config::Config |
from_env() |
Parse MIME configuration from Config::from_env() |
default() |
Clone the current global default MIME configuration |
set_default(config) |
Replace the global default used by future default instances |
reload_default(config) |
Parse and replace the global default from a Config |
reload_default_from_env() |
Parse and replace the global default from process environment |
mime_detector_default() |
Read the configured detector selector |
mime_detector_fallbacks() |
Read the configured detector fallback chain |
media_stream_classifier_default() |
Read the configured media classifier selector |
Module Layout
The source layout is grouped by detector, classifier, and repository concerns:
src/
mime_detector.rs # top-level MimeDetector trait
mime_config.rs # precise detection configuration
detector/ # detector implementations
classifier/ # media stream classifier interface and implementations
repository/ # MIME database, glob, magic, and metadata types
Use the root re-exports for normal application code. Use the nested modules when you need to inspect or extend a specific detector, classifier, or repository component.
Detection Rules
Filename Rules
Filename detection uses only the final path component. Matches are ranked by:
- Higher glob weight.
- Longer glob pattern when weights tie.
- Database order when the repository returns multiple equal candidates.
Content Rules
Content detection checks the provided byte prefix against each MIME type's
direct magic rules. Matches are ranked by higher magic priority. Use
repository.max_test_bytes() to know the largest useful prefix length for a
repository.
Combined Rules
Combined detection first evaluates filename globs. When there is exactly one filename match and magic checking is not forced, that filename match is used. Otherwise, content magic is evaluated and merged with filename candidates.
Comparison with Java common-mime
| Aspect | Java common-mime |
Qubit MIME |
|---|---|---|
| Database model | Freedesktop shared MIME-info | Same model |
| Filename detection | Glob rules | Glob rules |
| Content detection | Magic rules | Magic rules |
| Alias lookup | Supported | Supported |
| Detector interface | MimeDetector |
MimeDetector trait |
| Media stream classifier | MediaStreamClassifier |
MediaStreamClassifier trait |
| Repository detector | RepositoryMimeDetector |
RepositoryMimeDetector |
| File command detector | FileCommandMimeDetector |
FileCommandMimeDetector |
| FFprobe classifier | FfprobeCommandMediaStreamClassifier |
FfprobeCommandMediaStreamClassifier |
| Repository loading | XML resource | Embedded XML or explicit XML |
| Return style | Java objects and collections | Rust Option, slices, and vectors |
| Errors | Java exceptions | Concrete MimeError |
Testing & Code Coverage
This project keeps tests under tests/ and validates repository parsing,
filename matching, content magic matching, reader/path detection, and coverage
thresholds.
Running Tests
# Run all tests
# Generate a coverage report
# Generate a text format coverage report
# Run CI checks (format, clippy, tests, docs, coverage, audit)
Dependencies
Runtime dependencies are intentionally small:
qubit-commandruns externalfilecommands with timeout and output capture.qubit-configloads MIME defaults from configuration objects and environment.regexcompiles and runs filename glob matchers.roxmltreeparses shared MIME-info XML.thiserrorprovides the concreteMimeErrorimplementation.
License
Copyright (c) 2026. Haixing Hu, Qubit Co. Ltd. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
See LICENSE for the full license text.
Contributing
Contributions are welcome. Please keep changes aligned with the existing Rust
project structure and run ./ci-check.sh before opening a pull request.
Author
Haixing Hu - Qubit Co. Ltd.
Related Projects
More Rust libraries from Qubit are published under the qubit-ltd GitHub organization.
Repository: https://github.com/qubit-ltd/rs-mime