mirror of
https://github.com/storytold/figcraft.git
synced 2026-11-26 02:37:26 +00:00
format: read Sketch previews through shared ZIP (#2552)
* format: read Sketch previews through shared ZIP * Credits: add Terra854 to About
This commit is contained in:
Generated
+1
@@ -2279,6 +2279,7 @@ dependencies = [
|
||||
"craft-jpeg 0.1.0 (git+https://github.com/storytold/craft-libs?rev=a49329e71e32ad93794ab595c41f33209a7d2220)",
|
||||
"craft-penpot",
|
||||
"craft-sketch",
|
||||
"craft-zip",
|
||||
"figcraft-doc",
|
||||
"figcraft-layout",
|
||||
"figcraft-pathops",
|
||||
|
||||
@@ -65,6 +65,7 @@ craft-penpot = { git = "https://github.com/storytold/craft-libs", rev = "a49329e
|
||||
# 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.
|
||||
craft-sketch = { git = "https://github.com/storytold/craft-libs", rev = "a49329e71e32ad93794ab595c41f33209a7d2220" }
|
||||
craft-zip = { git = "https://github.com/storytold/craft-libs", rev = "a49329e71e32ad93794ab595c41f33209a7d2220" }
|
||||
craft-fig = { git = "https://github.com/storytold/craft-libs", rev = "a49329e71e32ad93794ab595c41f33209a7d2220" }
|
||||
craft-figcraft-format = { git = "https://github.com/storytold/craft-libs", rev = "a49329e71e32ad93794ab595c41f33209a7d2220" }
|
||||
craft-figma-rest = { git = "https://github.com/storytold/craft-libs", rev = "a49329e71e32ad93794ab595c41f33209a7d2220" }
|
||||
|
||||
@@ -128,3 +128,6 @@ sync_github_name = true
|
||||
|
||||
[people.B-uchi]
|
||||
real_name = "Ibuchukwu Umeano"
|
||||
|
||||
[people.Terra854]
|
||||
display_name = "Terra854"
|
||||
|
||||
@@ -22,6 +22,7 @@ sha2 = { workspace = true }
|
||||
image = { workspace = true }
|
||||
craft-penpot = { workspace = true }
|
||||
craft-sketch = { workspace = true }
|
||||
craft-zip = { workspace = true }
|
||||
craft-fig = { workspace = true }
|
||||
craft-figcraft-format = { workspace = true }
|
||||
craft-figma-rest = { workspace = true }
|
||||
|
||||
@@ -559,6 +559,7 @@ pub fn load_bytes(bytes: &[u8]) -> Result<(Document, Vec<String>), FormatError>
|
||||
}
|
||||
|
||||
/// Read a member from a foreign ZIP archive, refusing entries larger than `limit`.
|
||||
#[cfg(test)]
|
||||
pub(crate) fn read_member<R: Read + std::io::Seek>(z: &mut zip::ZipArchive<R>, name: &str, limit: u64) -> Result<Vec<u8>, FormatError> {
|
||||
let f = z.by_name(name).map_err(|e| FormatError::Corrupt(format!("{name}: {e}")))?;
|
||||
if f.size() > limit {
|
||||
|
||||
@@ -8,6 +8,8 @@ use std::path::Path;
|
||||
|
||||
/// Largest `thumbnail.png` read from an archive.
|
||||
pub const MAX_THUMBNAIL_BYTES: u64 = craft_figcraft_format::MAX_PREVIEW_THUMBNAIL_BYTES;
|
||||
/// Maximum ZIP index bytes read while locating a Sketch preview.
|
||||
const MAX_SKETCH_PREVIEW_METADATA_BYTES: u64 = 64 * 1024 * 1024;
|
||||
/// 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.
|
||||
@@ -33,8 +35,13 @@ pub fn thumbnail_png<R: Read + Seek>(archive: R) -> Option<Vec<u8>> {
|
||||
|
||||
/// The bytes of `previews/preview.png` in a `.sketch` zip; `None` as [`thumbnail_png`] gives it.
|
||||
pub fn sketch_preview_png<R: Read + Seek>(archive: R) -> Option<Vec<u8>> {
|
||||
let mut z = zip::ZipArchive::new(archive).ok()?;
|
||||
crate::read_member(&mut z, "previews/preview.png", MAX_THUMBNAIL_BYTES).ok()
|
||||
let limits = craft_zip::SeekableLimits {
|
||||
max_entries: craft_sketch::MAX_ENTRIES,
|
||||
max_metadata_bytes: MAX_SKETCH_PREVIEW_METADATA_BYTES,
|
||||
max_total_bytes: MAX_THUMBNAIL_BYTES,
|
||||
};
|
||||
let mut archive = craft_zip::SeekableArchive::open(archive, limits).ok()?;
|
||||
archive.read("previews/preview.png", MAX_THUMBNAIL_BYTES).ok().flatten()
|
||||
}
|
||||
|
||||
/// Decode a picture (PNG, JPEG, WebP, GIF, BMP or TIFF) no larger than [`MAX_THUMBNAIL_SIDE`] on
|
||||
|
||||
@@ -235,6 +235,19 @@ fn previews_carry_the_first_pages_colour() {
|
||||
assert!(p.page.is_none() && p.picture.is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sketch_preview_rejects_archives_above_the_import_entry_limit() {
|
||||
let mut zip = zip::ZipWriter::new(Cursor::new(Vec::new()));
|
||||
let stored = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored);
|
||||
zip.start_file("previews/preview.png", stored).unwrap();
|
||||
zip.write_all(&png(1, 1)).unwrap();
|
||||
for number in 0..craft_sketch::MAX_ENTRIES {
|
||||
zip.start_file(format!("pages/{number}.json"), stored).unwrap();
|
||||
}
|
||||
let bytes = zip.finish().unwrap().into_inner();
|
||||
assert!(crate::thumbnail::sketch_preview_png(Cursor::new(bytes)).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sketch_files_show_their_preview_png() {
|
||||
use crate::thumbnail::{Preview, file_preview};
|
||||
|
||||
Reference in New Issue
Block a user