diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..adf4aa5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +.svn +.vscode + +*.app +*.dmg +.DS_Store + +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..122dc7e --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,316 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "anstream" +version = "0.6.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8acc5369981196006228e28809f761875c0327210a891e941f4c683b3a99529b" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9" + +[[package]] +name = "anstyle-parse" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b2d16507662817a6a20a9ea92df6652ee4f94f914589377d69f3b21bc5798a9" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79947af37f4177cfead1110013d678905c37501914fba0efea834c3fe9a8d60c" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3534e77181a9cc07539ad51f2141fe32f6c3ffd4df76db8ad92346b003ae4e" +dependencies = [ + "anstyle", + "once_cell", + "windows-sys", +] + +[[package]] +name = "bitflags" +version = "2.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967" + +[[package]] +name = "clap" +version = "4.5.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed93b9805f8ba930df42c2590f05453d5ec36cbb85d018868a5b24d31f6ac000" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "379026ff283facf611b0ea629334361c4211d1b12ee01024eec1591133b04120" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09176aae279615badda0765c0c0b3f6ed53f4709118af73cf4655d85d1530cd7" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6" + +[[package]] +name = "colorchoice" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" + +[[package]] +name = "dir-odt-to-pdf" +version = "0.1.0" +dependencies = [ + "clap", + "which", +] + +[[package]] +name = "either" +version = "1.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" + +[[package]] +name = "env_home" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7f84e12ccf0a7ddc17a6c41c93326024c42920d7ee630d04950e6926645c0fe" + +[[package]] +name = "errno" +version = "0.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cea14ef9355e3beab063703aa9dab15afd25f0667c341310c1e5274bb1d0da18" +dependencies = [ + "libc", + "windows-sys", +] + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" + +[[package]] +name = "libc" +version = "0.2.172" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d750af042f7ef4f724306de029d18836c26c1765a54a6a3f094cbd23a7267ffa" + +[[package]] +name = "linux-raw-sys" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd945864f07fe9f5371a27ad7b52a172b4b499999f1d97574c9fa68373937e12" + +[[package]] +name = "once_cell" +version = "1.21.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" + +[[package]] +name = "proc-macro2" +version = "1.0.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rustix" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c71e83d6afe7ff64890ec6b71d6a69bb8a610ab78ce364b3352876bb4c801266" +dependencies = [ + "bitflags", + "errno", + "libc", + "linux-raw-sys", + "windows-sys", +] + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "syn" +version = "2.0.101" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ce2b7fc941b3a24138a0a7cf8e858bfc6a992e7978a068a5c760deb0ed43caf" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[package]] +name = "which" +version = "7.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d643ce3fd3e5b54854602a080f34fb10ab75e0b813ee32d00ca2b44fa74762" +dependencies = [ + "either", + "env_home", + "rustix", + "winsafe", +] + +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "winsafe" +version = "0.0.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d135d17ab770252ad95e9a872d365cf3090e3be864a34ab46f48555993efc904" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..b65b45d --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "dir-odt-to-pdf" +version = "0.1.0" +description = "Convert source directory with odt files to target path with pdf files" +authors = ["Jesús Pérez "] +edition = "2024" + +[dependencies] +clap = { version = "4.5.38", features = ["derive"] } +which = "7.0.3" diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..596bb00 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,151 @@ +use clap::Parser; +use std::fs; +use std::path::{Path, PathBuf}; +use std::process::Command; +use std::time::UNIX_EPOCH; +use std::env; +// use std::time::{SystemTime, UNIX_EPOCH}; + +#[derive(Parser, Debug)] +#[command( + author, + version, + about = "Convert source directory with odt files to target path with pdf files with changes verification" +)] +struct Args { + #[arg(help = "Source directory with .odt files")] + source: PathBuf, + + #[arg(help = "Target directory for PDFs converted files")] + dest: PathBuf, +} + +fn needs_conversion(odt_path: &Path, pdf_path: &Path) -> bool { + if !pdf_path.exists() { + return true; + } + + let odt_modified = fs::metadata(odt_path) + .and_then(|m| m.modified()) + .unwrap_or(UNIX_EPOCH); + + let pdf_modified = fs::metadata(pdf_path) + .and_then(|m| m.modified()) + .unwrap_or(UNIX_EPOCH); + + odt_modified > pdf_modified +} + +fn get_soffice_path() -> Result { + // First try environment variable if set + if let Ok(path) = env::var("LIBREOFFICE_PATH") { + if Path::new(&path).exists() { + return Ok(path); + } + } + + // Check common installation paths + let paths = if cfg!(target_os = "macos") { + vec![ + "/Applications/LibreOffice.app/Contents/MacOS/soffice", + "/Applications/LibreOffice-still.app/Contents/MacOS/soffice", + "/Applications/LibreOffice-fresh.app/Contents/MacOS/soffice", + ] + } else { + vec![ + "/usr/bin/soffice", + "/usr/local/bin/soffice", + "/usr/lib/libreoffice/program/soffice", + "soffice", // Try PATH + ] + }; + + // Try each path + for path in paths { + if Path::new(path).exists() || which::which(path).is_ok() { + return Ok(path.to_string()); + } + } + + Err("LibreOffice not found. Please install LibreOffice or set LIBREOFFICE_PATH environment variable.".to_string()) +} + +fn convert_file(odt_path: &Path, dest_dir: &Path) -> Result<(), String> { + let soffice_path = get_soffice_path()?; + + // Verify input file exists and is readable + if !odt_path.exists() { + return Err(format!("Source file does not exist: {}", odt_path.display())); + } + + // Verify destination directory exists and is writable + if !dest_dir.exists() { + return Err(format!("Destination directory does not exist: {}", dest_dir.display())); + } + + // Try to get write permissions on destination directory + let metadata = fs::metadata(dest_dir) + .map_err(|e| format!("Failed to get destination directory metadata: {}", e))?; + + #[cfg(unix)] + use std::os::unix::fs::PermissionsExt; + #[cfg(unix)] + if metadata.permissions().mode() & 0o200 == 0 { + return Err(format!("Destination directory is not writable: {}", dest_dir.display())); + } + + let output = Command::new(&soffice_path) + .args(&[ + "--headless", + "--convert-to", + "pdf", + "--outdir", + dest_dir.to_str().ok_or("Invalid destination path")?, + odt_path.to_str().ok_or("Invalid source path")?, + ]) + .output() + .map_err(|e| format!("Failed to execute LibreOffice command: {}", e))?; + + if !output.status.success() { + let error_msg = String::from_utf8_lossy(&output.stderr); + let stdout_msg = String::from_utf8_lossy(&output.stdout); + return Err(format!( + "Conversion failed:\nError: {}\nOutput: {}", + error_msg, + stdout_msg + )); + } + + 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; + } + + // 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), + } + } + } + } + } +}