mirror of
https://github.com/storytold/figcraft.git
synced 2026-11-15 23:02:36 +00:00
Use shared FigCraft preview reader
This commit is contained in:
1 parent
990d03d6a6
commit
6081e41a70
3 files changed
+15
-16
No files matched your search
Generated
+3
-3
@@ -1029,9 +1029,9 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-figcraft-format"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=0e2a717c644bcfaae1a27cd5e2f6b4c0a6b7798b#0e2a717c644bcfaae1a27cd5e2f6b4c0a6b7798b"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=976eb51c59edb0a37f24105850f52d921c8518ee#976eb51c59edb0a37f24105850f52d921c8518ee"
|
||||
dependencies = [
|
||||
"craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=0e2a717c644bcfaae1a27cd5e2f6b4c0a6b7798b)",
|
||||
"craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=976eb51c59edb0a37f24105850f52d921c8518ee)",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"thiserror 2.0.21",
|
||||
@@ -1231,7 +1231,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-zip"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=0e2a717c644bcfaae1a27cd5e2f6b4c0a6b7798b#0e2a717c644bcfaae1a27cd5e2f6b4c0a6b7798b"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=976eb51c59edb0a37f24105850f52d921c8518ee#976eb51c59edb0a37f24105850f52d921c8518ee"
|
||||
dependencies = [
|
||||
"flate2",
|
||||
"thiserror 2.0.21",
|
||||
|
||||
+1
-1
@@ -60,7 +60,7 @@ craft-settings = { git = "https://github.com/storytold/craft-libs", rev = "e11f9
|
||||
# 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-fig = { git = "https://github.com/storytold/craft-libs", rev = "d13ac58a15c45d618f25910772502e38c4ce1d4c" }
|
||||
craft-figcraft-format = { git = "https://github.com/storytold/craft-libs", rev = "0e2a717c644bcfaae1a27cd5e2f6b4c0a6b7798b" }
|
||||
craft-figcraft-format = { git = "https://github.com/storytold/craft-libs", rev = "976eb51c59edb0a37f24105850f52d921c8518ee" }
|
||||
craft-figma-rest = { git = "https://github.com/storytold/craft-libs", rev = "d13ac58a15c45d618f25910772502e38c4ce1d4c" }
|
||||
|
||||
# foundation
|
||||
|
||||
@@ -7,13 +7,12 @@ use std::io::{Read, Seek};
|
||||
use std::path::Path;
|
||||
|
||||
/// Largest `thumbnail.png` read from an archive.
|
||||
pub const MAX_THUMBNAIL_BYTES: u64 = 8 * 1024 * 1024;
|
||||
pub const MAX_THUMBNAIL_BYTES: u64 = craft_figcraft_format::MAX_PREVIEW_THUMBNAIL_BYTES;
|
||||
/// Largest image file read as its own picture.
|
||||
pub const MAX_IMAGE_FILE_BYTES: u64 = 32 * 1024 * 1024;
|
||||
/// Widest or tallest picture decoded (pixels); a larger one gives none.
|
||||
pub const MAX_THUMBNAIL_SIDE: u32 = 8192;
|
||||
/// Largest `document.json` read for its page colour, and `.fig` `meta.json`.
|
||||
const MAX_DOCUMENT_BYTES: u64 = 64 * 1024 * 1024;
|
||||
/// Largest `.fig` `meta.json` read for its page colour.
|
||||
const MAX_META_BYTES: u64 = 1024 * 1024;
|
||||
/// Most memory one decode may take.
|
||||
const MAX_DECODE_ALLOC: u64 = 256 * 1024 * 1024;
|
||||
@@ -29,8 +28,7 @@ pub struct Thumbnail {
|
||||
/// The bytes of `thumbnail.png` in a zip archive (`.figcraft`, `.fig`); `None` when it isn't a zip,
|
||||
/// has no thumbnail, or the thumbnail is larger than [`MAX_THUMBNAIL_BYTES`].
|
||||
pub fn thumbnail_png<R: Read + Seek>(archive: R) -> Option<Vec<u8>> {
|
||||
let mut z = zip::ZipArchive::new(archive).ok()?;
|
||||
crate::read_member(&mut z, "thumbnail.png", MAX_THUMBNAIL_BYTES).ok()
|
||||
craft_figcraft_format::read_preview_thumbnail(archive).ok().flatten()
|
||||
}
|
||||
|
||||
/// The bytes of `previews/preview.png` in a `.sketch` zip; `None` as [`thumbnail_png`] gives it.
|
||||
@@ -148,19 +146,20 @@ pub fn file_preview(path: &Path, fit: u32) -> Preview {
|
||||
/// The [`Preview`] of a `.figcraft` or `.fig` archive: `thumbnail.png`, and the first page's colour
|
||||
/// from `document.json` (`.figcraft`) or `meta.json` (`.fig`).
|
||||
pub fn archive_preview<R: Read + Seek>(archive: R, fit: u32) -> Preview {
|
||||
let Ok(mut z) = zip::ZipArchive::new(archive) else { return Preview::default() };
|
||||
let picture = crate::read_member(&mut z, "thumbnail.png", MAX_THUMBNAIL_BYTES).ok().and_then(|png| decode_thumbnail(&png, fit));
|
||||
let (page, kind) = if z.file_names().any(|n| n == "document.json") {
|
||||
crate::read_member(&mut z, "document.json", MAX_DOCUMENT_BYTES).ok().map(|json| figcraft_page(&json)).unwrap_or_default()
|
||||
let Ok(mut z) = craft_figcraft_format::SeekArchive::open(archive, u64::MAX) else { return Preview::default() };
|
||||
let native = craft_figcraft_format::read_preview_archive_from(&mut z);
|
||||
let picture = native.thumbnail_png.and_then(|png| decode_thumbnail(&png, fit));
|
||||
let (page, kind) = if native.has_document {
|
||||
native.document.as_ref().map(figcraft_page).unwrap_or_default()
|
||||
} else {
|
||||
(crate::read_member(&mut z, "meta.json", MAX_META_BYTES).ok().and_then(|json| fig_page(&json)), figcraft_doc::FileKind::Design)
|
||||
let meta = z.read("meta.json", MAX_META_BYTES).ok().flatten();
|
||||
(meta.as_deref().and_then(fig_page), figcraft_doc::FileKind::Design)
|
||||
};
|
||||
Preview { page, picture, kind }
|
||||
}
|
||||
|
||||
/// The first page's background in a `.figcraft` `document.json`, and the file's kind.
|
||||
fn figcraft_page(json: &[u8]) -> (Option<[f32; 4]>, figcraft_doc::FileKind) {
|
||||
let Ok(head) = craft_figcraft_format::read_preview_document(json) else { return (None, figcraft_doc::FileKind::Design) };
|
||||
fn figcraft_page(head: &craft_figcraft_format::PreviewDocument) -> (Option<[f32; 4]>, figcraft_doc::FileKind) {
|
||||
let page = head.pages.first().and_then(|first| head.nodes.get(&first.to_string())).and_then(|n| n.background.as_ref()).and_then(rgba);
|
||||
let kind = match head.kind {
|
||||
craft_figcraft_format::PreviewKind::Design => figcraft_doc::FileKind::Design,
|
||||
|
||||
Reference in new issue
Block a user