From 6b1ee28c06df155712dfb902bf1a352ac21dc7cc Mon Sep 17 00:00:00 2001 From: Didier Date: Sat, 3 Jun 2023 21:28:48 +0200 Subject: [PATCH] multi-commit: cleanup, fix file handling feat: added cleanup feat: added new config key (keep_directory_structure) fix: file handling --- config.example.toml | 1 + src/configuration/mod.rs | 2 ++ src/main.rs | 38 +++++++++++++++++++++++++++++++++++--- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/config.example.toml b/config.example.toml index 4a7578e..50806ef 100644 --- a/config.example.toml +++ b/config.example.toml @@ -1,6 +1,7 @@ [files] input_path = "/data/input" output_path = "/data/output" +keep_directory_structure = true # keep directory structure when outputting files include = [ 'mp4', 'avi', 'mkv' ] # file extensions to include [files.cleanup] diff --git a/src/configuration/mod.rs b/src/configuration/mod.rs index 04a2131..e7eab65 100644 --- a/src/configuration/mod.rs +++ b/src/configuration/mod.rs @@ -32,6 +32,7 @@ pub struct ConfigFilesCleanup { pub struct ConfigFiles { pub input_path: String, pub output_path: String, + pub keep_directory_structure: Option, pub include: Vec, pub cleanup: Option, } @@ -51,6 +52,7 @@ impl Config { files: ConfigFiles { input_path: String::from("/data/input"), output_path: String::from("/data/output"), + keep_directory_structure: None, include: Vec::new(), cleanup: None, }, diff --git a/src/main.rs b/src/main.rs index 05dd769..eeff7cd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -47,9 +47,21 @@ fn start_transcode_run(input_files: &Vec, config: &Config) -> i32 { error!("Failed to process file {}: {}", job.input, remarks); }; - let mut output_path = file.clone(); + let mut output_path = if let Some(keep_directory_structure) = config.files.keep_directory_structure { + if keep_directory_structure { + let mut output_path = PathBuf::from(&config.files.output_path); + output_path.push(file.strip_prefix(&config.files.input_path).unwrap()); + output_path + } else { + PathBuf::from(&config.files.output_path) + .join(file.file_name().unwrap()) + } + } else { + PathBuf::from(&config.files.output_path) + .join(file.file_name().unwrap()) + }; output_path.set_extension(&config.ffmpeg.output.format); - let output_path = PathBuf::from(&config.files.output_path).join(output_path.file_name().unwrap()); // TODO: This is a bit of a mess. + let job = TranscodeJob::new( file.to_str().unwrap(), @@ -87,7 +99,27 @@ fn start_transcode_run(input_files: &Vec, config: &Config) -> i32 { continue; } - // TODO: Cleanup + if let Some(cleanup) = &config.files.cleanup { + if !cleanup.enabled { () } + match cleanup.original_cleanup_behavior { + configuration::ConfigFilesCleanupOriginalBehavior::delete => { + if let Err(e) = std::fs::remove_file(&job.input) { + error!("Failed to delete original file {}: {}", job.input, e); + } + }, + configuration::ConfigFilesCleanupOriginalBehavior::archive => { + let mut archive_path = PathBuf::from(&cleanup.archive.path); + archive_path.push(job.input.strip_prefix(&config.files.input_path).unwrap()); + if let Err(e) = std::fs::create_dir_all(archive_path.parent().unwrap()) { + error!("Failed to create archive directory {}: {}", archive_path.parent().unwrap().to_str().unwrap(), e); + } + if let Err(e) = std::fs::rename(&job.input, &archive_path) { + error!("Failed to archive original file {}: {}", job.input, e); + } + }, + configuration::ConfigFilesCleanupOriginalBehavior::keep => (), + } + } info!("Finished processing file {}.", job.input); success_count += 1;