From 6963e0d5ce7d08c1166f947cff750aebfd74ec24 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Mon, 16 Nov 2026 21:58:13 -0500 Subject: [PATCH] Use shared Sketch container while retaining Fig document projection (#2418) --- Cargo.lock | 37 +++++- Cargo.toml | 1 + crates/format/Cargo.toml | 1 + crates/format/src/sketch.rs | 184 ++---------------------------- crates/format/src/sketch/tests.rs | 18 +++ 5 files changed, 67 insertions(+), 174 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d6d010579..083d59554 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1016,7 +1016,7 @@ name = "craft-fig" version = "0.1.0" source = "git+https://github.com/storytold/craft-libs?rev=d13ac58a15c45d618f25910772502e38c4ce1d4c#d13ac58a15c45d618f25910772502e38c4ce1d4c" dependencies = [ - "craft-figma-rest", + "craft-figma-rest 0.1.0 (git+https://github.com/storytold/craft-libs?rev=d13ac58a15c45d618f25910772502e38c4ce1d4c)", "craft-kiwi", "craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=d13ac58a15c45d618f25910772502e38c4ce1d4c)", "flate2", @@ -1048,6 +1048,16 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-figma-rest" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=ff142b3ad58aa7306676749428f4f2f9b277b5dd#ff142b3ad58aa7306676749428f4f2f9b277b5dd" +dependencies = [ + "serde", + "serde_json", + "thiserror 2.0.21", +] + [[package]] name = "craft-font-catalog" version = "0.1.0" @@ -1153,6 +1163,18 @@ dependencies = [ "toml_edit", ] +[[package]] +name = "craft-sketch" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=ff142b3ad58aa7306676749428f4f2f9b277b5dd#ff142b3ad58aa7306676749428f4f2f9b277b5dd" +dependencies = [ + "craft-figma-rest 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ff142b3ad58aa7306676749428f4f2f9b277b5dd)", + "craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ff142b3ad58aa7306676749428f4f2f9b277b5dd)", + "kurbo", + "serde_json", + "thiserror 2.0.21", +] + [[package]] name = "craft-ui" version = "0.1.0" @@ -1265,6 +1287,16 @@ dependencies = [ "zip 9.1.1", ] +[[package]] +name = "craft-zip" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=ff142b3ad58aa7306676749428f4f2f9b277b5dd#ff142b3ad58aa7306676749428f4f2f9b277b5dd" +dependencies = [ + "flate2", + "thiserror 2.0.21", + "zip 9.1.1", +] + [[package]] name = "crc32fast" version = "1.5.3" @@ -2016,7 +2048,8 @@ version = "0.18.1" dependencies = [ "craft-fig", "craft-figcraft-format", - "craft-figma-rest", + "craft-figma-rest 0.1.0 (git+https://github.com/storytold/craft-libs?rev=d13ac58a15c45d618f25910772502e38c4ce1d4c)", + "craft-sketch", "figcraft-doc", "figcraft-layout", "figcraft-pathops", diff --git a/Cargo.toml b/Cargo.toml index 05aa2d393..57cc46f7a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -61,6 +61,7 @@ craft-settings = { git = "https://github.com/storytold/craft-libs", rev = "e11f9 # The design-file formats (craft-libs): the .fig container and its kiwi encoding, the mapping of # a .fig message onto the REST file JSON, and that JSON's shared helpers. These crates share # types, so they stay on one rev; they share none with the crates above. +craft-sketch = { git = "https://github.com/storytold/craft-libs", rev = "ff142b3ad58aa7306676749428f4f2f9b277b5dd" } craft-fig = { git = "https://github.com/storytold/craft-libs", rev = "d13ac58a15c45d618f25910772502e38c4ce1d4c" } craft-figcraft-format = { git = "https://github.com/storytold/craft-libs", rev = "976eb51c59edb0a37f24105850f52d921c8518ee" } craft-figma-rest = { git = "https://github.com/storytold/craft-libs", rev = "d13ac58a15c45d618f25910772502e38c4ce1d4c" } diff --git a/crates/format/Cargo.toml b/crates/format/Cargo.toml index 9424b95e0..b5673119d 100644 --- a/crates/format/Cargo.toml +++ b/crates/format/Cargo.toml @@ -20,6 +20,7 @@ ruzstd = { workspace = true } sha1 = { workspace = true } sha2 = { workspace = true } image = { workspace = true } +craft-sketch = { workspace = true } craft-fig = { workspace = true } craft-figcraft-format = { workspace = true } craft-figma-rest = { workspace = true } diff --git a/crates/format/src/sketch.rs b/crates/format/src/sketch.rs index 182447015..2389813bb 100644 --- a/crates/format/src/sketch.rs +++ b/crates/format/src/sketch.rs @@ -43,48 +43,15 @@ mod tree_tests; use std::collections::BTreeSet; use figcraft_doc::Document; -use serde_json::{Value, json}; +use serde_json::Value; + +// Container parsing is shared; FigCraft document projection remains in this module. +pub(crate) use craft_sketch::Archive; use crate::FormatError; -/// The oldest Sketch whose files open: from 48 on, text is plain JSON too. -pub const MIN_SKETCH: u32 = 48; -/// The newest `meta.json` format version checked against real files (Sketch 83); newer files -/// open with a warning. -pub const NEWEST_TESTED_VERSION: u64 = 139; -/// What opening a file saved by an older Sketch says. -pub const TOO_OLD: &str = "saved by a Sketch older than 48; open it in a newer Sketch and save it again"; -/// Largest JSON member read (a real page of a few thousand layers takes 33 MB). -const MAX_JSON_MEMBER: u64 = 512 * 1024 * 1024; -/// Most bytes a member may unpack to, per compressed byte (real JSON packs 5–20 times and images -/// barely; a zero-filled member a thousand times), with a floor for small members. -const MAX_RATIO: u64 = 100; -const MIN_ALLOWANCE: u64 = 16 * 1024 * 1024; -/// Most JSON read from one file in all. -#[cfg(not(target_pointer_width = "32"))] -const MAX_JSON_TOTAL: u64 = 2 * 1024 * 1024 * 1024; -#[cfg(target_pointer_width = "32")] -const MAX_JSON_TOTAL: u64 = 512 * 1024 * 1024; -/// Largest image member read. -const MAX_IMAGE: u64 = figcraft_doc::ImageData::MAX_BYTES; -/// Most image bytes read from one file in all. -#[cfg(not(target_pointer_width = "32"))] -const MAX_IMAGE_TOTAL: u64 = 1024 * 1024 * 1024; -#[cfg(target_pointer_width = "32")] -const MAX_IMAGE_TOTAL: u64 = 256 * 1024 * 1024; -/// Most pages read from one file. -const MAX_PAGES: usize = 1000; - -/// Whether `bytes` look like a `.sketch` file: a zip holding `document.json` and `meta.json`, or -/// the SQLite database Sketch wrote before version 43 (so that it gets the "too old" message). -pub fn is_sketch(bytes: &[u8]) -> bool { - bytes.starts_with(b"SQLite format 3\0") - || (bytes.starts_with(b"PK") - && zip::ZipArchive::new(std::io::Cursor::new(bytes)).is_ok_and(|z| { - let names: BTreeSet<&str> = z.file_names().collect(); - names.contains("document.json") && names.contains("meta.json") - })) -} +pub use craft_sketch::{MIN_SKETCH, NEWEST_TESTED_VERSION, TOO_OLD, is_sketch}; +const MAX_JSON_MEMBER: u64 = craft_sketch::MAX_JSON_MEMBER; /// Finds the installed face a PostScript name belongs to: its family and style. pub type FontLookup = Box Option<(String, String)>>; @@ -133,7 +100,12 @@ fn import_parts( if bytes.starts_with(b"SQLite format 3\0") { return Err(FormatError::Io(TOO_OLD.into())); } - let archive = Archive::open(bytes, max_member)?; + let archive = + Archive::open_with(bytes, craft_sketch::Limits { max_page: max_member, max_image: figcraft_doc::ImageData::MAX_BYTES, ..Default::default() }) + .map_err(|error| match error { + craft_sketch::Error::TooOld => FormatError::Io(TOO_OLD.into()), + craft_sketch::Error::Corrupt(message) => FormatError::Corrupt(format!("Sketch file: {message}")), + })?; let map = tree::to_rest(&archive, name, max_nodes, fonts); if map.too_old { return Err(FormatError::Io(TOO_OLD.into())); @@ -172,135 +144,3 @@ fn embedded_fonts(document: &Value) -> Option { let list: Vec<&str> = families.iter().map(String::as_str).collect(); Some(format!("the file has embedded fonts FigCraft doesn't use ({}); install them to see the text as designed", list.join(", "))) } - -fn corrupt(msg: impl Into) -> FormatError { - FormatError::Corrupt(format!("Sketch file: {}", msg.into())) -} - -/// The parts of a `.sketch` archive: the document, the pages in document order and the images. -pub(crate) struct Archive<'b> { - pub document: Value, - /// `meta.json`'s format version (0 when missing). - pub version: u64, - /// `(id, page JSON)` in the document's order. - pub pages: Vec<(String, Value)>, - /// Problems met while reading (pages skipped). - pub warnings: Vec, - zip: zip::ZipArchive>, - /// Image bytes read so far. - image_total: std::cell::Cell, -} - -impl<'b> Archive<'b> { - fn open(bytes: &'b [u8], max_member: u64) -> Result { - let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes)).map_err(|e| corrupt(e.to_string()))?; - let mut total = 0u64; - let meta = json_member(&mut z, "meta.json", MAX_JSON_MEMBER, &mut total)?; - if !meta.is_object() { - return Err(corrupt("meta.json is not an object")); - } - if app_major(&meta).is_some_and(|v| v < MIN_SKETCH) { - return Err(FormatError::Io(TOO_OLD.into())); - } - let mut warnings = Vec::new(); - if meta.get("version").and_then(Value::as_u64).is_some_and(|v| v > NEWEST_TESTED_VERSION) { - warnings.push("the file was saved by a newer Sketch than the import was checked with; some layers may differ".to_string()); - } - // A document.json that can't be read (nested too deep, damaged) loses its shared styles and - // library copies, not the pages: they are then taken in archive order. - let document = match json_member(&mut z, "document.json", MAX_JSON_MEMBER, &mut total) { - Ok(d) if d.is_object() => d, - other => { - let why = other.err().map_or_else(|| "not an object".to_string(), |e| e.to_string()); - warnings.push(format!( - "the document's shared styles and library symbols were skipped: {}", - why.trim_start_matches("not a valid FigCraft file: ") - )); - let pages: Vec = z - .file_names() - .filter_map(|n| n.strip_prefix("pages/")?.strip_suffix(".json")) - .map(|id| json!({"_ref": format!("pages/{id}")})) - .collect(); - json!({"pages": pages}) - } - }; - let refs = document.get("pages").and_then(Value::as_array).map(Vec::as_slice).unwrap_or_default(); - let mut pages = Vec::new(); - let mut seen = std::collections::HashSet::new(); - for r in refs.iter().take(MAX_PAGES) { - let Some(id) = r.get("_ref").and_then(Value::as_str).and_then(|s| s.strip_prefix("pages/")).filter(|id| valid_id(id)) else { - warnings.push("pages the document lists without a valid reference were skipped".into()); - continue; - }; - // A page listed twice is read once: a repeated reference must not repeat the unpacking. - if !seen.insert(id) { - continue; - } - match json_member(&mut z, &format!("pages/{id}.json"), max_member, &mut total) { - Ok(page) if page.is_object() => pages.push((id.to_string(), page)), - Ok(_) => warnings.push("pages that aren't JSON objects were skipped".into()), - Err(e) => warnings.push(format!("a page was skipped: {}", e.to_string().trim_start_matches("not a valid FigCraft file: "))), - } - } - if refs.len() > MAX_PAGES { - warnings.push(format!("the file has more than {MAX_PAGES} pages; the rest were skipped")); - } - let version = meta.get("version").and_then(Value::as_u64).unwrap_or(0); - Ok(Archive { document, version, pages, warnings, zip: z, image_total: std::cell::Cell::new(0) }) - } - - /// The bytes of the image a layer references (`images/`). - pub(crate) fn image(&self, r: &str) -> Option> { - let name = r.strip_prefix("images/").unwrap_or(r); - if !valid_id(name.split('.').next().unwrap_or("")) || name.split('.').count() > 2 { - return None; - } - // A clone shares the parsed directory and the bytes; the resolver only has shared access. - let mut z = self.zip.clone(); - let mut total = self.image_total.get(); - let data = capped_member(&mut z, &format!("images/{name}"), MAX_IMAGE, &mut total, MAX_IMAGE_TOTAL).ok(); - self.image_total.set(total); - data - } -} - -/// Member `name`, refused before unpacking when it would be larger than `limit`, than -/// [`MAX_RATIO`] times what it takes in the archive (past a floor), or than what is left of -/// `max_total` (`total`: read so far, counted up). -fn capped_member( - z: &mut zip::ZipArchive>, - name: &str, - limit: u64, - total: &mut u64, - max_total: u64, -) -> Result, FormatError> { - let (size, packed) = { - let f = z.by_name(name).map_err(|e| corrupt(format!("{name}: {e}")))?; - (f.size(), f.compressed_size()) - }; - let allowed = limit.min(packed.saturating_mul(MAX_RATIO).max(MIN_ALLOWANCE)).min(max_total.saturating_sub(*total)); - if size > allowed { - return Err(corrupt(format!("{name} is too large"))); - } - let data = crate::read_member(z, name, allowed); - // A failed read (a bad checksum at the end, say) unpacked all the same: charged in full, so - // a page list naming the member again can't repeat it within the budget. - *total += data.as_ref().map_or(size, |d| d.len() as u64); - data -} - -/// JSON member `name`, read by [`capped_member`] within the file's [`MAX_JSON_TOTAL`]. -fn json_member(z: &mut zip::ZipArchive>, name: &str, limit: u64, total: &mut u64) -> Result { - let data = capped_member(z, name, limit, total, MAX_JSON_TOTAL)?; - serde_json::from_slice(&data).map_err(|e| corrupt(format!("{name}: {e}"))) -} - -/// The major version of the Sketch that saved the file (`meta.json` `appVersion`, "50.2" → 50). -fn app_major(meta: &Value) -> Option { - meta.get("appVersion").and_then(Value::as_str)?.split('.').next()?.trim().parse().ok() -} - -/// Ids name archive members: letters, digits, `-` and `_` only, never a path. -fn valid_id(id: &str) -> bool { - !id.is_empty() && id.len() <= 128 && id.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'-' || b == b'_') -} diff --git a/crates/format/src/sketch/tests.rs b/crates/format/src/sketch/tests.rs index d8ace8be9..784c542bd 100644 --- a/crates/format/src/sketch/tests.rs +++ b/crates/format/src/sketch/tests.rs @@ -206,3 +206,21 @@ fn members_over_budget_are_skipped() { assert!(warnings.iter().any(|w| w.contains("too large")), "{warnings:?}"); assert!(!page_names(&doc).contains(&"Big".to_string())); } + +/// The shared container refuses ambiguous member names instead of selecting one silently. +#[test] +fn duplicate_archive_members_are_refused() { + let mut z = sketch_zip("50.2", 103, document(&["A", "B"]), &[page("A", "Alpha", vec![]), page("B", "Beta", vec![])], &[]); + let from = b"pages/B.json"; + let to = b"pages/A.json"; + let mut replaced = 0; + for i in 0..z.len().saturating_sub(from.len()).saturating_add(1) { + if z.get(i..i + from.len()) == Some(from.as_slice()) { + z[i..i + from.len()].copy_from_slice(to); + replaced += 1; + } + } + assert_eq!(replaced, 2, "local and central directory names changed"); + let error = import(&z, "ambiguous").unwrap_err().to_string(); + assert!(error.contains("two entries of the archive share a name"), "{error}"); +}