PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
Shorten over-long part file names in doc_split by bookmarks
NetterSilver7cc073ab89f0
diff --git a/crates/automation/src/lib.rs b/crates/automation/src/lib.rsindex 88856305b..3cdc64217 100644--- a/crates/automation/src/lib.rs+++ b/crates/automation/src/lib.rs@@ -1746,16 +1746,21 @@ impl Automation {let mut used = std::collections::HashSet::new();for (i, (first, last, bytes)) in parts.iter().enumerate() {let safe = |t: &str| t.chars().map(|c| if c.is_alphanumeric() || " -_.,()".contains(c) { c } else { '_' }).collect::<String>();- let file = match titles.iter().find(|t| t.0 + 1 == *first) {- Some((_, t)) => format!("{stem}-{}.pdf", safe(t)),- None => format!("{stem}-part{}.pdf", i + 1),+ let mut base = match titles.iter().find(|t| t.0 + 1 == *first) {+ Some((_, t)) => format!("{stem}-{}", safe(t)),+ None => format!("{stem}-part{}", i + 1),};+ // A file name is at most 255 bytes: keep the base to 200, cut between characters, which leaves room for ".pdf" and a -N suffix.+ if let Some((end, _)) = base.char_indices().find(|(at, c)| at + c.len_utf8() > 200) {+ base.truncate(end);+ }+ let file = format!("{base}.pdf");// Equal titles (or titles that sanitize alike) get -2, -3, ... so no part overwrites another; compared case-insensitively.- let mut unique = file.clone();+ let mut unique = file;let mut n = 1;while !used.insert(unique.to_lowercase()) {n += 1;- unique = format!("{}-{n}.pdf", file.trim_end_matches(".pdf"));+ unique = format!("{base}-{n}.pdf");}let path = child(&dir, &unique);write_atomic(&path, bytes)?;diff --git a/crates/automation/tests/automation.rs b/crates/automation/tests/automation.rsindex 1e662d8f3..ed5280567 100644--- a/crates/automation/tests/automation.rs+++ b/crates/automation/tests/automation.rs@@ -3806,6 +3806,35 @@ fn bookmark_split_with_equal_titles_keeps_every_part() {}}+#[test]+fn bookmark_split_with_long_titles_shortens_the_file_names() {+ let dir = workdir("organize_long_titles");+ let mut a = auto(&dir);+ let doc = ok(&mut a, "doc_open", json!({ "path": "a.pdf" }))["doc"].as_u64().unwrap();+ // Two equal titles far over the 255-byte name limit, then a multibyte one (3 bytes a character).+ let long = format!("Chapter {}", "x".repeat(300));+ ok(&mut a, "bookmark_add", json!({ "doc": doc, "title": long, "page": 1 }));+ ok(&mut a, "bookmark_add", json!({ "doc": doc, "title": long, "page": 2 }));+ ok(&mut a, "bookmark_add", json!({ "doc": doc, "title": "章".repeat(100), "page": 3 }));+ let s = ok(&mut a, "doc_split", json!({ "doc": doc, "bookmarks": true, "out_dir": "parts" }));+ let files: Vec<String> = s["files"]+ .as_array()+ .unwrap()+ .iter()+ .map(|f| std::path::Path::new(f["path"].as_str().unwrap()).file_name().unwrap().to_string_lossy().into_owned())+ .collect();+ assert_eq!(files.len(), 3, "{files:?}");+ let distinct: std::collections::HashSet<_> = files.iter().map(|f| f.to_lowercase()).collect();+ assert_eq!(distinct.len(), 3, "{files:?}");+ for f in &files {+ assert!(f.len() <= 255, "{f} is {} bytes", f.len());+ assert!(f.ends_with(".pdf"), "{f}");+ assert!(dir.join("parts").join(f).exists(), "{f} missing");+ }+ assert!(files[0].starts_with("a-Chapter x"), "{files:?}");+ assert!(files[2].starts_with("a-章"), "{files:?}");+}+#[test]fn organizing_with_filters_bookmark_splits_and_extract_options() {let dir = workdir("organize2");