Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Merge PR #8150: fix(automation): doc.replaceSmartObject replaces Smart Object contents from the read root (#8061)

David Mann7c9d6ac344f2
diff --cc crates/automation/src/headless.rs
index 1e3e83e1b,b9d36a599..4fc11ce56
--- a/crates/automation/src/headless.rs
+++ b/crates/automation/src/headless.rs
@@@ -131,27 -113,53 +131,51 @@@ impl Headless
/// automation (#2810). `path` resolves beneath the read root the way `open` resolves one,
/// and `params` carries the command's own options (`scale`, `fit`, `center`).
pub fn place(&mut self, index: Option<usize>, path: &str, params: &Value) -> Result<Value, AutomationError> {
- let i = self.doc_index(index)?;
- if self.session.active_index() != Some(i) && !self.session.set_active(i) {
- return Err(AutomationError::BadRequest(format!("no document at index {i}")));
- }
- let mut opts = params.as_object().cloned().unwrap_or_default();
- opts.insert("path".into(), json!(path));
- // Where ambient paths are allowed the command reads the file itself, as everywhere else.
- if matches!(&self.filesystem, Filesystem::TrustedLocal) {
- return self.command_run("file.placeEmbedded", Value::Object(opts));
- }
- let bytes = match &self.filesystem {
- Filesystem::Workspace(workspace) => workspace.read(path)?,
- _ => {
- return Err(AutomationError::BadRequest("automation filesystem access is not granted: read authority is absent".into()));
+ self.on_doc(index, |this| {
+ let mut opts = params.as_object().cloned().unwrap_or_default();
+ opts.insert("path".into(), json!(path));
+ // Where ambient paths are allowed the command reads the file itself, as everywhere else.
+ if matches!(&this.filesystem, Filesystem::TrustedLocal) {
+ return this.command_run("file.placeEmbedded", Value::Object(opts));
}
- };
- let name = Path::new(path).file_name().and_then(|name| name.to_str()).unwrap_or(path);
- self.sync_jobs();
- // The bytes are already in memory and the path resolved beneath the root capability, so
- // placing them needs no filesystem check of its own (`place_bytes` is the command body).
- Ok(file_cmds::place_bytes(&mut self.session, name, bytes, None, &Value::Object(opts))?)
+ let bytes = match &this.filesystem {
+ Filesystem::Workspace(workspace) => workspace.read(path)?,
+ _ => {
+ return Err(AutomationError::BadRequest("automation filesystem access is not granted: read authority is absent".into()));
+ }
+ };
+ let name = Path::new(path).file_name().and_then(|name| name.to_str()).unwrap_or(path);
+ this.sync_jobs();
+ // The bytes are already in memory and the path resolved beneath the root capability, so
+ // placing them needs no filesystem check of its own (`place_bytes` is the command body).
+ Ok(file_cmds::place_bytes(&mut this.session, name, bytes, None, &Value::Object(opts))?)
+ })
}
+ /// Replace a smart object's contents with a file from the read root: the capability-scoped
+ /// `layer.smartObjects.replaceContents`, which takes an ambient `path` and is refused in
+ /// automation (#8061). `params` is the command's own (`layer`).
+ pub fn replace_smart_object(&mut self, index: Option<usize>, path: &str, params: &Value) -> Result<Value, AutomationError> {
+ let i = self.doc_index(index)?;
+ if self.session.active_index() != Some(i) && !self.session.set_active(i) {
+ return Err(AutomationError::BadRequest(format!("no document at index {i}")));
+ }
+ let mut opts = params.as_object().cloned().unwrap_or_default();
+ opts.insert("path".into(), json!(path));
+ if matches!(&self.filesystem, Filesystem::TrustedLocal) {
+ return self.command_run("layer.smartObjects.replaceContents", Value::Object(opts));
+ }
+ let bytes = match &self.filesystem {
+ Filesystem::Workspace(workspace) => workspace.read(path)?,
+ _ => {
+ return Err(AutomationError::BadRequest("automation filesystem access is not granted: read authority is absent".into()));
+ }
+ };
+ let name = Path::new(path).file_name().and_then(|name| name.to_str()).unwrap_or(path);
+ self.sync_jobs();
+ Ok(photocraft_engine::smart_cmds::replace_contents_from_bytes(&mut self.session, &Value::Object(opts), name, bytes)?)
+ }
+
/// Export each exported layer of a document as its own file beneath the write root: the
/// capability-scoped `file.export.layersToFiles`, which takes an ambient `dir` and is
/// therefore refused in automation (#3216). The bytes come from the command's own core, so