fix: correct path handling in directory processing

- Fix duplicate subdirectory creation bug when processing nested directories
- Change relative path calculation to use current directory instead of root
- Remove redundant subdirectory creation code
- Improve path display in logs to show cleaner relative paths
This commit is contained in:
Jesús Pérex 2025-05-23 19:49:11 +01:00
parent 4bfd43e1e0
commit a0b78e3c7e
2 changed files with 10 additions and 7 deletions

View File

@ -3,7 +3,7 @@ use std::fs;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::time::UNIX_EPOCH; use std::time::UNIX_EPOCH;
use crate::tools; use crate::tools;
use crate::FILES_TO_COPY; use crate::{FILES_TO_COPY, FILES_TO_CONVERT};
pub struct DirectoryProcessor { pub struct DirectoryProcessor {
source_dir: PathBuf, source_dir: PathBuf,
@ -45,7 +45,7 @@ impl DirectoryProcessor {
if path.is_dir() { if path.is_dir() {
self.get_source_files(&path)?; self.get_source_files(&path)?;
} else if let Some(ext) = path.extension().and_then(|e| e.to_str()) { } else if let Some(ext) = path.extension().and_then(|e| e.to_str()) {
if ext == "odt" || FILES_TO_COPY.contains(&ext.to_lowercase().as_str()) { if FILES_TO_CONVERT.contains(&ext.to_lowercase().as_str()) || FILES_TO_COPY.contains(&ext.to_lowercase().as_str()) {
if let Ok(rel_path) = path.strip_prefix(&self.source_dir) { if let Ok(rel_path) = path.strip_prefix(&self.source_dir) {
self.source_files.insert(rel_path.to_path_buf()); self.source_files.insert(rel_path.to_path_buf());
} }
@ -83,8 +83,10 @@ impl DirectoryProcessor {
// Check if this file should exist based on source files // Check if this file should exist based on source files
let should_exist = if let Some(ext) = rel_path.extension().and_then(|e| e.to_str()) { let should_exist = if let Some(ext) = rel_path.extension().and_then(|e| e.to_str()) {
if ext == "pdf" { if ext == "pdf" {
// For PDF files, check if corresponding ODT exists in source // For PDF files, check if any corresponding source file exists
self.source_files.contains(&rel_path.with_extension("odt")) FILES_TO_CONVERT.iter().any(|&ext| {
self.source_files.contains(&rel_path.with_extension(ext))
})
} else { } else {
// For other files, check if they exist in source // For other files, check if they exist in source
self.source_files.contains(rel_path) self.source_files.contains(rel_path)
@ -135,7 +137,7 @@ impl DirectoryProcessor {
let relative_path = path.strip_prefix(current_source) let relative_path = path.strip_prefix(current_source)
.map_err(|e| format!("Error getting relative path: {}", e))?; .map_err(|e| format!("Error getting relative path: {}", e))?;
if ext == "odt" { if FILES_TO_CONVERT.contains(&ext.to_lowercase().as_str()) {
// Construct the PDF path in the current target directory // Construct the PDF path in the current target directory
let pdf_path = current_target.join( let pdf_path = current_target.join(
relative_path.with_extension("pdf") relative_path.with_extension("pdf")

View File

@ -7,15 +7,16 @@ mod directory_processor;
use directory_processor::DirectoryProcessor; use directory_processor::DirectoryProcessor;
pub const FILES_TO_COPY: [&str; 10] = ["jpg", "jpeg", "png", "gif", "bmp", "tiff", "webp", "avif", "txt", "md"]; pub const FILES_TO_COPY: [&str; 10] = ["jpg", "jpeg", "png", "gif", "bmp", "tiff", "webp", "avif", "txt", "md"];
pub const FILES_TO_CONVERT: [&str; 3] = ["odt", "doc", "docx"];
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command( #[command(
author, author,
version, version,
about = "Convert source directory with odt files to target path with pdf files with changes verification" about = "Convert source directory with document files (odt/doc/docx) to target path with pdf files with changes verification"
)] )]
struct Args { struct Args {
#[arg(help = "Source directory with .odt files")] #[arg(help = "Source directory with .odt, .doc, or .docx files")]
source: PathBuf, source: PathBuf,
#[arg(help = "Target directory for PDFs converted files")] #[arg(help = "Target directory for PDFs converted files")]