diff --git a/crates/engine/src/script/dom/save_open.rs b/crates/engine/src/script/dom/save_open.rs index c7c5eadd14..550a848721 100644 --- a/crates/engine/src/script/dom/save_open.rs +++ b/crates/engine/src/script/dom/save_open.rs @@ -12,7 +12,8 @@ //! describe. //! //! Each method runs a command (`file.saveAs`, `document.export`, `document.open`) inside the run's -//! transaction. The file is a `File` object, and the method reads and writes only where that +//! transaction and writes command warnings to the run's console. +//! The file is a `File` object, and the method reads and writes only where that //! `File` may ([`super::super::files`]): the path is checked before the command runs, and the //! command's own reads and writes leave out the files of the open documents that `vc.run`'s //! commands may use. @@ -663,6 +664,14 @@ fn exactly(h: &Host, f: impl FnOnce() -> Answer) -> Answer { r } +fn file_command(h: &Host, id: &str, params: &Value) -> Answer { + let (data, result) = super::command_value(h, id, params)?; + for warning in result.get("warnings").and_then(Value::as_array).into_iter().flatten().filter_map(Value::as_str) { + super::super::each_line(&format!("{id}: {warning}"), |line, cut| h.line(super::super::console::Entry::Log(line), cut)); + } + Ok(data) +} + /// The path to give a command for the real path `real`. fn plain(real: std::path::PathBuf) -> String { crate::cmd::findfiles::plain(real).to_string_lossy().into_owned() @@ -723,7 +732,7 @@ fn doc_save_as(h: &Host, a: &[Data]) -> Answer { } let real = h.disk.borrow().save_target(path).map_err(|m| refused(m, path))?; p.insert("path".into(), json!(plain(real))); - exactly(h, || on_doc(h, doc.uid, false, |h| command(h, "file.saveAs", &Value::Object(p))))?; + exactly(h, || on_doc(h, doc.uid, false, |h| file_command(h, "file.saveAs", &Value::Object(p))))?; Ok(Data::Undefined) } @@ -777,7 +786,7 @@ fn doc_export_file(h: &Host, a: &[Data]) -> Answer { let named = with_extension(path, &kind, svgz); let real = h.disk.borrow().save_target(&named).map_err(|m| refused(m, &named))?; p.insert("path".into(), json!(plain(real))); - exactly(h, || on_doc(h, doc.uid, false, |h| command(h, "document.export", &Value::Object(p))))?; + exactly(h, || on_doc(h, doc.uid, false, |h| file_command(h, "document.export", &Value::Object(p))))?; Ok(Data::Undefined) } @@ -823,7 +832,7 @@ fn app_open(h: &Host, a: &[Data]) -> Answer { } let real = h.disk.borrow().open_target(path).map_err(|m| refused(m, path))?; p.insert("path".into(), json!(plain(real))); - let r = exactly(h, || command(h, "document.open", &Value::Object(p)))?; + let r = exactly(h, || file_command(h, "document.open", &Value::Object(p)))?; let index = match &r { Data::Object(items) => items.iter().find_map(|(k, v)| match (k.as_str(), v) { ("index", Data::Number(n)) => whole(*n).and_then(|i| usize::try_from(i).ok()), @@ -923,7 +932,7 @@ fn doc_close(h: &Host, a: &[Data]) -> Answer { let name = super::with_doc(h, doc, |_, st| st.title()); let real = h.disk.borrow().save_target(&path).map_err(|m| refused(m, &name))?; let p = json!({"path": plain(real), "format": format}); - exactly(h, || on_doc(h, doc.uid, false, |h| command(h, "file.saveAs", &p)))?; + exactly(h, || on_doc(h, doc.uid, false, |h| file_command(h, "file.saveAs", &p)))?; } _ => return Err(HostError::Error(UNSAVED_CHANGES.into())), } diff --git a/crates/engine/src/script/dom/save_open/screens.rs b/crates/engine/src/script/dom/save_open/screens.rs index 07ef0aa324..825e509445 100644 --- a/crates/engine/src/script/dom/save_open/screens.rs +++ b/crates/engine/src/script/dom/save_open/screens.rs @@ -45,8 +45,8 @@ use serde_json::{Map, Value, json}; use vectorcraft_js::{Data, HostClass, HostError, HostFunction, HostMember}; use super::{ - Answer, Default, Host, Link, MAX_TEXT_BYTES, OptionClass, arg, boolean, builtin_preset_name, command, current, exactly, fresh, given, member, - method, named, num, on_doc, plain, refused, text, this_doc, typename, whole, whole_in, written_as_range, + Answer, Default, Host, Link, MAX_TEXT_BYTES, OptionClass, arg, boolean, builtin_preset_name, current, exactly, file_command, fresh, given, + member, method, named, num, on_doc, plain, refused, text, this_doc, typename, whole, whole_in, written_as_range, }; /// The enumerations these members use, as `dom`'s are made. @@ -474,7 +474,7 @@ fn doc_export_for_screens(h: &Host, a: &[Data]) -> Answer { for items in exports { let mut p = base.clone(); p.extend(items); - exactly(h, || on_doc(h, doc.uid, false, |h| command(h, "document.exportForScreens", &Value::Object(p))))?; + exactly(h, || on_doc(h, doc.uid, false, |h| file_command(h, "document.exportForScreens", &Value::Object(p))))?; } Ok(Data::Undefined) }) diff --git a/crates/engine/src/tests/script_save_open.rs b/crates/engine/src/tests/script_save_open.rs index 0c4efa0c58..2de01e9279 100644 --- a/crates/engine/src/tests/script_save_open.rs +++ b/crates/engine/src/tests/script_save_open.rs @@ -10,6 +10,34 @@ use crate::*; const DENIED: &str = "Script file access denied"; +#[test] +fn file_wrappers_report_encoder_warnings_without_changing_their_return_values() { + for method in ["exportFile", "saveAs", "exportForScreens"] { + let base = folder(&format!("encoder-warnings-{method}")); + let mut s = session(&base); + s.execute("file.new", &json!({"width": 80, "height": 40})).unwrap(); + crate::tests_menucmds::place_undecodable_png(&mut s); + let format = if method == "saveAs" { "pdf" } else { "png" }; + let expected = s.execute("document.export", &json!({"format": format})).unwrap()["warnings"].clone(); + let warnings = expected.as_array().unwrap(); + assert!(!warnings.is_empty(), "the fixture must produce an encoder warning"); + let dir = data_folder(&mut s, &base); + let path = quoted(&dir.join("output.png")); + let call = match method { + "exportFile" => format!("d.exportFile(new File({path}), ExportType.PNG24)"), + "saveAs" => format!("d.saveAs(new File({}))", quoted(&dir.join("output.pdf"))), + _ => format!("d.exportForScreens(new Folder({}), ExportForScreensType.SE_PNG24)", quoted(&dir.join("screens"))), + }; + let report = + run(&mut s, &base, &format!("Folder.userData; var d = app.activeDocument; var answer = {call}; vc.log(answer === undefined);")).unwrap(); + assert_eq!(report["log"], json!([true]), "{method}: {report}"); + let lines = report["console"]["lines"].as_array().unwrap(); + for warning in warnings.iter().filter_map(Value::as_str) { + assert!(lines.iter().filter_map(Value::as_str).any(|line| line.contains(warning)), "{method}: {warning}: {lines:?}"); + } + } +} + /// A fresh folder for test `name`, by its real path, with `main` for the script. fn folder(name: &str) -> PathBuf { let base = std::env::temp_dir().join(format!("vc-script-save-{name}-{}", std::process::id())); diff --git a/docs/mcp.md b/docs/mcp.md index 6d3caef969..c3fe069dfc 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -2907,6 +2907,7 @@ other files through `$`: each run. The enumerations `ExportType`, `Compatibility`, `PDFCompatibility`, `PDFBoxType`, `EPSPreview`, `EPSPostScriptLevelEnum`, `PDFOverprint`, `SaveOptions` and `UserInteractionLevel` hold strings such as `"ExportType.PNG24"`. + `saveAs`, `exportFile`, `exportForScreens`, and `app.open` write their command warnings to the run's console. VectorCraft's own rules: each path is checked as a `File` opened for reading or writing is checked, before the command runs. A path outside the script's folders throws `Script file access denied`, also when it is an open document's file, a link that leads outside them, or the settings folder; a read-only file throws `Script file is