PhotoCraft: An open-source, photo editor written by scratch in pure Rust
File › Scripts › Browse: reject Photoshop ExtendScript (#5406) (#5435)
Vikram Bhaskaranf02c0cbef704
diff --git a/crates/engine/src/automate_cmds.rs b/crates/engine/src/automate_cmds.rsindex 6a98ee3c4..4c78f90b3 100644--- a/crates/engine/src/automate_cmds.rs+++ b/crates/engine/src/automate_cmds.rs@@ -24,6 +24,22 @@ fn bad(cmd: &str, msg: impl Into<String>) -> EngineError {EngineError::BadParams { cmd: cmd.into(), msg: msg.into() }}+/// Photoshop ExtendScript / UXP script suffixes — not PhotoCraft JSON or command lists.+fn extendscript_ext(path: &str) -> Option<&str> {+ let name = path.rsplit(['/', '\\']).next().unwrap_or(path);+ let dot = name.rfind('.')?;+ let ext = &name[dot + 1..];+ let lower = ext.to_ascii_lowercase();+ matches!(lower.as_str(), "js" | "jsx" | "jsxbin" | "psjs" | "ccjs").then_some(ext)+}++fn reject_extendscript(cmd: &str, path: &str) -> Result<()> {+ match extendscript_ext(path) {+ Some(_) => Err(bad(cmd, "PhotoCraft scripts are JSON or command lists (one command.id {json} per line), not Photoshop ExtendScript (.js/.jsx)")),+ None => Ok(()),+ }+}+/// Session state of the File-menu commands (not saved, not history).#[derive(Clone, Debug, Default)]pub struct FileMenuState {@@ -127,6 +143,12 @@ fn run_steps(s: &mut Session, steps: &[(String, Value)]) -> Vec<Value> {/// File › Scripts › Browse…: run a script file on the session.fn browse(s: &mut Session, p: &Value) -> Result<Value> {let cmd = "file.scripts.browse";+ if let Some(path) = p.get("path").and_then(Value::as_str) {+ reject_extendscript(cmd, path)?;+ }+ if let Some(name) = p.get("name").and_then(Value::as_str) {+ reject_extendscript(cmd, name)?;+ }let steps = match (p.get("path").and_then(Value::as_str), p.get("script").and_then(Value::as_str), p.get("steps")) {(Some(path), _, _) => parse_script(&String::from_utf8_lossy(&read_file(path)?))?,(_, Some(text), _) => parse_script(text)?,diff --git a/crates/engine/src/automate_cmds/tests.rs b/crates/engine/src/automate_cmds/tests.rsindex 67852534c..06d0e88f3 100644--- a/crates/engine/src/automate_cmds/tests.rs+++ b/crates/engine/src/automate_cmds/tests.rs@@ -54,6 +54,27 @@ fn browse_runs_a_script_file() {assert_eq!(r["results"].as_array().unwrap().len(), 1);}+#[test]+fn browse_rejects_photoshop_extendscript() {+ let dir = tmp("jsx");+ let jsx = format!("{dir}/hello.jsx");+ std::fs::write(&jsx, "alert('hi');\n").unwrap();+ let mut s = Session::new();+ let err = s.execute("file.scripts.browse", json!({"path": jsx})).unwrap_err().to_string();+ assert!(err.contains("not Photoshop ExtendScript"), "{err}");+ assert!(err.contains("JSON"), "{err}");+ let js = format!("{dir}/hello.js");+ std::fs::write(&js, "alert('hi');\n").unwrap();+ assert!(s.execute("file.scripts.browse", json!({"path": js})).is_err());+ let err = s.execute("file.scripts.browse", json!({"name": "Resize.jsx", "script": "alert(1);"})).unwrap_err().to_string();+ assert!(err.contains(".js/.jsx"), "{err}");+ // A JSON script still runs even if the file name is not a Photoshop suffix.+ let json = format!("{dir}/rotate.json");+ std::fs::write(&json, r#"[["file.new", {"width": 8, "height": 8}]]"#).unwrap();+ let r = s.execute("file.scripts.browse", json!({"path": json})).unwrap();+ assert_eq!(r["ok"], true);+}+#[test]fn recording_browse_script_captures_each_effect_once() {let mut s = Session::new();diff --git a/crates/ui-egui/src/file_dialog/tests.rs b/crates/ui-egui/src/file_dialog/tests.rsindex dc21c670a..2a325ea2c 100644--- a/crates/ui-egui/src/file_dialog/tests.rs+++ b/crates/ui-egui/src/file_dialog/tests.rs@@ -329,10 +329,23 @@ fn scripts_browse_lists_only_script_files() {assert!(matches!(open.borrow().first().map(|(r, _)| r),Some(FileDialogRequest::Open { multiple: false, extensions: Some(exts), .. })- if exts.iter().map(String::as_str).collect::<Vec<_>>() == ["psjs", "ccjs", "jsx", "js", "jsxbin", "json", "txt"]+ if exts.iter().map(String::as_str).collect::<Vec<_>>() == ["psjs", "ccjs", "jsx", "js", "jsxbin", "json", "txt", "pcdroplet"]));}+#[test]+fn scripts_browse_extendscript_picks_report_the_error() {+ // Photoshop scripts stay in the picker (matching its filter) and choosing one shows why it won't run.+ let (mut app, open, _) = app();+ let ctx = egui::Context::default();+ menus::invoke(&mut app, &ctx, "file.scripts.browse", json!({})).unwrap();+ app.poll_file_dialog(&ctx, None);+ answer(&open, Some(FileDialogAnswer::Contents("resize.jsx".into(), b"alert('hi');".to_vec())));+ app.poll_file_dialog(&ctx, None);+ assert!(app.ui.status_error);+ assert!(app.ui.status.contains("not Photoshop ExtendScript"), "{}", app.ui.status);+}+#[test]fn last_used_dir_takes_the_most_recent_parent() {assert_eq!(last_used_dir(&[]), None);diff --git a/crates/ui-egui/src/file_ui.rs b/crates/ui-egui/src/file_ui.rsindex c3b8abc4d..3f87f6339 100644--- a/crates/ui-egui/src/file_ui.rs+++ b/crates/ui-egui/src/file_ui.rs@@ -55,8 +55,10 @@ fn form(app: &mut PhotocraftApp, command: &str, label: &str, fields: Value, choijson!({"dialog": app.ui.open_dialog(DialogKind::Command, f)})}-/// File › Scripts › Browse… lists these script files (plus the JSON/text the engine parses).-const SCRIPT_EXTENSIONS: &[&str] = &["psjs", "ccjs", "jsx", "js", "jsxbin", "json", "txt"];+/// File › Scripts › Browse… lists Photoshop's script types (matching its filter, #2452) plus the+/// JSON/text/droplet files the engine parses; selecting a Photoshop script gets the engine's+/// "not Photoshop ExtendScript" error instead of hiding those files from the picker.+const SCRIPT_EXTENSIONS: &[&str] = &["psjs", "ccjs", "jsx", "js", "jsxbin", "json", "txt", "pcdroplet"];/// Menu items fronted here (dialogs before the engine command). `None` when not ours.pub fn invoke(app: &mut PhotocraftApp, ctx: &egui::Context, id: &str, params: &Value) -> Option<Result<Value, String>> {@@ -104,7 +106,7 @@ pub fn invoke(app: &mut PhotocraftApp, ctx: &egui::Context, id: &str, params: &V"file.scripts.statistics" => json!({"dialog": crate::stack_dialog::open_statistics(app)}),"file.scripts.browse" => {return Some(app.pick_file_bytes_filtered(SCRIPT_EXTENSIONS, |app, name, bytes| {- let r = app.run("file.scripts.browse", json!({"script": String::from_utf8_lossy(&bytes)}));+ let r = app.run("file.scripts.browse", json!({"name": name, "script": String::from_utf8_lossy(&bytes)}));if r.is_ok() {let template = "Ran script {name}";app.ui.status = crate::i18n::fmt(tl!(template), &[("name", &name)]);