use std::path::Path;
use std::process::{Command, Stdio};
use std::time::Duration;
#[cfg(feature = "shell-accel")]
use which;
use crate::core::extract::{single, ExtractedDocument, MAX_EXTRACTED_BYTES};
#[derive(Debug, Clone)]
pub struct Accelerator {
pub name: &'static str,
pub binary: &'static str,
pub args: &'static [&'static str],
pub extensions: &'static [&'static str],
pub priority: u8, }
pub fn accelerators() -> Vec<Accelerator> {
vec![
Accelerator {
name: "pdftotext",
binary: "pdftotext",
args: &["-layout", "-nopgbrk", "-"],
extensions: &["pdf"],
priority: 10,
},
Accelerator {
name: "pandoc",
binary: "pandoc",
args: &["-f", "auto", "-t", "plain", "--wrap=none", "-"],
extensions: &["docx", "epub", "html", "htm", "md", "markdown"],
priority: 20,
},
Accelerator {
name: "libreoffice",
binary: "libreoffice",
args: &[
"--headless",
"--convert-to",
"txt:Text",
"--outdir",
"/dev/stdout",
"-",
],
extensions: &["docx", "xlsx", "pptx", "odt", "ods", "odp"],
priority: 30,
},
]
}
#[cfg(feature = "shell-accel")]
pub fn find_binary(name: &str) -> Option<std::path::PathBuf> {
which::which(name).ok()
}
#[cfg(not(feature = "shell-accel"))]
pub fn find_binary(_name: &str) -> Option<std::path::PathBuf> {
None
}
pub fn extract_with_accelerator(
accelerator: &Accelerator,
path: &Path,
) -> anyhow::Result<Option<Vec<ExtractedDocument>>> {
let binary_path = find_binary(accelerator.binary);
let Some(binary_path) = binary_path else {
return Ok(None); };
let mut cmd = Command::new(&binary_path);
for arg in accelerator.args {
cmd.arg(arg);
}
cmd.stdin(Stdio::from(std::fs::File::open(path)?))
.stdout(Stdio::piped())
.stderr(Stdio::null());
let mut child = cmd.spawn()?;
let timeout = Duration::from_secs(30);
let start = std::time::Instant::now();
let output = loop {
match child.try_wait()? {
Some(_status) => break child.wait_with_output()?,
None if start.elapsed() >= timeout => {
let _ = child.kill();
let _ = child.wait();
return Ok(Some(single(
&path.to_string_lossy(),
accelerator.name,
Vec::new(),
vec!["accelerator timed out after 30s".to_string()],
)));
}
None => std::thread::sleep(Duration::from_millis(100)),
}
};
if !output.status.success() {
return Ok(Some(single(
&path.to_string_lossy(),
accelerator.name,
Vec::new(),
vec![format!("accelerator exited with {}", output.status)],
)));
}
let text = String::from_utf8_lossy(&output.stdout).to_string();
if text.trim().is_empty() {
return Ok(None);
}
let text = if text.len() > MAX_EXTRACTED_BYTES {
let mut cut = MAX_EXTRACTED_BYTES;
while cut > 0 && !text.is_char_boundary(cut) {
cut -= 1;
}
text[..cut].to_string()
} else {
text
};
Ok(Some(single(
&path.to_string_lossy(),
accelerator.name,
vec![crate::core::extract::Block::text(text)],
Vec::new(),
)))
}
pub fn try_accelerators(path: &Path, ext: &str) -> anyhow::Result<Option<Vec<ExtractedDocument>>> {
let mut accelerators = accelerators();
accelerators.retain(|a| a.extensions.contains(&ext));
accelerators.sort_by_key(|a| a.priority);
for accelerator in accelerators {
if let Ok(Some(docs)) = extract_with_accelerator(&accelerator, path) {
if !docs.is_empty() {
tracing::info!(
"extracted {} using accelerator: {}",
path.display(),
accelerator.name
);
return Ok(Some(docs));
}
}
}
Ok(None)
}