mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-12-06 05:53:49 -05:00
M10: refuse an out-of-range page_size before out_dir is made (#3273)
image_page validated the shape of page_size but not its magnitude, so [14401, 792] passed it and only the conversion refused the page. In mode "separate" that is a side effect ahead of the check: create_separate makes out_dir first and then fails once per file, leaving an empty folder behind. The comment there claimed a bad page_size left nothing behind, which was only true of a malformed one. ImagePage::check already applies the 3 to 14400 point limit (ISO 32000-2 Annex C), so make it public and call it from image_page rather than repeat the number: an unmakeable page is the caller's argument being wrong, so it is InvalidArgs, and every creation path shares the one helper. The tool schema gains the maximum it was documenting only as a minimum. Tests: the existing refusal test gains [14401, 792], [612, 2] and [-612, 792], and already asserts that no file and no folder is left behind. Removing the check reds it with `doc_create: [14401,792] should be refused`. cargo fmt --check, clippy -D warnings, pdfcraft-create (52) and pdfcraft-automation (245 + 37) tests, xtask layers (37 crates) and xtask assets all pass.
This commit is contained in:
4 files changed
+24
-12
No files matched your search
@@ -10,15 +10,19 @@ impl Automation {
|
||||
fn image_page(a: &Args) -> Result<pdfcraft_engine::ImagePage> {
|
||||
let turn = a.opt_bool("page_turn")?.unwrap_or(true);
|
||||
let bad = || ToolError::InvalidArgs("page_size must be a paper name or [width, height] in points".into());
|
||||
match a.get("page_size") {
|
||||
None | Some(Value::Null) => Ok(pdfcraft_engine::ImagePage::Image),
|
||||
Some(Value::String(s)) => Ok(pdfcraft_engine::ImagePage::fixed(super::printing::paper_named(Some(s))?.1, turn)),
|
||||
let page = match a.get("page_size") {
|
||||
None | Some(Value::Null) => pdfcraft_engine::ImagePage::Image,
|
||||
Some(Value::String(s)) => pdfcraft_engine::ImagePage::fixed(super::printing::paper_named(Some(s))?.1, turn),
|
||||
Some(Value::Array(v)) if v.len() == 2 => {
|
||||
let side = |i: usize| v.get(i).and_then(Value::as_f64).ok_or_else(bad);
|
||||
Ok(pdfcraft_engine::ImagePage::fixed((side(0)?, side(1)?), turn))
|
||||
pdfcraft_engine::ImagePage::fixed((side(0)?, side(1)?), turn)
|
||||
}
|
||||
Some(_) => Err(bad()),
|
||||
}
|
||||
Some(_) => return Err(bad()),
|
||||
};
|
||||
// A page the creator cannot make is the caller's argument being wrong, not a conversion
|
||||
// failure, and refusing it here is what keeps mode "separate" from making out_dir first.
|
||||
page.check().map_err(|e| ToolError::InvalidArgs(e.to_string()))?;
|
||||
Ok(page)
|
||||
}
|
||||
|
||||
pub(crate) fn doc_create(&mut self, a: &Args) -> Result<Value> {
|
||||
|
||||
@@ -276,7 +276,7 @@ pub fn tools() -> Vec<ToolDef> {
|
||||
"out": save_out.clone(),
|
||||
"open": open.clone(),
|
||||
"out_dir": { "type": "string", "description": "Folder for mode \"separate\"." },
|
||||
"page_size": { "type": ["string", "array"], "items": { "type": "number", "minimum": 3 }, "minItems": 2, "maxItems": 2, "description": "For image files: put each image on a sheet of this size instead of a page as large as the image. A paper name (A4, Letter, Legal, Tabloid, A0-A6, ARCH A-E, ANSI C-E) or [width, height] in points; the image is scaled to fit and centred." },
|
||||
"page_size": { "type": ["string", "array"], "items": { "type": "number", "minimum": 3, "maximum": 14400 }, "minItems": 2, "maxItems": 2, "description": "For image files: put each image on a sheet of this size instead of a page as large as the image. A paper name (A4, Letter, Legal, Tabloid, A0-A6, ARCH A-E, ANSI C-E) or [width, height] in points, each between 3 and 14400 (the largest a PDF page may be); the image is scaled to fit and centred." },
|
||||
"page_turn": { "type": "boolean", "description": "With page_size: turn the sheet to match each image's orientation, so a landscape photo gets a landscape page (default true)." },
|
||||
}),
|
||||
&["paths"],
|
||||
@@ -1340,7 +1340,7 @@ pub fn tools() -> Vec<ToolDef> {
|
||||
"width": { "type": "number", "minimum": 3 },
|
||||
"height": { "type": "number", "minimum": 3 },
|
||||
"name": { "type": "string", "description": "Name for the new document (default derived from the source)." },
|
||||
"page_size": { "type": ["string", "array"], "items": { "type": "number", "minimum": 3 }, "minItems": 2, "maxItems": 2, "description": "For image files: put each image on a sheet of this size instead of a page as large as the image. A paper name (A4, Letter, Legal, Tabloid, A0-A6, ARCH A-E, ANSI C-E) or [width, height] in points; the image is scaled to fit and centred." },
|
||||
"page_size": { "type": ["string", "array"], "items": { "type": "number", "minimum": 3, "maximum": 14400 }, "minItems": 2, "maxItems": 2, "description": "For image files: put each image on a sheet of this size instead of a page as large as the image. A paper name (A4, Letter, Legal, Tabloid, A0-A6, ARCH A-E, ANSI C-E) or [width, height] in points, each between 3 and 14400 (the largest a PDF page may be); the image is scaled to fit and centred." },
|
||||
"page_turn": { "type": "boolean", "description": "With page_size: turn the sheet to match each image's orientation, so a landscape photo gets a landscape page (default true)." },
|
||||
|
||||
}),
|
||||
|
||||
@@ -9465,9 +9465,12 @@ fn image_pages_go_on_a_chosen_paper_size_through_every_create_tool() {
|
||||
let one = ok(&mut a, "doc_open", json!({ "path": "out/a_page_1.pdf" }))["doc"].as_u64().unwrap();
|
||||
assert!(close(size(&mut a, one, 0), A4));
|
||||
|
||||
// A size that is not a paper name or a pair of points is refused, by every tool that takes
|
||||
// one and in both of doc_create_multiple's modes, before anything is written.
|
||||
for bad in [json!("A9"), json!([300]), json!(["a", "b"]), json!(5), json!({})] {
|
||||
// A size no page can be is refused the same way: not a paper name, not a pair of points, or
|
||||
// a pair outside the 3 to 14400 points a PDF page may be (ISO 32000-2 Annex C). Checked for
|
||||
// every tool that takes one and in both of doc_create_multiple's modes, because "separate"
|
||||
// makes out_dir before it converts anything and a size reported only by the conversion would
|
||||
// leave the folder behind.
|
||||
for bad in [json!("A9"), json!([300]), json!(["a", "b"]), json!(5), json!({}), json!([14401, 792]), json!([612, 2]), json!([-612, 792])] {
|
||||
for (tool, args) in [
|
||||
("doc_create", json!({ "from": "images", "paths": shots, "page_size": bad.clone() })),
|
||||
("doc_create_multiple", json!({ "paths": shots, "page_size": bad.clone(), "out": "refused.pdf" })),
|
||||
|
||||
@@ -684,7 +684,12 @@ impl ImagePage {
|
||||
Self::Fixed { width: size.0, height: size.1, turn }
|
||||
}
|
||||
|
||||
fn check(self) -> Result<(), CreateError> {
|
||||
/// Refuse a page this crate cannot make.
|
||||
///
|
||||
/// Public so a caller that acts before converting can refuse the size before the side effect:
|
||||
/// `doc_create_multiple` in mode "separate" makes `out_dir` first, and an out-of-range size
|
||||
/// reported only by the conversion would leave the folder behind.
|
||||
pub fn check(self) -> Result<(), CreateError> {
|
||||
let Self::Fixed { width, height, .. } = self else { return Ok(()) };
|
||||
if !(width.is_finite() && height.is_finite() && (3.0..=MAX_SIDE).contains(&width) && (3.0..=MAX_SIDE).contains(&height)) {
|
||||
return Err(CreateError::Invalid(format!("the page must be between 3 and {MAX_SIDE} points on each side")));
|
||||
|
||||
Reference in new issue
Block a user