document-bundle: turn a non-zip file away after four bytes in read_preview_from

The zip reader searches a whole file for an end-of-directory record
before it gives up on a file that isn't a zip. DesignCraft reads the
preview of every recent file this way before the start screen's first
frame: 2.6 s for a local 110 MB InDesign document, 43 s on a network
share.

read_preview_from now checks the zip local-file header first.

Fixes #2031
This commit is contained in:
r31lin committed 2026-11-17 12:21:21 +01:00
1 parent 83cb1133dd
commit 8d5b02457e
1 file changed
+47 -1
+47 -1
View File
@@ -316,8 +316,19 @@ pub fn read_preview(bytes: &[u8], preview_entry: &str) -> Option<Vec<u8>> {
}
/// Read a bounded preview from a seekable file without loading the full bundle into memory.
pub fn read_preview_from<R: Read + Seek>(reader: R, preview_entry: &str) -> Option<Vec<u8>> {
///
/// A file that does not start with a zip local-file header is turned away after four bytes. The zip
/// reader would otherwise search the whole file for an end-of-directory record it does not have,
/// which takes seconds for a large non-bundle file (an InDesign document in a recent-files list)
/// and minutes on a network share.
pub fn read_preview_from<R: Read + Seek>(mut reader: R, preview_entry: &str) -> Option<Vec<u8>> {
const PREVIEW_LIMIT: u64 = 4 << 20;
let mut magic = [0u8; 4];
reader.read_exact(&mut magic).ok()?;
if &magic != b"PK\x03\x04" {
return None;
}
reader.seek(std::io::SeekFrom::Start(0)).ok()?;
let mut zip = zip::ZipArchive::new(reader).ok()?;
let entry = zip.by_name(preview_entry).ok()?;
if entry.size() > PREVIEW_LIMIT {
@@ -409,6 +420,41 @@ mod tests {
assert_eq!(read_preview(&bytes, SPEC.preview_entry).as_deref(), Some(&b"png"[..]));
}
/// Counts the bytes read through it.
struct Counting<R> {
inner: R,
read: usize,
}
impl<R: Read> Read for Counting<R> {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
let n = self.inner.read(buf)?;
self.read += n;
Ok(n)
}
}
impl<R: Seek> Seek for Counting<R> {
fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result<u64> {
self.inner.seek(pos)
}
}
#[test]
fn preview_from_a_reader_reads_a_bundle() {
let bytes = write(br#"{}"#, 4, "Test 0.1", Some(b"png"), &[], &[], SPEC).unwrap();
assert_eq!(read_preview_from(Cursor::new(&bytes), SPEC.preview_entry).as_deref(), Some(&b"png"[..]));
}
#[test]
fn preview_from_a_non_zip_reads_only_its_signature() {
let mut file = Counting { inner: Cursor::new(vec![0x42u8; 1 << 20]), read: 0 };
assert_eq!(read_preview_from(&mut file, SPEC.preview_entry), None);
assert!(file.read <= 4, "read {} bytes of a file that is not a zip", file.read);
let mut empty = Counting { inner: Cursor::new(Vec::new()), read: 0 };
assert_eq!(read_preview_from(&mut empty, SPEC.preview_entry), None);
}
#[test]
fn extracts_legacy_inline_assets() {
let loaded = read(br#"{"assetData":{"7":"AQID"},"name":"x"}"#, 4, SPEC).unwrap();