feat: add recursive directory processing for ODT files - Add process_directory function, maintain directory structure, improve error handling
This commit is contained in:
parent
8f7558bac4
commit
8326aad265
74
src/main.rs
74
src/main.rs
@ -119,33 +119,61 @@ fn convert_file(odt_path: &Path, dest_dir: &Path) -> Result<(), String> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args = Args::parse();
|
||||
|
||||
// Create target directory if not exists
|
||||
if let Err(e) = fs::create_dir_all(&args.dest) {
|
||||
eprintln!("Error create target directory: {}", e);
|
||||
return;
|
||||
fn process_directory(source_dir: &Path, dest_dir: &Path) -> Result<(), String> {
|
||||
// Create destination directory if it doesn't exist
|
||||
if let Err(e) = fs::create_dir_all(dest_dir) {
|
||||
return Err(format!("Error creating destination directory: {}", e));
|
||||
}
|
||||
|
||||
// Process .odt files
|
||||
if let Ok(entries) = fs::read_dir(&args.source) {
|
||||
for entry in entries.flatten() {
|
||||
let path = entry.path();
|
||||
if path.extension().map_or(false, |ext| ext == "odt") {
|
||||
let pdf_path = args.dest.join(
|
||||
path.file_stem()
|
||||
.map(|s| s.to_string_lossy() + ".pdf")
|
||||
.unwrap_or_default()
|
||||
.as_ref(),
|
||||
);
|
||||
if needs_conversion(&path, &pdf_path) {
|
||||
match convert_file(&path, &args.dest) {
|
||||
Ok(_) => println!("Converted: {:?}", path.file_name().unwrap_or_default()),
|
||||
Err(e) => eprintln!("Error: {} - {}", path.display(), e),
|
||||
}
|
||||
let entries = fs::read_dir(source_dir)
|
||||
.map_err(|e| format!("Error reading source directory: {}", e))?;
|
||||
|
||||
for entry in entries.flatten() {
|
||||
let path = entry.path();
|
||||
|
||||
if path.is_dir() {
|
||||
// Get the relative path from source to the current subdirectory
|
||||
let relative_path = path.strip_prefix(source_dir)
|
||||
.map_err(|e| format!("Error getting relative path: {}", e))?;
|
||||
|
||||
// Create the corresponding destination subdirectory
|
||||
let dest_subdir = dest_dir.join(relative_path);
|
||||
|
||||
// Recursively process the subdirectory
|
||||
process_directory(&path, &dest_subdir)?;
|
||||
} else if path.extension().map_or(false, |ext| ext == "odt") {
|
||||
// Get the relative path from source to the current file
|
||||
let relative_path = path.strip_prefix(source_dir)
|
||||
.map_err(|e| format!("Error getting relative path: {}", e))?;
|
||||
|
||||
// Create the corresponding destination subdirectory if needed
|
||||
if let Some(parent) = relative_path.parent() {
|
||||
let dest_subdir = dest_dir.join(parent);
|
||||
if let Err(e) = fs::create_dir_all(&dest_subdir) {
|
||||
return Err(format!("Error creating destination subdirectory: {}", e));
|
||||
}
|
||||
}
|
||||
|
||||
// Construct the PDF path with the same directory structure
|
||||
let pdf_path = dest_dir.join(
|
||||
relative_path.with_extension("pdf")
|
||||
);
|
||||
|
||||
if needs_conversion(&path, &pdf_path) {
|
||||
match convert_file(&path, &pdf_path.parent().unwrap_or(dest_dir)) {
|
||||
Ok(_) => println!("Converted: {:?}", relative_path),
|
||||
Err(e) => eprintln!("Error: {} - {}", relative_path.display(), e),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args = Args::parse();
|
||||
|
||||
if let Err(e) = process_directory(&args.source, &args.dest) {
|
||||
eprintln!("Error processing directory: {}", e);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user