Compare commits

...
Author SHA1 Message Date
FredApps 69f53c2f03 Detect image, audio, video and 3D file types with the shared craft-sniff table (#2862)
Part of storytold/craft-libs#778 (ArtCraft × image / audio / video type
detection).

`crates/lib/files/mimetypes` now names image, audio, video and 3D types
from `craft-sniff` instead of the third-party `infer` crate and the
local GLB/FBX/PLY matchers. Public functions stay the same. Pin:
craft-libs #1841 merge `070005125f0af1a5ae4a59002270e5849505bfc0`.

### Behaviour changes
- WAV and FLAC now return `audio/wav` and `audio/flac` (so the existing
`FileExtension` mappings apply).
- Fonts and office documents now come back unknown; `craft-sniff` has no
signatures for them, and this crate only reports types it can detect
from magic bytes.

### Checks (Linux, locally)
- `cargo clippy -p mimetypes --all-targets` and `cargo test -p mimetypes
--all-targets` (16 passed)


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

* **Improvements**
* File type detection now recognizes additional formats, including GLB,
binary FBX, PLY, MP4, WAV, and FLAC.
* Files with unrecognized content continue to use the generic
`application/octet-stream` type.
  * Missing-file and other file access errors are returned to callers.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-11-16 14:20:58 +01:00
