mirror of
https://github.com/storytold/effectcraft.git
synced 2026-11-16 15:22:43 +00:00
Merge pull request #3643 from storytold/codex/effectcraft-dotlottie-container
Use shared ZIP codec for dotLottie archives
This commit is contained in:
8 files changed
+66
-132
No files matched your search
Generated
+12
-1
@@ -1194,8 +1194,9 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-lottie"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b425569506f4daa4faadac9#47107f660191de2f0b425569506f4daa4faadac9"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=2284a6bf76b3d629b5e5bb2ca71e1c319d8dae5f#2284a6bf76b3d629b5e5bb2ca71e1c319d8dae5f"
|
||||
dependencies = [
|
||||
"craft-zip",
|
||||
"serde_json",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
@@ -1374,6 +1375,16 @@ dependencies = [
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-zip"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=2284a6bf76b3d629b5e5bb2ca71e1c319d8dae5f#2284a6bf76b3d629b5e5bb2ca71e1c319d8dae5f"
|
||||
dependencies = [
|
||||
"flate2",
|
||||
"thiserror 2.0.21",
|
||||
"zip 9.1.3",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crc32fast"
|
||||
version = "1.5.2"
|
||||
|
||||
+1
-1
@@ -23,7 +23,7 @@ craft-gif = { version = "0.1.0", git = "https://github.com/storytold/craft-libs"
|
||||
craft-psd = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" }
|
||||
craft-svg = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" }
|
||||
craft-webp = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" }
|
||||
craft-lottie = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" }
|
||||
craft-lottie = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "2284a6bf76b3d629b5e5bb2ca71e1c319d8dae5f" }
|
||||
craft-mesh = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9", default-features = false, features = ["stl", "obj", "gltf", "fbx"] }
|
||||
craft-aep = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" }
|
||||
craft-doc = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" }
|
||||
|
||||
@@ -17,7 +17,7 @@ fn export_lottie(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let dot = path.to_ascii_lowercase().ends_with(".lottie");
|
||||
let bytes = if dot {
|
||||
let stem = std::path::Path::new(&path).file_stem().map(|n| n.to_string_lossy().to_string()).unwrap_or_default();
|
||||
effectcraft_lottie::to_dotlottie(&res, &stem)
|
||||
effectcraft_lottie::try_to_dotlottie(&res, &stem).map_err(|e| EngineError::Other(e.to_string()))?
|
||||
} else {
|
||||
res.to_string_compact().into_bytes()
|
||||
};
|
||||
|
||||
@@ -505,11 +505,17 @@ pub fn export_comp(project: &Project, comp: ItemId, opts: &ExportOptions, read:
|
||||
|
||||
/// Package an export as a dotLottie archive (`manifest.json` + `animations/<id>.json`).
|
||||
pub fn to_dotlottie(res: &ExportResult, id: &str) -> Vec<u8> {
|
||||
try_to_dotlottie(res, id).unwrap_or_default()
|
||||
}
|
||||
|
||||
/// Package an export, reporting serialization or ZIP failures before any output is written.
|
||||
pub fn try_to_dotlottie(res: &ExportResult, id: &str) -> Result<Vec<u8>, LottieError> {
|
||||
let id: String = id.chars().map(|c| if c.is_ascii_alphanumeric() || c == '_' || c == '-' { c } else { '_' }).collect();
|
||||
let id = if id.is_empty() { "animation".to_string() } else { id };
|
||||
let manifest = json!({"version": "1", "generator": "EffectCraft", "author": "", "animations": [{"id": id, "speed": 1, "loop": true}]});
|
||||
crate::zip::store(&[
|
||||
("manifest.json".into(), serde_json::to_vec(&manifest).unwrap_or_default()),
|
||||
(format!("animations/{id}.json"), serde_json::to_vec(&res.json).unwrap_or_default()),
|
||||
crate::zip::try_store(&[
|
||||
("manifest.json".into(), serde_json::to_vec(&manifest).map_err(|e| LottieError::Invalid(e.to_string()))?),
|
||||
(format!("animations/{id}.json"), serde_json::to_vec(&res.json).map_err(|e| LottieError::Invalid(e.to_string()))?),
|
||||
])
|
||||
.map_err(|e| LottieError::Invalid(format!("dotLottie ZIP: {e}")))
|
||||
}
|
||||
@@ -41,7 +41,7 @@ pub struct ImportResult {
|
||||
pub warnings: Vec<String>,
|
||||
}
|
||||
|
||||
/// The Lottie document inside raw bytes (plain JSON, or a stored dotLottie archive), read into
|
||||
/// The Lottie document inside raw bytes (plain JSON, or a dotLottie archive), read into
|
||||
/// `craft-lottie`'s model within its default limits.
|
||||
fn document(bytes: &[u8]) -> Result<Animation, LottieError> {
|
||||
let read = |b: &[u8]| {
|
||||
@@ -51,12 +51,12 @@ fn document(bytes: &[u8]) -> Result<Animation, LottieError> {
|
||||
})
|
||||
};
|
||||
if bytes.starts_with(b"PK") {
|
||||
let entries = crate::zip::read_stored(bytes);
|
||||
let entries = crate::zip::read_entries(bytes).map_err(|e| LottieError::Parse(format!("dotLottie ZIP: {e}")))?;
|
||||
let anim = entries
|
||||
.iter()
|
||||
.find(|(n, _)| n.starts_with("animations/") && n.ends_with(".json"))
|
||||
.or_else(|| entries.iter().find(|(n, _)| n.ends_with(".json") && n != "manifest.json"))
|
||||
.ok_or_else(|| LottieError::Parse("dotLottie archive without an uncompressed animation".into()))?;
|
||||
.ok_or_else(|| LottieError::Parse("dotLottie archive without a JSON animation".into()))?;
|
||||
return read(&anim.1);
|
||||
}
|
||||
read(bytes)
|
||||
@@ -102,7 +102,7 @@ fn external_image(stored: &str, base_dir: &str) -> Result<String, String> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Import a Lottie document (JSON or a stored `.lottie` archive) into `project` as a new
|
||||
/// Import a Lottie document (JSON or a `.lottie` archive) into `project` as a new
|
||||
/// composition. `store` saves embedded images and returns the file path to use;
|
||||
/// `base_dir` resolves external image references.
|
||||
pub fn import(
|
||||
|
||||
@@ -30,7 +30,7 @@ pub mod zip;
|
||||
use effectcraft_color::BlendMode;
|
||||
use effectcraft_time::{FrameRate, TICKS_PER_SECOND, Tick};
|
||||
|
||||
pub use export::{ExportOptions, ExportResult, export_comp, to_dotlottie};
|
||||
pub use export::{ExportOptions, ExportResult, export_comp, to_dotlottie, try_to_dotlottie};
|
||||
pub use import::{ImportResult, ImportedImage, import};
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
|
||||
@@ -12,7 +12,7 @@ use effectcraft_render::render_frame;
|
||||
use effectcraft_time::{FrameRate, Tick};
|
||||
use serde_json::{Value as Json, json};
|
||||
|
||||
use crate::{ExportOptions, export_comp, import};
|
||||
use crate::{ExportOptions, LottieError, export_comp, import};
|
||||
|
||||
fn s(x: f64) -> Tick {
|
||||
Tick::from_seconds_f64(x)
|
||||
@@ -799,6 +799,32 @@ fn dotlottie_archive_roundtrip() {
|
||||
assert_eq!(q.comp(imp.comp).unwrap().layers.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dotlottie_archive_without_animation_is_rejected() {
|
||||
let zip = craft_lottie::zip::try_store(&[("manifest.json".into(), b"{}".to_vec())]).unwrap();
|
||||
let mut project = Project::default();
|
||||
let err = import(&mut project, &zip, "empty archive", "", &mut |_| None).unwrap_err();
|
||||
assert!(matches!(err, LottieError::Parse(message) if message.contains("without a JSON animation")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deflated_dotlottie_archive_imports() {
|
||||
let (mut p, cid) = setup();
|
||||
let l = solid_layer(&mut p, cid, [0.0, 1.0, 0.0], 40, 40);
|
||||
push(&mut p, cid, l);
|
||||
let res = export_comp(&p, cid, &ExportOptions::default(), &no_read).unwrap();
|
||||
let manifest = serde_json::to_vec(&serde_json::json!({"version":"1","animations":[{"id":"main"}]})).unwrap();
|
||||
let animation = serde_json::to_vec(&res.json).unwrap();
|
||||
let zip = craft_lottie::zip::try_store_with_compression(
|
||||
&[("manifest.json".into(), manifest), ("animations/main.json".into(), animation)],
|
||||
craft_lottie::zip::Compression::Deflated,
|
||||
)
|
||||
.unwrap();
|
||||
let mut q = Project::default();
|
||||
let imp = import(&mut q, &zip, "compressed", "", &mut |_| None).unwrap();
|
||||
assert_eq!(q.comp(imp.comp).unwrap().layers.len(), 1);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- hand-written fixtures
|
||||
|
||||
fn fixture(name: &str) -> (Project, ItemId, Vec<String>) {
|
||||
@@ -1062,3 +1088,12 @@ fn only_image_bytes_are_embedded() {
|
||||
assert!(res.json["assets"].as_array().is_none_or(|a| a.is_empty()), "{}", res.json["assets"]);
|
||||
assert!(res.warnings.iter().any(|w| w.contains("not a PNG")), "{:?}", res.warnings);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dotlottie_export_rejects_an_unrepresentable_entry_name() {
|
||||
let (p, cid) = setup();
|
||||
let res = export_comp(&p, cid, &ExportOptions::default(), &no_read).unwrap();
|
||||
let err = crate::try_to_dotlottie(&res, &"a".repeat(usize::from(u16::MAX))).unwrap_err();
|
||||
assert!(matches!(err, LottieError::Invalid(message) if message.contains("dotLottie ZIP")));
|
||||
assert_eq!(crate::try_to_dotlottie(&res, "Main").unwrap(), crate::to_dotlottie(&res, "Main"));
|
||||
}
|
||||
+2
-120
@@ -1,121 +1,3 @@
|
||||
//! A minimal ZIP writer (stored entries, PKWARE APPNOTE 6.3 local headers + central directory)
|
||||
//! for `.lottie` (dotLottie) archives.
|
||||
//! Shared stored-entry ZIP functions used by dotLottie imports and exports.
|
||||
|
||||
fn crc32(data: &[u8]) -> u32 {
|
||||
let mut crc = 0xFFFF_FFFFu32;
|
||||
for &b in data {
|
||||
crc ^= b as u32;
|
||||
for _ in 0..8 {
|
||||
crc = if crc & 1 != 0 { (crc >> 1) ^ 0xEDB8_8320 } else { crc >> 1 };
|
||||
}
|
||||
}
|
||||
!crc
|
||||
}
|
||||
|
||||
/// Build a ZIP archive with the given (name, bytes) entries, uncompressed.
|
||||
pub fn store(entries: &[(String, Vec<u8>)]) -> Vec<u8> {
|
||||
let mut out = Vec::new();
|
||||
let mut central = Vec::new();
|
||||
let put16 = |v: &mut Vec<u8>, x: u16| v.extend_from_slice(&x.to_le_bytes());
|
||||
let put32 = |v: &mut Vec<u8>, x: u32| v.extend_from_slice(&x.to_le_bytes());
|
||||
for (name, data) in entries {
|
||||
let offset = out.len() as u32;
|
||||
let crc = crc32(data);
|
||||
let n = name.as_bytes();
|
||||
// Local file header.
|
||||
put32(&mut out, 0x0403_4b50);
|
||||
put16(&mut out, 20); // version needed
|
||||
put16(&mut out, 0x0800); // flags: UTF-8 names
|
||||
put16(&mut out, 0); // stored
|
||||
put16(&mut out, 0); // time
|
||||
put16(&mut out, 0x21); // date (1980-01-01)
|
||||
put32(&mut out, crc);
|
||||
put32(&mut out, data.len() as u32);
|
||||
put32(&mut out, data.len() as u32);
|
||||
put16(&mut out, n.len() as u16);
|
||||
put16(&mut out, 0);
|
||||
out.extend_from_slice(n);
|
||||
out.extend_from_slice(data);
|
||||
// Central directory record.
|
||||
put32(&mut central, 0x0201_4b50);
|
||||
put16(&mut central, 20); // made by
|
||||
put16(&mut central, 20);
|
||||
put16(&mut central, 0x0800);
|
||||
put16(&mut central, 0);
|
||||
put16(&mut central, 0);
|
||||
put16(&mut central, 0x21);
|
||||
put32(&mut central, crc);
|
||||
put32(&mut central, data.len() as u32);
|
||||
put32(&mut central, data.len() as u32);
|
||||
put16(&mut central, n.len() as u16);
|
||||
put16(&mut central, 0); // extra
|
||||
put16(&mut central, 0); // comment
|
||||
put16(&mut central, 0); // disk
|
||||
put16(&mut central, 0); // internal attrs
|
||||
put32(&mut central, 0); // external attrs
|
||||
put32(&mut central, offset);
|
||||
central.extend_from_slice(n);
|
||||
}
|
||||
let cd_offset = out.len() as u32;
|
||||
let cd_len = central.len() as u32;
|
||||
out.extend_from_slice(¢ral);
|
||||
put32(&mut out, 0x0605_4b50);
|
||||
put16(&mut out, 0);
|
||||
put16(&mut out, 0);
|
||||
put16(&mut out, entries.len() as u16);
|
||||
put16(&mut out, entries.len() as u16);
|
||||
put32(&mut out, cd_len);
|
||||
put32(&mut out, cd_offset);
|
||||
put16(&mut out, 0);
|
||||
out
|
||||
}
|
||||
|
||||
/// Whether an entry name is a relative path inside the archive: not absolute (`/…`, `\…`), no
|
||||
/// drive (`C:…`) and no `..` component.
|
||||
fn is_relative_name(name: &str) -> bool {
|
||||
let drive = name.as_bytes().get(1) == Some(&b':');
|
||||
!name.starts_with(['/', '\\']) && !drive && !name.split(['/', '\\']).any(|c| c == "..")
|
||||
}
|
||||
|
||||
/// Read the stored (uncompressed) entries of a ZIP archive written by [`store`] or any other
|
||||
/// writer that did not compress. Compressed entries and entries whose names are not relative
|
||||
/// paths inside the archive are skipped.
|
||||
pub fn read_stored(data: &[u8]) -> Vec<(String, Vec<u8>)> {
|
||||
let mut out = vec![];
|
||||
let mut i = 0usize;
|
||||
let u16_at = |p: usize| data.get(p..p + 2).map(|b| u16::from_le_bytes([b[0], b[1]]) as usize);
|
||||
let u32_at = |p: usize| data.get(p..p + 4).map(|b| u32::from_le_bytes([b[0], b[1], b[2], b[3]]) as usize);
|
||||
while u32_at(i) == Some(0x0403_4b50) {
|
||||
let (Some(method), Some(size), Some(nlen), Some(xlen)) = (u16_at(i + 8), u32_at(i + 18), u16_at(i + 26), u16_at(i + 28)) else { break };
|
||||
let name_start = i + 30;
|
||||
let body = name_start + nlen + xlen;
|
||||
let (Some(name), Some(bytes)) = (data.get(name_start..name_start + nlen), data.get(body..body + size)) else { break };
|
||||
let name = String::from_utf8_lossy(name).to_string();
|
||||
if method == 0 && is_relative_name(&name) {
|
||||
out.push((name, bytes.to_vec()));
|
||||
}
|
||||
i = body + size;
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn crc_and_roundtrip() {
|
||||
assert_eq!(super::crc32(b"123456789"), 0xCBF4_3926);
|
||||
let z = super::store(&[("a.json".into(), b"{}".to_vec()), ("b/c.txt".into(), b"hello".to_vec())]);
|
||||
let back = super::read_stored(&z);
|
||||
assert_eq!(back.len(), 2);
|
||||
assert_eq!(back[1].0, "b/c.txt");
|
||||
assert_eq!(back[1].1, b"hello");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn entries_outside_the_archive_root_are_skipped() {
|
||||
let names = ["../up.txt", "a/../../up.txt", "/abs/x", "\\abs\\x", "C:\\x", "c:x", "a\\..\\x", "ok/x.txt", "ok/..x"];
|
||||
let z = super::store(&names.iter().map(|n| (n.to_string(), b"x".to_vec())).collect::<Vec<_>>());
|
||||
let back: Vec<String> = super::read_stored(&z).into_iter().map(|(n, _)| n).collect();
|
||||
assert_eq!(back, ["ok/x.txt", "ok/..x"]);
|
||||
}
|
||||
}
|
||||
pub use craft_lottie::zip::{Limits, read_entries, read_entries_with_limits, read_stored, store, try_store, try_store_with_compression};
|
||||
Reference in new issue
Block a user