Skip to content

PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust

Merge PR #3765: Refuse an OCR batch whose inputs would write one output file

aiongg23d2e1c8237e
diff --cc crates/automation/src/a11y.rs
index ced65f002,833e5de9f..e1be76937
--- a/crates/automation/src/a11y.rs
+++ b/crates/automation/src/a11y.rs
@@@ -610,11 -616,11 +621,11 @@@ impl Automation
let ocr = pdfcraft_engine::ocr::engine().map_err(failed)?;
let mut out = Vec::new();
for p in paths {
- let name = std::path::Path::new(p).file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_else(|| "document.pdf".into());
+ let name = input_file_name(p);
let result =
- self.resolve(p, false).map_err(|e| e.to_string()).and_then(|src| std::fs::read(&src).map_err(|e| e.to_string())).and_then(|bytes| {
- pdfcraft_engine::ocr::recognize_file(&name, std::sync::Arc::new(bytes), None, settings.clone(), &ocr, |_, _| true)
- });
+ self.resolve(p, false).map_err(|e| e.to_string()).and_then(|src| crate::read_input_bytes(&src).map_err(|e| e.to_string())).and_then(
+ |bytes| pdfcraft_engine::ocr::recognize_file(&name, std::sync::Arc::new(bytes), None, settings.clone(), &ocr, |_, _| true),
+ );
out.push(match result {
Ok(r) => {
let target = child(&folder, &name);
@@@ -659,8 -661,36 +670,38 @@@
}
}
+ /// The file name an input is written under: its own file name, or `document.pdf` when it has none.
+ fn input_file_name(path: &str) -> String {
+ std::path::Path::new(path).file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_else(|| "document.pdf".into())
+ }
+
+ /// The output file names that two or more of `targets` share, each spelling listed once, in input
+ /// order. Names are compared the way the default filesystem compares them: without regard to case
+ /// on Windows and macOS and exactly elsewhere; on Windows a trailing dot or space is not part of
+ /// the name (`scan.pdf.` is `scan.pdf`). The folder is the same for every target, so only the
+ /// names are compared.
+ fn shared_file_names(targets: &[std::path::PathBuf]) -> Vec<String> {
+ let mut groups: std::collections::BTreeMap<String, (usize, Vec<String>)> = std::collections::BTreeMap::new();
+ for target in targets {
+ let name = target.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default();
+ let key = if cfg!(windows) {
+ name.trim_end_matches(['.', ' ']).to_lowercase()
+ } else if cfg!(target_os = "macos") {
+ name.to_lowercase()
+ } else {
+ name.clone()
+ };
+ let (count, spellings) = groups.entry(key).or_insert_with(|| (0, Vec::new()));
+ *count += 1;
+ if !spellings.contains(&name) {
+ spellings.push(name);
+ }
+ }
+ groups.into_values().filter(|(count, _)| *count > 1).flat_map(|(_, spellings)| spellings).collect()
+ }
+
+/// A script's request to the viewer as the JSON that running a script reports; a side panel
+/// goes by its fixed id.
fn request_json(r: &pdfcraft_engine::js::Request) -> Value {
use pdfcraft_engine::js::Request as R;
match r {