mirror of
https://github.com/storytold/artcraft.git
synced 2026-11-16 12:05:26 -05:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
69f53c2f03 |
@@ -96,7 +96,3 @@ Organize for top-to-bottom reading. Important things first, details later.
|
||||
|
||||
Not: `| Model | Configuration | Credits | Speed | Score |` packed tight
|
||||
with varying widths per row.
|
||||
|
||||
## Shared Craft libraries
|
||||
|
||||
Before adding or changing reusable file-format, codec, protocol or UI code, check [craft-libs](https://github.com/storytold/craft-libs) and existing contributor work; reuse its public APIs or extend the appropriate shared library instead of adding an app-local duplicate, keeping app-specific policy and model mapping local and preserving compatibility, security limits and regression coverage.
|
||||
|
||||
Generated
+6
-1
@@ -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",
|
||||
]
|
||||
|
||||
|
||||
@@ -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;
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user