diff --git a/crates/engine/src/cmd/file.rs b/crates/engine/src/cmd/file.rs index cdf1acf1b..2406e3c75 100644 --- a/crates/engine/src/cmd/file.rs +++ b/crates/engine/src/cmd/file.rs @@ -466,6 +466,15 @@ pub fn writable_format_for_path(path: &str) -> Option<&'static str> { WRITABLE_EXTENSIONS.contains(&ext.as_str()).then(|| format_for_path(path)) } +/// The format a save path names, or an error listing the file types DeckCraft writes. +fn writable_format(path: &str) -> Result<&'static str> { + writable_format_for_path(path).ok_or_else(|| { + EngineError::Other(format!( + "{path}: DeckCraft cannot save this file type; save as .pptx, .potx, .ppsx, .deckcraft, .pdf, .png, .jpg/.jpeg, .gif, .txt, .md or .rtf" + )) + }) +} + /// Animated GIF bytes for `doc` with the options in `p` (see [`crate::gif_export::GifOptions::from_params`]). pub fn gif_bytes(doc: &Presentation, p: &Value) -> Result> { Ok(crate::gif_export::encode(doc, &crate::gif_export::GifOptions::from_params(p)?)?.bytes) @@ -523,7 +532,8 @@ fn save(s: &mut Session, p: &Value) -> Result { s.ui_requests.push(crate::UiRequest::PickFile { purpose: "saveAs".into() }); return Ok(json!({"needsPath": true})); }; - let format = str_param(p, "format").unwrap_or(format_for_path(&path)); + let path_format = writable_format(&path)?; + let format = str_param(p, "format").unwrap_or(path_format); let bytes = save_bytes(&s.doc()?.doc, format)?; write_file(&path, &bytes)?; mark_saved(s, Some(path.clone()))?; diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 52006b7a7..7d91a54f3 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -7452,6 +7452,52 @@ fn save_to_image_path_is_an_error_not_zip_bytes() { let _ = std::fs::remove_dir_all(&dir); } +#[test] +fn unsupported_save_paths_are_refused_and_write_nothing() { + let dir = std::env::temp_dir().join(format!("deckcraft-save-ext-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + for name in ["a.pptm", "a.ppt", "a.pot", "a.potm", "a.pps", "a.ppsm", "a.odp", "a.xml", "a.svg", "a.bmp", "a"] { + let mut s = Session::new(); + sample::open_sample(&mut s).unwrap(); + let path = dir.join(name).to_string_lossy().to_string(); + let before = s.doc().unwrap().path.clone(); + let err = s.execute("file.saveAs", &json!({"path": path})).unwrap_err(); + assert!(err.to_string().contains("cannot save"), "{name}: {err}"); + assert!(!std::path::Path::new(&path).exists(), "{name} must not be written"); + assert_eq!(s.doc().unwrap().path, before, "{name}: the document keeps its path"); + } + let _ = std::fs::remove_dir_all(&dir); +} + +#[test] +fn unsupported_paths_are_refused_even_with_an_explicit_format() { + let dir = std::env::temp_dir().join(format!("deckcraft-save-ext-fmt-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + let mut s = Session::new(); + sample::open_sample(&mut s).unwrap(); + let path = dir.join("a.pptm").to_string_lossy().to_string(); + let err = s.execute("file.saveAs", &json!({"path": path, "format": "pptx"})).unwrap_err(); + assert!(err.to_string().contains("cannot save"), "{err}"); + assert!(!std::path::Path::new(&path).exists(), "the path guard wins over the explicit format"); + let _ = std::fs::remove_dir_all(&dir); +} + +#[test] +fn unsupported_extensionless_save_is_refused_even_with_an_explicit_format() { + let dir = std::env::temp_dir().join(format!("deckcraft-save-extless-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + let mut s = Session::new(); + sample::open_sample(&mut s).unwrap(); + let path = dir.join("no-extension").to_string_lossy().to_string(); + let err = s.execute("file.saveAs", &json!({"path": path, "format": "pptx"})).unwrap_err(); + assert!(err.to_string().contains("cannot save"), "{err}"); + assert!(!std::path::Path::new(&path).exists(), "the path guard wins over the explicit format"); + let _ = std::fs::remove_dir_all(&dir); +} + /// `shape.connect` glues to a shape inside a group, as PowerPoint does: the connector's /// end is set to the grouped shape and follows it when the group moves. The glue was dropped at /// once (the end stayed `None`) because rerouting only looked at the slide's top-level shapes,