use walkdir::DirEntry;
use std::process::Command;
use std::io;
use std::fs::{self, OpenOptions};
use std::io::Write;
use std::path::Path;
use anyhow::Result;
use crate::indexer::config::config::indexable_extensions;
const MAX_FILE_SIZE_BYTES: u64 = 6 * 1024 * 1024;
pub fn is_indexable_file(entry: &DirEntry) -> bool {
let path = entry.path();
if !path.is_file() {
return false;
}
if let Ok(metadata) = fs::metadata(path) {
if metadata.len() > MAX_FILE_SIZE_BYTES {
return false;
}
} else {
return false;
}
if let Some(ext) = path.extension() {
let ext = ext.to_string_lossy().to_lowercase();
indexable_extensions().iter().any(|valid_ext| valid_ext == &ext)
} else {
false
}
}
pub fn save_to_file<T: serde::Serialize>(path: &Path, data: &T) -> Result<()> {
let temp_path = path.with_extension("tmp");
let mut file = OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open(&temp_path)?;
serde_json::to_writer_pretty(&mut file, data)?;
file.flush()?;
fs::rename(temp_path, path)?;
Ok(())
}
pub fn pdftotext(path: &Path) -> io::Result<String> {
let output = Command::new("pdftotext")
.arg(path)
.arg("-") .output()?;
if output.status.success() {
Ok(String::from_utf8_lossy(&output.stdout).to_string())
} else {
Err(io::Error::new(
io::ErrorKind::Other,
format!(
"pdftotext failed: {}",
String::from_utf8_lossy(&output.stderr)
),
))
}
}