Skip to content

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

Fixes #8519 quick export honours extension (#8535)

Martin Ball4bfeb7889da4
diff --git a/contributors/people.toml b/contributors/people.toml
index b3cab8e04..42470ea22 100644
--- a/contributors/people.toml
+++ b/contributors/people.toml
@@ -191,3 +191,6 @@ sync_github_name = true
[people.ha1des]
sync_github_name = true
+
+[people.benandez]
+real_name = "Martin Ball"
diff --git a/crates/engine/src/web_cmds.rs b/crates/engine/src/web_cmds.rs
index f77a02081..9ccb5e02b 100644
--- a/crates/engine/src/web_cmds.rs
+++ b/crates/engine/src/web_cmds.rs
@@ -1086,6 +1086,14 @@ fn quick_export(s: &mut Session, p: &Value) -> Result<Value> {
target
}
};
+ // The name typed in the save dialog wins over Export Preferences' format: Quick Export as PNG
+ // renamed to `photo.jpeg` must write a JPEG, not PNG bytes upload checks refuse (#8519).
+ let fmt = match crate::file_cmds::extension(&path).as_deref() {
+ None => fmt,
+ Some("jpg" | "jpeg") => "jpg".into(),
+ Some(e @ ("png" | "gif" | "webp")) => e.into(),
+ Some(e) => return Err(bad(cmd, format!("Quick Export writes PNG, JPEG, GIF or WebP, not .{e}; use Export As"))),
+ };
let d = s.active().ok_or(EngineError::NoDocument)?;
let doc = d.doc.clone();
let metadata = serde_json::to_value(prefs.metadata).ok().and_then(|v| v.as_str().map(str::to_string)).unwrap_or_default();
diff --git a/crates/engine/src/web_cmds/tests.rs b/crates/engine/src/web_cmds/tests.rs
index ea8fe4017..cf45dbb36 100644
--- a/crates/engine/src/web_cmds/tests.rs
+++ b/crates/engine/src/web_cmds/tests.rs
@@ -466,6 +466,27 @@ fn quick_export_never_replaces_the_documents_own_file_unasked() {
s.execute("prefs.reset", json!({"path": "export"})).unwrap();
}
+/// #8519: Quick Export as PNG renamed to `photo.jpeg` in the save dialog wrote PNG bytes under a
+/// JPEG name, which upload checks refuse. A Quick Export extension picks the format; another
+/// image extension is refused; no extension keeps Export Preferences' format.
+#[test]
+fn quick_export_writes_the_format_its_path_names() {
+ let dir = tmp("quick_ext");
+ let mut s = session(8);
+ s.execute("file.export.exportPreferences", json!({"quickExportFormat": "png"})).unwrap();
+ for (name, format, magic) in [("a.jpeg", "jpg", &b"\xFF\xD8\xFF"[..]), ("b.JPG", "jpg", b"\xFF\xD8\xFF"), ("c.gif", "gif", b"GIF8"), ("d.webp", "webp", b"RIFF"), ("e", "png", b"\x89PNG")] {
+ let path = format!("{dir}/{name}");
+ let r = s.execute("file.export.quickExport", json!({"path": path})).unwrap();
+ assert_eq!(r["format"], json!(format), "{name}");
+ assert!(std::fs::read(&path).unwrap().starts_with(magic), "{name} holds {format} data");
+ }
+ let tif = format!("{dir}/f.tif");
+ let e = s.execute("file.export.quickExport", json!({"path": tif})).unwrap_err().to_string();
+ assert!(e.contains("Export As"), "{e}");
+ assert!(!std::path::Path::new(&tif).exists());
+ s.execute("prefs.reset", json!({"path": "export"})).unwrap();
+}
+
#[test]
fn same_file_sees_through_other_names_for_one_file() {
let dir = tmp("same_file");