DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
Integration: a dropped file without an extension is recognised from its first bytes
aiongg1014d841ac15
diff --git a/crates/engine/src/cmd/file.rs b/crates/engine/src/cmd/file.rsindex 3a43b164d..ac66397c6 100644--- a/crates/engine/src/cmd/file.rs+++ b/crates/engine/src/cmd/file.rs@@ -174,10 +174,15 @@ pub fn with_document_extension(path: &str) -> String {}/// Whether `bytes` are a document `file.open` reads: a DesignCraft package or an IDML package.+/// The first [`DOCUMENT_SNIFF_BYTES`] of a file are enough for packages that start with their+/// `mimetype` entry, as both formats write them.pub fn is_document(bytes: &[u8]) -> bool {- designcraft_idml::is_idml(bytes) || designcraft_format::is_designcraft(bytes)+ designcraft_idml::is_idml(bytes) || designcraft_format::starts_designcraft(bytes) || designcraft_format::is_designcraft(bytes)}+/// How much of a file [`is_document`] needs to recognise a package by its `mimetype` entry.+pub const DOCUMENT_SNIFF_BYTES: u64 = 64 << 10;+/// The longer side of the first-page picture saved in the file, in pixels.const PREVIEW_PX: f64 = 320.0;@@ -1923,6 +1928,11 @@ mod extension_tests {let idml = dir.join("Package");std::fs::write(&idml, designcraft_idml::export_idml(&s.doc().unwrap().doc)).unwrap();assert!(is_document(&std::fs::read(&idml).unwrap()) && is_document(&std::fs::read(&bare).unwrap()));+ // The start of the file is enough.+ for path in [&idml, &bare] {+ let bytes = std::fs::read(path).unwrap();+ assert!(is_document(&bytes[..bytes.len().min(128)]), "{path:?}");+ }t.execute("file.open", &json!({"path": idml.to_string_lossy()})).unwrap();assert_eq!(t.doc().unwrap().path, None);diff --git a/crates/engine/src/cmd/mod.rs b/crates/engine/src/cmd/mod.rsindex 69be818de..c7270f2f0 100644--- a/crates/engine/src/cmd/mod.rs+++ b/crates/engine/src/cmd/mod.rs@@ -332,7 +332,9 @@ pub fn file_from(b: &[u8]) -> Result<designcraft_doc::Document> {}pub(crate) use datamerge::sync_placeholders;pub(crate) use file::load_document_fonts;-pub use file::{RecentPreview, base64_decode, base64_encode, from_bytes, is_document, recent_preview, to_bytes, with_document_extension};+pub use file::{+ DOCUMENT_SNIFF_BYTES, RecentPreview, base64_decode, base64_encode, from_bytes, is_document, recent_preview, to_bytes, with_document_extension,+};pub use layout::chapter_numbering;pub use paste_svg::is_svg_markup;pub(crate) use place_text::autoflow as place_text_autoflow;diff --git a/crates/format/src/lib.rs b/crates/format/src/lib.rsindex 86018b8a9..1b8d30d47 100644--- a/crates/format/src/lib.rs+++ b/crates/format/src/lib.rs@@ -107,6 +107,16 @@ pub fn is_designcraft(bytes: &[u8]) -> bool {bytes.starts_with(b"PK") && zip::ZipArchive::new(Cursor::new(bytes)).is_ok_and(|z| z.index_for_name("document.json").is_some())}+/// Whether `prefix`, the start of a file, opens a DesignCraft package: a zip whose first, stored+/// entry is the `mimetype` naming this format. Packages saved before they had one are recognised+/// only whole ([`is_designcraft`]).+pub fn starts_designcraft(prefix: &[u8]) -> bool {+ prefix.starts_with(b"PK\x03\x04") && prefix.get(26..28) == Some(&8u16.to_le_bytes()[..]) && prefix.get(30..38) == Some(b"mimetype") && {+ let extra = prefix.get(28..30).map_or(0, |e| usize::from(u16::from_le_bytes([e[0], e[1]])));+ prefix.get(38 + extra..).is_some_and(|rest| rest.starts_with(MIME.as_bytes()))+ }+}+/// The zip entry holding the first page's picture.pub const PREVIEW: &str = "preview.png";diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rsindex 11ccf6085..b06aa1225 100644--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@ -1299,7 +1299,8 @@ impl DesignApp {}let document = opens_as_document(path)|| (std::path::Path::new(path).extension().is_none()- && designcraft_engine::io::read(path).is_ok_and(|b| designcraft_engine::cmd::is_document(&b)));+ && designcraft_engine::io::read_capped(path, designcraft_engine::cmd::DOCUMENT_SNIFF_BYTES)+ .is_ok_and(|b| designcraft_engine::cmd::is_document(&b)));if document {return self.open_file("file.open", json!({"path": path}));}