mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
PSD export: embed a linked file only when it is an image PhotoCraft can open
A PSD/PSB/layered-TIFF save read every linked smart object's file and embedded it, even when no importer can decode it (a text or settings file the link happened to point at). Such a layer can't show its contents in any reader. A linked file is now embedded only when it decodes; otherwise the layer is written as pixels with a warning. Embedded sources are unchanged. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
1 parent
b07b7fab55
commit
5ca6fc7767
2 files changed
+47
-8
No files matched your search
@@ -255,3 +255,35 @@ fn data_sets_and_footage_read_only_followed_files() {
|
||||
assert!(err.contains("is not opened: the path is on another computer"), "{err}");
|
||||
footage(&mut s, "blue.png").unwrap();
|
||||
}
|
||||
|
||||
/// A link to a file PhotoCraft can't place (a text or settings file) is never embedded: a saved
|
||||
/// PSD would otherwise carry bytes no reader can show as the layer's contents. The layer is
|
||||
/// written as pixels; Convert to Embedded and Relink refuse.
|
||||
#[test]
|
||||
fn links_to_files_that_are_not_images_are_never_embedded() {
|
||||
const TEXT: &[u8] = b"plain text, not an image
|
||||
photocraft-text-marker
|
||||
";
|
||||
let d = dir("not-image");
|
||||
std::fs::create_dir_all(d.join("notes")).unwrap();
|
||||
std::fs::create_dir_all(d.join("Downloads")).unwrap();
|
||||
std::fs::write(d.join("notes").join("readme"), TEXT).unwrap();
|
||||
std::fs::write(d.join("Downloads").join(".settings"), TEXT).unwrap();
|
||||
png(&d.join("red.png"), |_| [255, 0, 0]);
|
||||
let (mut s, id) = document(&d, &d.join("red.png"));
|
||||
s.active_mut().unwrap().path = Some(d.join("Downloads").join("doc.pcraft").to_string_lossy().into_owned());
|
||||
let has_text = |bytes: &[u8]| bytes.windows(TEXT.len()).any(|w| w == TEXT);
|
||||
for (n, path) in ["../notes/readme", ".settings"].into_iter().enumerate() {
|
||||
relink(&mut s, id, path);
|
||||
for ext in ["psd", "psb", "tif"] {
|
||||
let out = d.join(format!("copy{n}.{ext}"));
|
||||
let r = s.execute("file.saveACopy", json!({"path": out.to_string_lossy(), "tiffLayers": true})).unwrap();
|
||||
assert!(!has_text(&std::fs::read(&out).unwrap()), "{path} embedded in the {ext}: {r}");
|
||||
let warnings = r["warnings"].to_string();
|
||||
assert!(warnings.contains("written as pixels"), "{path} {ext}: {warnings}");
|
||||
}
|
||||
assert!(s.execute("layer.smartObjects.convertToEmbedded", json!({"layer": id.0})).is_err(), "{path}");
|
||||
let abs = d.join("notes").join("readme");
|
||||
assert!(s.execute("layer.smartObjects.relinkToFile", json!({"layer": id.0, "path": abs.to_string_lossy()})).is_err(), "{path}");
|
||||
}
|
||||
}
|
||||
@@ -1005,13 +1005,14 @@ impl Ex {
|
||||
let file = crate::doc_path::resolve(path, self.document.as_deref()).map_err(|e| format!("the linked file {path} is not opened: {e}"))?;
|
||||
let bytes = std::fs::read(file).map_err(|e| format!("the linked file {path} can't be read: {e}"))?;
|
||||
let name = path.rsplit(['/', '\\']).next().unwrap_or(path).to_string();
|
||||
let source = self.embed(sm.contents_id, &name, &bytes, true)?;
|
||||
self.warnings.push(format!("the linked smart object {name} was embedded (PSD export keeps no external links)"));
|
||||
self.embed(sm.contents_id, &name, &bytes)
|
||||
Ok(source)
|
||||
}
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
Err(format!("the linked file {path} can't be read here"))
|
||||
}
|
||||
SmartSource::Embedded { file_name, bytes } => self.embed(sm.contents_id, file_name, bytes),
|
||||
SmartSource::Embedded { file_name, bytes } => self.embed(sm.contents_id, file_name, bytes, false),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1056,17 +1057,19 @@ impl Ex {
|
||||
|
||||
/// Adds `bytes` as an embedded file (converted to PSB when it is a `.pcraft` bundle), once
|
||||
/// per contents identity and bytes. Equal bytes in independent objects stay independent.
|
||||
fn embed(&mut self, contents_id: SmartContentsId, file_name: &str, bytes: &[u8]) -> Result<Source, String> {
|
||||
/// `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());
|
||||
if let Some(r) = self.smart.converted.get(&key) {
|
||||
return r.clone();
|
||||
}
|
||||
let r = self.convert_source(contents_id, file_name, bytes);
|
||||
let r = self.convert_source(contents_id, file_name, bytes, linked);
|
||||
self.smart.converted.insert(key, r.clone());
|
||||
r
|
||||
}
|
||||
|
||||
fn convert_source(&mut self, contents_id: SmartContentsId, file_name: &str, bytes: &[u8]) -> Result<Source, String> {
|
||||
fn convert_source(&mut self, contents_id: SmartContentsId, file_name: &str, bytes: &[u8], linked: bool) -> Result<Source, String> {
|
||||
let (name, data, size, dpi, nested) = if photocraft_format::is_pcraft(bytes) {
|
||||
if self.smart.depth >= MAX_NESTING {
|
||||
return Err(format!("smart objects nest more than {MAX_NESTING} deep"));
|
||||
@@ -1084,9 +1087,13 @@ impl Ex {
|
||||
let size = (f64::from(doc.size.width), f64::from(doc.size.height));
|
||||
(format!("{stem}.psb"), data, size, f64::from(doc.resolution_dpi), Some(doc))
|
||||
} else {
|
||||
// Formats we can't decode (vector PDF/AI…) are still embedded; their size comes from the layer.
|
||||
let (size, dpi) =
|
||||
crate::linked::source_geometry(file_name, bytes, &photocraft_raster::Interrupt::NONE).unwrap_or(((0.0, 0.0), f64::from(self.dpi)));
|
||||
// Embedded formats we can't decode (vector PDF/AI…) are kept; their size comes from the
|
||||
// layer. A linked file we can't decode is not embedded.
|
||||
let (size, dpi) = match crate::linked::source_geometry(file_name, bytes, &photocraft_raster::Interrupt::NONE) {
|
||||
Ok(geometry) => geometry,
|
||||
Err(e) if linked => return Err(format!("the linked file {file_name} is not an image PhotoCraft can open: {e}")),
|
||||
Err(_) => ((0.0, 0.0), f64::from(self.dpi)),
|
||||
};
|
||||
(file_name.to_string(), bytes.to_vec(), size, dpi, None)
|
||||
};
|
||||
let uuid = contents_uuid(contents_id, &data);
|
||||
|
||||
Reference in new issue
Block a user