mirror of
https://github.com/storytold/photocraft.git
synced 2026-11-21 01:18:43 +00:00
Automation: doc.save rejects a bad quality instead of ignoring or clamping it (#7023)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
5f65579ecb
commit
c6ea59461f
3 files changed
+42
-6
No files matched your search
@@ -114,10 +114,19 @@ impl Headless {
|
||||
let mut opts =
|
||||
photocraft_io::ExportOptions { tiff_layers: p.get("tiffLayers").and_then(Value::as_bool).unwrap_or(false), tga_bits, ..Default::default() };
|
||||
opts.encode.dpx_bits = photocraft_engine::file_cmds::dpx_bits_param(&p, "doc.save").map_err(|e| bad(e.to_string()))?;
|
||||
if let Some(q) = p.get("quality").and_then(Value::as_u64) {
|
||||
opts.encode.jpeg_quality = q.clamp(1, 100) as u8;
|
||||
opts.encode.webp_quality = q.clamp(1, 100) as u8;
|
||||
opts.encode.webp_lossless = false;
|
||||
match p.get("quality") {
|
||||
None | Some(Value::Null) => {}
|
||||
Some(v) => {
|
||||
// Like the CLI's `--quality`: never ignore or clamp a bad value.
|
||||
let q = v
|
||||
.as_u64()
|
||||
.filter(|q| (1..=100).contains(q))
|
||||
.and_then(|q| u8::try_from(q).ok())
|
||||
.ok_or_else(|| bad(format!("`quality` must be a whole number from 1 to 100, got {v}")))?;
|
||||
opts.encode.jpeg_quality = q;
|
||||
opts.encode.webp_quality = q;
|
||||
opts.encode.webp_lossless = false;
|
||||
}
|
||||
}
|
||||
let path = str_of(&p, "path").map(PathBuf::from);
|
||||
self.save(index_of(&p)?, path.as_deref(), str_of(&p, "format"), &opts)
|
||||
@@ -576,6 +585,26 @@ mod tests {
|
||||
std::fs::remove_dir_all(dir).unwrap();
|
||||
}
|
||||
|
||||
/// Regression: `quality` 85.0 or "85" was ignored (a lossless WebP, reply ok) and 0 or 500 was
|
||||
/// clamped, where the CLI refuses anything but a whole number from 1 to 100.
|
||||
#[test]
|
||||
fn doc_save_rejects_a_bad_quality_instead_of_ignoring_or_clamping_it() {
|
||||
let dir = std::env::temp_dir().join(format!("pc-rpc-save-quality-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let path = dir.join("x.webp");
|
||||
let mut h = Headless::trusted_local();
|
||||
h.handle("doc.new", json!({"width": 16, "height": 16})).unwrap();
|
||||
for q in [json!(85.0), json!("85"), json!(0), json!(500), json!(-5), json!(true)] {
|
||||
let err = h.handle("doc.save", json!({"path": path.to_string_lossy(), "quality": q})).unwrap_err().to_string();
|
||||
assert!(err.contains("quality"), "{q}: {err}");
|
||||
assert!(!path.exists(), "{q} wrote a file");
|
||||
}
|
||||
h.handle("doc.save", json!({"path": path.to_string_lossy(), "quality": 85})).unwrap();
|
||||
let bytes = std::fs::read(&path).unwrap();
|
||||
assert_eq!(bytes.get(12..16), Some(&b"VP8 "[..]), "a WebP saved with a quality is lossy");
|
||||
std::fs::remove_dir_all(dir).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn automation_preview_rejects_oversized_full_size_and_numeric_wraparound() {
|
||||
let mut h = Headless::new();
|
||||
|
||||
@@ -1001,6 +1001,9 @@ impl PhotocraftMcp {
|
||||
Some((_, Some(bits))) => Some(bits),
|
||||
Some((b, None)) => return Ok(fail(format!("`dpxBits` must be 8, 10, 12 or 16, got {b}"))),
|
||||
};
|
||||
if let Some(q) = p.quality.filter(|q| !(1..=100).contains(q)) {
|
||||
return Ok(fail(format!("`quality` must be a whole number from 1 to 100, got {q}")));
|
||||
}
|
||||
if let Some(b) = self.bridge_client() {
|
||||
// The bridge forwards to the running app's `app.save`, which takes only an optional
|
||||
// path (without one it writes back to the document's own layered file, as headless
|
||||
@@ -1033,8 +1036,8 @@ impl PhotocraftMcp {
|
||||
let mut opts = photocraft_io::ExportOptions { tiff_layers: p.tiff_layers, tga_bits, ..Default::default() };
|
||||
opts.encode.dpx_bits = dpx_bits;
|
||||
if let Some(q) = p.quality {
|
||||
opts.encode.jpeg_quality = q.clamp(1, 100);
|
||||
opts.encode.webp_quality = q.clamp(1, 100);
|
||||
opts.encode.jpeg_quality = q;
|
||||
opts.encode.webp_quality = q;
|
||||
opts.encode.webp_lossless = false;
|
||||
}
|
||||
let path = p.path.map(PathBuf::from);
|
||||
|
||||
@@ -318,6 +318,10 @@ async fn errors_are_tool_errors_not_crashes() {
|
||||
assert!(text(&r).contains("bridge"));
|
||||
let r = call(&client, "doc_export", json!({})).await;
|
||||
assert_eq!(r.is_error, Some(true));
|
||||
// Quality 0 is refused, not clamped to 1 (serde already refuses 500, -1 and "85").
|
||||
let r = call(&client, "doc_save", json!({"path": "q.jpg", "quality": 0})).await;
|
||||
assert_eq!(r.is_error, Some(true));
|
||||
assert!(text(&r).contains("from 1 to 100"), "{}", text(&r));
|
||||
// Server still alive.
|
||||
json_of(&call(&client, "session_list", json!({})).await);
|
||||
client.cancel().await.unwrap();
|
||||
|
||||
Reference in new issue
Block a user