PSD export: keep linked and embedded copies of the same contents apart in the cache

Duplicates share a contents identity, so a linked and an embedded copy
of the same bytes shared one cached conversion: whichever was written
first decided for both. The cache key now includes whether the bytes
came from a link.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
David MannandClaude Opus 5.5 committed 2026-10-24 18:58:09 +02:00
1 parent 5ca6fc7767
commit e279d5c824
2 files changed
+36 -3

No files matched your search

+32
View File
@@ -287,3 +287,35 @@ photocraft-text-marker
assert!(s.execute("layer.smartObjects.relinkToFile", json!({"layer": id.0, "path": abs.to_string_lossy()})).is_err(), "{path}");
}
}
/// Duplicates share their contents identity, so a linked and an embedded copy of the same bytes
/// meet in the export's conversion cache: each keeps its own rule, whichever is written first.
#[test]
fn a_linked_and_an_embedded_copy_of_the_same_bytes_keep_their_own_rule() {
const TEXT: &[u8] = b"plain text, not an image
photocraft-shared-marker
";
let d = dir("shared-contents");
std::fs::write(d.join("readme"), TEXT).unwrap();
png(&d.join("red.png"), |_| [255, 0, 0]);
let (mut s, linked) = document(&d, &d.join("red.png"));
s.execute("layer.duplicate", json!({})).unwrap();
let embedded = s.active().unwrap().active_layer.unwrap();
assert_ne!(linked, embedded);
relink(&mut s, linked, "readme");
s.edit("Embed", |doc, _| {
if let Some(LayerContent::Smart(sm)) = doc.layer_mut(embedded).map(|l| &mut l.content) {
sm.source = SmartSource::Embedded { file_name: "readme".into(), bytes: std::sync::Arc::new(TEXT.to_vec()) };
}
Ok(())
})
.unwrap();
for ext in ["psd", "psb", "tif"] {
let out = d.join(format!("copy.{ext}"));
let r = s.execute("file.saveACopy", json!({"path": out.to_string_lossy(), "tiffLayers": true})).unwrap();
let bytes = std::fs::read(&out).unwrap();
let copies = bytes.windows(TEXT.len()).filter(|w| *w == TEXT).count();
assert_eq!(copies, 1, "{ext}: only the embedded copy is kept: {r}");
assert!(r["warnings"].to_string().contains("written as pixels"), "{ext}: {r}");
}
}
+4 -3
View File
@@ -169,8 +169,9 @@ struct SmartOut {
prune_ok: bool,
/// Files to embed.
add: Vec<crate::linked::LinkedFile>,
/// Sources converted so far, by contents identity and bytes (`Err` = can't be embedded).
converted: std::collections::HashMap<(SmartContentsId, [u8; 32]), Result<Source, String>>,
/// Sources converted so far, by contents identity, bytes and whether they were read from a
/// link (`Err` = can't be embedded).
converted: std::collections::HashMap<(SmartContentsId, [u8; 32], bool), Result<Source, String>>,
/// Each placed layer has its own id, even when it duplicates an imported layer.
placed_used: std::collections::HashSet<String>,
/// Original instances reserve their imported placement regardless of layer order.
@@ -1060,7 +1061,7 @@ impl Ex {
/// `linked`: the bytes were read from a path the document names, so only a file PhotoCraft
/// can open is embedded (a link to a text or settings file is written as pixels instead).
fn embed(&mut self, contents_id: SmartContentsId, file_name: &str, bytes: &[u8], linked: bool) -> Result<Source, String> {
let key = (contents_id, *blake3::hash(bytes).as_bytes());
let key = (contents_id, *blake3::hash(bytes).as_bytes(), linked);
if let Some(r) = self.smart.converted.get(&key) {
return r.clone();
}