From f1427bba65bdd36d86e8d855d2ae062c268f8180 Mon Sep 17 00:00:00 2001 From: Hector Villarreal Date: Thu, 5 Nov 2026 13:03:20 -0600 Subject: [PATCH] Save for Web: refuse slice numbers with a whole-image path (CodeRabbit review on crates/engine/src/cmd/web_cmds/save_web.rs:237) `path` alone writes the whole image as one file, so slice `numbers` with it were ignored. It is refused; `dir` exports the chosen slices. Main's `folder` destination (#5305) is the whole image too, so `folder` alone with `numbers` on a document with slices is refused the same way. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/cmd/web_cmds.rs | 2 +- crates/engine/src/cmd/web_cmds/save_web.rs | 10 +++++++ crates/engine/src/cmd/web_cmds/tests.rs | 34 ++++++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/cmd/web_cmds.rs b/crates/engine/src/cmd/web_cmds.rs index a206e6bc747..624250e5096 100644 --- a/crates/engine/src/cmd/web_cmds.rs +++ b/crates/engine/src/cmd/web_cmds.rs @@ -80,7 +80,7 @@ pub fn specs() -> Vec { "Save for Web (Legacy)…", &["File", "Export"], Some("Cmd+Alt+Shift+S"), - r##"{"preset":"GIF 128 Dithered|JPEG High|PNG-24|…"?,"format":"gif|png8|png24|jpeg|wbmp|webp"="png24","palette":"perceptual|selective|adaptive|restrictive|exact|systemMac|systemWindows|uniform"="selective","colors":2..256=256,"colorTable":["#rrggbb",…]? (fixed opaque source palette, 1–256 entries; 255 with transparency),"colorTableEdits":{"0":"#rrggbb",…}? (replace entries after indexing; requires colorTable),"dither":"none|diffusion|pattern|noise"="diffusion","ditherAmount":0..100=88,"transparency":bool=true,"matte":"#rrggbb|none"="none" (a preset's own: white; formats without transparency go over white),"interlaced":bool=false,"webSnap":0..100=0,"quality":0..100=60 (JPEG, lossy WebP),"lossless":bool=false (WebP),"progressive":bool=false,"optimized":bool=true,"embedIcc":bool=false,"metadata":"none|copyright|copyrightAndContact|allExceptCameraInfo|all"="copyright" (EXIF only with all),"convertToSrgb":bool=true,"width"|"height"|"percent"? (image size),"resample":"bicubic|bilinear|nearest"?,"path":file? (whole image),"folder":folder? (whole image, named after the document; ignored with "path" or "dir"),"dir":folder? (one file per slice in images/),"imagesFolder":relative-folder?="images","html":bool=false,"trustedSliceHtml":bool=false (only for trusted documents; emit raw no-image cell HTML),"slices":"all|user|selected"="all" (selected requires numbers when the document has slices),"numbers":[n]? (slice numbers, each an existing slice; chooses the slices whatever the mode)} → no path/dir: {bytes,width,height,colors,colorTable,transparentIndex} estimate; else {files, html, bytes, warnings}"##, + r##"{"preset":"GIF 128 Dithered|JPEG High|PNG-24|…"?,"format":"gif|png8|png24|jpeg|wbmp|webp"="png24","palette":"perceptual|selective|adaptive|restrictive|exact|systemMac|systemWindows|uniform"="selective","colors":2..256=256,"colorTable":["#rrggbb",…]? (fixed opaque source palette, 1–256 entries; 255 with transparency),"colorTableEdits":{"0":"#rrggbb",…}? (replace entries after indexing; requires colorTable),"dither":"none|diffusion|pattern|noise"="diffusion","ditherAmount":0..100=88,"transparency":bool=true,"matte":"#rrggbb|none"="none" (a preset's own: white; formats without transparency go over white),"interlaced":bool=false,"webSnap":0..100=0,"quality":0..100=60 (JPEG, lossy WebP),"lossless":bool=false (WebP),"progressive":bool=false,"optimized":bool=true,"embedIcc":bool=false,"metadata":"none|copyright|copyrightAndContact|allExceptCameraInfo|all"="copyright" (EXIF only with all),"convertToSrgb":bool=true,"width"|"height"|"percent"? (image size),"resample":"bicubic|bilinear|nearest"?,"path":file? (whole image),"folder":folder? (whole image, named after the document; ignored with "path" or "dir"),"dir":folder? (one file per slice in images/),"imagesFolder":relative-folder?="images","html":bool=false,"trustedSliceHtml":bool=false (only for trusted documents; emit raw no-image cell HTML),"slices":"all|user|selected"="all" (selected requires numbers when the document has slices),"numbers":[n]? (slice numbers, each an existing slice; chooses the slices whatever the mode; with `path` or `folder` alone on a document with slices, refused: use `dir`)} → no path/dir: {bytes,width,height,colors,colorTable,transparentIndex} estimate; else {files, html, bytes, warnings}"##, has_doc, save_for_web ), diff --git a/crates/engine/src/cmd/web_cmds/save_web.rs b/crates/engine/src/cmd/web_cmds/save_web.rs index b614e7689ef..db35af9c218 100644 --- a/crates/engine/src/cmd/web_cmds/save_web.rs +++ b/crates/engine/src/cmd/web_cmds/save_web.rs @@ -205,6 +205,16 @@ fn slice_pick(p: &Value, cmd: &str, doc: &photocraft_doc::Document) -> Result 1 { + return Err(bad(cmd, "`path` writes the whole image as one file: to export the slices in `numbers`, use `dir`")); + } + // `folder` alone (#5305) is the whole image too, under the document's name. + if given("folder") && !given("path") && !given("dir") && count > 1 { + return Err(bad(cmd, "`folder` writes the whole image as one file: to export the slices in `numbers`, use `dir`")); + } Ok(SlicePick::Numbers(numbers)) } diff --git a/crates/engine/src/cmd/web_cmds/tests.rs b/crates/engine/src/cmd/web_cmds/tests.rs index 1ea3b60107f..2a341c5e697 100644 --- a/crates/engine/src/cmd/web_cmds/tests.rs +++ b/crates/engine/src/cmd/web_cmds/tests.rs @@ -1547,3 +1547,37 @@ fn slice_modes_are_validated() { assert_eq!(decode(&path).dimensions(), (64, 48), "{mode}"); } } + +#[test] +fn a_whole_image_path_with_slice_numbers_is_refused() { + // CodeRabbit review on PR #10557: `path` alone writes the whole image as one file, so slice + // `numbers` with it were ignored and every slice came out in that file. It is refused; `dir` + // exports the chosen slices. + let dir = tmp("path-numbers"); + let (mut s, left, right) = two_slices(); + let path = format!("{dir}/web.png"); + assert_refused(&mut s, &dir, json!({"format": "png24", "path": path, "numbers": [right]}), "use `dir`"); + assert_refused(&mut s, &dir, json!({"format": "png24", "path": path, "dir": "", "numbers": [left, right]}), "use `dir`"); + // `folder` alone is the whole image under the document's name (#5305): refused the same way. + assert_refused(&mut s, &dir, json!({"format": "png24", "folder": dir, "numbers": [right]}), "use `dir`"); + // Valid calls still work: `path` alone is the whole image; with `slices`, the chosen slices + // go next to it; `dir` with `numbers` writes those slices. + let r = s.execute("file.export.saveForWebLegacy", json!({"format": "png24", "path": path})).unwrap(); + assert_eq!(r["files"], json!([path])); + assert_eq!(decode(&path).dimensions(), (64, 48)); + let names = |r: &Value| r["files"].as_array().unwrap().iter().map(|v| v.as_str().unwrap().rsplit('/').next().unwrap().to_string()).collect::>(); + let out = tmp("path-numbers-selected"); + let r = s + .execute("file.export.saveForWebLegacy", json!({"format": "png24", "path": format!("{out}/web.png"), "slices": "selected", "numbers": [right]})) + .unwrap(); + assert_eq!(names(&r), ["right.png"]); + let out = tmp("path-numbers-dir"); + let r = s.execute("file.export.saveForWebLegacy", json!({"format": "png24", "path": format!("{out}/web.png"), "dir": out, "numbers": [left]})).unwrap(); + assert_eq!(names(&r), ["left.png"]); + // A document without slices is one image: its only slice number is the whole image. + let mut s = session(8); + let out = tmp("path-numbers-none"); + let whole = format!("{out}/whole.png"); + s.execute("file.export.saveForWebLegacy", json!({"format": "png24", "path": whole, "numbers": [1]})).unwrap(); + assert_eq!(decode(&whole).dimensions(), (64, 48)); +}