8 changed files with 92 additions and 104 deletions
Generated
+6 -1
View File
@@ -1775,6 +1775,11 @@ dependencies = [
"libc",
]
[[package]]
name = "craft-sniff"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=070005125f0af1a5ae4a59002270e5849505bfc0#070005125f0af1a5ae4a59002270e5849505bfc0"
[[package]]
name = "craft-update"
version = "0.1.0"
@@ -5059,7 +5064,7 @@ name = "mimetypes"
version = "0.0.1"
dependencies = [
"anyhow",
"infer",
"craft-sniff",
"test_utils",
]
+2 -2
View File
@@ -15,8 +15,8 @@ path = "src/lib.rs"
[dependencies]
# External
infer = "0.19.0" # Mimetype detection
# Shared Craft libraries
craft-sniff = { git = "https://github.com/storytold/craft-libs", rev = "070005125f0af1a5ae4a59002270e5849505bfc0" } # File signature detection
[dev-dependencies]
test_utils = { path = "../../../testing/test_utils" }
@@ -1,78 +0,0 @@
use infer::Infer;
use std::sync::LazyLock;
pub (crate) static CUSTOM_INFER: LazyLock<Infer> = LazyLock::new(|| {
let mut infer = Infer::new();
add_custom_types(&mut infer);
infer
});
fn add_custom_types(infer: &mut Infer) {
infer.add("model/gltf-binary", "glb", gltf_matcher);
infer.add("model/fbx", "fbx", fbx_matcher);
infer.add("model/ply", "ply", ply_matcher);
}
fn gltf_matcher(buf: &[u8]) -> bool {
// Magic is 'glTF' (103 108 84 70)
// https://github.com/KhronosGroup/glTF/blob/main/extensions/1.0/Khronos/KHR_binary_glTF/README.md
buf.len() >= 4 && buf[0] == 103 && buf[1] == 108 && buf[2] == 84 && buf[3] == 70
}
fn ply_matcher(buf: &[u8]) -> bool {
// PLY files (ASCII and binary alike) start with the line "ply".
// https://paulbourke.net/dataformats/ply/
buf.len() >= 4
&& &buf[..3] == b"ply"
&& (buf[3] == b'\n' || buf[3] == b'\r')
}
fn fbx_matcher(buf: &[u8]) -> bool {
// Binary FBX magic: "Kaydara FBX Binary " (two trailing spaces).
// https://code.blender.org/2013/08/fbx-binary-file-format-specification/
// NB: ASCII FBX files have no magic and are not detected here.
const FBX_MAGIC: &[u8] = b"Kaydara FBX Binary ";
buf.len() >= FBX_MAGIC.len() && &buf[..FBX_MAGIC.len()] == FBX_MAGIC
}
#[cfg(test)]
mod tests {
use crate::custom::custom_infer::CUSTOM_INFER;
use test_utils::test_file_path::test_file_path;
#[test]
fn test_gltf() -> anyhow::Result<()> {
let path = test_file_path("test_data/3d/hanashi.glb")?;
let maybe_type = CUSTOM_INFER.get_from_path(path)?.unwrap();
assert_eq!(maybe_type.mime_type(), "model/gltf-binary");
Ok(())
}
#[test]
fn test_binary_fbx_magic() {
let mut bytes = b"Kaydara FBX Binary ".to_vec();
bytes.extend_from_slice(&[0x00, 0x1a, 0x00]);
let maybe_type = CUSTOM_INFER.get(&bytes).unwrap();
assert_eq!(maybe_type.mime_type(), "model/fbx");
}
#[test]
fn test_ply_magic() {
let bytes = b"ply\nformat binary_little_endian 1.0\n".to_vec();
let maybe_type = CUSTOM_INFER.get(&bytes).unwrap();
assert_eq!(maybe_type.mime_type(), "model/ply");
}
#[test]
fn test_ply_prefix_without_newline_is_not_detected() {
let bytes = b"plywood texture".to_vec();
assert!(CUSTOM_INFER.get(&bytes).is_none());
}
#[test]
fn test_ascii_fbx_is_not_detected() {
// ASCII FBX files have no magic bytes.
let bytes = b"; FBX 7.3.0 project file".to_vec();
assert!(CUSTOM_INFER.get(&bytes).is_none());
}
}
@@ -1 +0,0 @@
pub (crate) mod custom_infer;
-1
View File
@@ -19,7 +19,6 @@
#![allow(dead_code)]
#![allow(non_snake_case)]
pub (crate) mod custom;
pub mod mimetype_for_bytes;
pub mod mimetype_for_file;
pub mod mimetype_info;
@@ -1,14 +1,12 @@
const DEFAULT_BINARY_MIMETYPE : &str = "application/octet-stream";
pub fn get_mimetype_for_bytes(bytes: &[u8]) -> Option<&'static str> {
infer::get(bytes)
.map(|typ| typ.mime_type())
craft_sniff::identify(bytes)
.map(|kind| kind.mime_type())
}
pub fn get_mimetype_for_bytes_or_default(bytes: &[u8]) -> &'static str {
infer::get(bytes)
.map(|typ| typ.mime_type())
get_mimetype_for_bytes(bytes)
.unwrap_or(DEFAULT_BINARY_MIMETYPE)
}
@@ -38,4 +36,11 @@ mod tests {
assert_eq!(get_mimetype_for_bytes(&bytes), None);
assert_eq!(get_mimetype_for_bytes_or_default(&bytes), "application/octet-stream");
}
#[test]
fn test_mimetype_3d_models() {
assert_eq!(get_mimetype_for_bytes(b"glTF\x02\0\0\0"), Some("model/gltf-binary"));
assert_eq!(get_mimetype_for_bytes(b"Kaydara FBX Binary \0\x1a\0"), Some("model/fbx"));
assert_eq!(get_mimetype_for_bytes(b"ply\nformat ascii 1.0\n"), Some("model/ply"));
}
}
@@ -1,25 +1,35 @@
use std::fs::File;
use std::io::Read;
use std::path::Path;
use crate::mimetype_for_bytes::get_mimetype_for_bytes;
const DEFAULT_BINARY_MIMETYPE : &str = "application/octet-stream";
pub fn get_mimetype_for_file<P: AsRef<Path>>(file: P) -> std::io::Result<Option<&'static str>> {
infer::get_from_path(file)
.map(|maybe_type| maybe_type.map(|typ| typ.mime_type()))
let head = read_head(file)?;
Ok(get_mimetype_for_bytes(&head))
}
pub fn get_mimetype_for_file_or_default<P: AsRef<Path>>(file: P) -> std::io::Result<&'static str> {
get_mimetype_for_file(file)
.map(|maybe_type| match maybe_type {
Some(mimetype) => mimetype,
None => DEFAULT_BINARY_MIMETYPE,
})
.map(|maybe_type| maybe_type.unwrap_or(DEFAULT_BINARY_MIMETYPE))
}
/// Read the leading bytes signature detection looks at (all of them for a shorter file).
pub (crate) fn read_head<P: AsRef<Path>>(file: P) -> std::io::Result<Vec<u8>> {
let mut head = Vec::with_capacity(craft_sniff::HEAD);
File::open(file)?
.take(craft_sniff::HEAD as u64)
.read_to_end(&mut head)?;
Ok(head)
}
#[cfg(test)]
mod tests {
use std::path::PathBuf;
use crate::mimetype_for_file::get_mimetype_for_file;
use crate::mimetype_for_file::{get_mimetype_for_file, get_mimetype_for_file_or_default};
fn test_file(path_from_repo_root: &str) -> PathBuf {
// https://doc.rust-lang.org/cargo/reference/environment-variables.html
@@ -36,9 +46,23 @@ mod tests {
}
#[test]
fn test_mimetype_unknown() {
fn test_mimetype_mp4() {
let path = test_file("test_data/video/mp4/golden_sun_garoh.mp4");
assert_eq!(get_mimetype_for_file(&path).unwrap(), Some("video/mp4"));
}
#[test]
fn test_mimetype_or_default_for_unknown_file() {
let path = test_file("crates/lib/files/mimetypes/Cargo.toml");
assert_eq!(get_mimetype_for_file_or_default(&path).unwrap(), "application/octet-stream");
}
#[test]
fn test_missing_file_is_an_error() {
let path = test_file("test_data/does_not_exist.bin");
assert!(get_mimetype_for_file(&path).is_err());
}
}
@@ -1,6 +1,6 @@
use crate::custom::custom_infer::CUSTOM_INFER;
use crate::mimetype_for_file::read_head;
use crate::mimetype_info::file_extension::FileExtension;
use infer::Type;
use craft_sniff::Kind;
use std::io;
use std::path::Path;
@@ -15,17 +15,17 @@ pub struct MimetypeInfo {
impl MimetypeInfo {
pub fn get_for_bytes(bytes: &[u8]) -> Option<Self> {
CUSTOM_INFER.get(bytes)
.map(|t| Self::for_type(t))
craft_sniff::identify(bytes)
.map(Self::for_kind)
}
pub fn get_for_path<P: AsRef<Path>>(path: P) -> io::Result<Option<Self>> {
CUSTOM_INFER.get_from_path(path)
.map(|maybe_type| maybe_type.map(|t| Self::for_type(t)))
let head = read_head(path)?;
Ok(Self::get_for_bytes(&head))
}
fn for_type(r#type: Type) -> Self {
let mime_type = r#type.mime_type().to_string();
fn for_kind(kind: Kind) -> Self {
let mime_type = kind.mime_type().to_string();
MimetypeInfo {
file_extension: FileExtension::from_mimetype(&mime_type),
mime_type,
@@ -60,6 +60,40 @@ mod tests {
assert_eq!(ext.extension_with_period(), ".png");
assert_eq!(ext.extension_without_period(), "png");
}
#[test]
fn binary_fbx() {
let t = MimetypeInfo::get_for_bytes(b"Kaydara FBX Binary \x00\x1a\x00").expect("should detect bytes");
assert_eq!(t.mime_type(), "model/fbx");
assert_eq!(t.file_extension(), Some(FileExtension::Fbx));
}
#[test]
fn ply() {
let t = MimetypeInfo::get_for_bytes(b"ply\nformat binary_little_endian 1.0\n").expect("should detect bytes");
assert_eq!(t.mime_type(), "model/ply");
assert_eq!(t.file_extension(), Some(FileExtension::Ply));
}
#[test]
fn wav_and_flac_have_extensions() {
let mut wav = b"RIFF\x24\x00\x00\x00WAVEfmt ".to_vec();
wav.extend_from_slice(&[0; 8]);
let t = MimetypeInfo::get_for_bytes(&wav).expect("should detect bytes");
assert_eq!(t.mime_type(), "audio/wav");
assert_eq!(t.file_extension(), Some(FileExtension::Wav));
let t = MimetypeInfo::get_for_bytes(b"fLaC\x00\x00\x00\x22").expect("should detect bytes");
assert_eq!(t.mime_type(), "audio/flac");
assert_eq!(t.file_extension(), Some(FileExtension::Flac));
}
#[test]
fn signatureless_formats_are_not_detected() {
// ASCII FBX has no magic bytes, and "ply" must be its own line.
assert!(MimetypeInfo::get_for_bytes(b"; FBX 7.3.0 project file").is_none());
assert!(MimetypeInfo::get_for_bytes(b"plywood texture").is_none());
}
}
mod for_files {