mirror of
https://github.com/storytold/deckcraft.git
synced 2026-12-02 10:38:15 -06:00
fix: reject save formats that conflict with the path (#2383)
Signed-off-by: MaheshDesu <mahesh.desu3@gmail.com>
This commit is contained in:
@@ -476,6 +476,18 @@ fn writable_format(path: &str) -> Result<&'static str> {
|
||||
})
|
||||
}
|
||||
|
||||
/// Whether an explicit save format names the same writer as the path. `txt`/`outline` are the
|
||||
/// same writer only for a `.txt` path, and `jpg`/`jpeg` are aliases; every other mismatch would
|
||||
/// write bytes that lie about the file type.
|
||||
fn same_save_format(path: &str, path_format: &str, format: &str) -> bool {
|
||||
if format == path_format {
|
||||
return true;
|
||||
}
|
||||
let lower = path.to_ascii_lowercase();
|
||||
(lower.ends_with(".txt") && matches!((path_format, format), ("outline", "txt") | ("txt", "outline")))
|
||||
|| matches!((path_format, format), ("jpeg", "jpg") | ("jpg", "jpeg"))
|
||||
}
|
||||
|
||||
/// 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<Vec<u8>> {
|
||||
Ok(crate::gif_export::encode(doc, &crate::gif_export::GifOptions::from_params(p)?)?.bytes)
|
||||
@@ -535,6 +547,11 @@ fn save(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
};
|
||||
let path_format = writable_format(&path)?;
|
||||
let format = str_param(p, "format").unwrap_or(path_format);
|
||||
if !same_save_format(&path, path_format, format) {
|
||||
return Err(EngineError::Other(format!(
|
||||
"{path}: format `{format}` does not match the file type; save as {path_format} or choose a matching extension"
|
||||
)));
|
||||
}
|
||||
let bytes = save_bytes(&s.doc()?.doc, format)?;
|
||||
write_file(&path, &bytes)?;
|
||||
mark_saved(s, Some(path.clone()))?;
|
||||
|
||||
@@ -7552,6 +7552,53 @@ fn supported_paths_still_save() {
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn explicit_save_format_must_match_the_path() {
|
||||
let dir = std::env::temp_dir().join(format!("deckcraft-save-mismatch-{}", 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.pptx").to_string_lossy().to_string();
|
||||
std::fs::write(&path, b"sentinel").unwrap();
|
||||
let before = s.doc().unwrap().path.clone();
|
||||
let err = s.execute("file.saveAs", &json!({"path": path, "format": "deckcraft"})).unwrap_err();
|
||||
assert!(err.to_string().contains("does not match"), "{err}");
|
||||
assert_eq!(std::fs::read(&path).unwrap(), b"sentinel", "the existing file is untouched");
|
||||
assert_eq!(s.doc().unwrap().path, before, "the document keeps its path");
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn existing_unsupported_destination_is_untouched() {
|
||||
let dir = std::env::temp_dir().join(format!("deckcraft-save-existing-{}", 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();
|
||||
std::fs::write(&path, b"sentinel").unwrap();
|
||||
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"), "{err}");
|
||||
assert_eq!(std::fs::read(&path).unwrap(), b"sentinel", "the existing file is untouched");
|
||||
assert_eq!(s.doc().unwrap().path, before, "the document keeps its path");
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn explicit_txt_format_is_allowed_for_a_txt_path() {
|
||||
let dir = std::env::temp_dir().join(format!("deckcraft-save-txt-alias-{}", 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("outline.txt").to_string_lossy().to_string();
|
||||
s.execute("file.saveAs", &json!({"path": path, "format": "txt"})).unwrap();
|
||||
assert!(!std::fs::read(&path).unwrap().is_empty());
|
||||
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,
|
||||
|
||||
Reference in New Issue
Block a user