multi-commit: cleanup, fix file handling

feat: added cleanup
feat: added new config key (keep_directory_structure)
fix: file handling
This commit is contained in:
Didier Slof 2023-06-03 21:28:48 +02:00
parent 16395dc570
commit 6b1ee28c06
Signed by: didier
GPG key ID: 01E71F18AA4398E5
3 changed files with 38 additions and 3 deletions

View file

@ -1,6 +1,7 @@
[files] [files]
input_path = "/data/input" input_path = "/data/input"
output_path = "/data/output" output_path = "/data/output"
keep_directory_structure = true # keep directory structure when outputting files
include = [ 'mp4', 'avi', 'mkv' ] # file extensions to include include = [ 'mp4', 'avi', 'mkv' ] # file extensions to include
[files.cleanup] [files.cleanup]

View file

@ -32,6 +32,7 @@ pub struct ConfigFilesCleanup {
pub struct ConfigFiles { pub struct ConfigFiles {
pub input_path: String, pub input_path: String,
pub output_path: String, pub output_path: String,
pub keep_directory_structure: Option<bool>,
pub include: Vec<String>, pub include: Vec<String>,
pub cleanup: Option<ConfigFilesCleanup>, pub cleanup: Option<ConfigFilesCleanup>,
} }
@ -51,6 +52,7 @@ impl Config {
files: ConfigFiles { files: ConfigFiles {
input_path: String::from("/data/input"), input_path: String::from("/data/input"),
output_path: String::from("/data/output"), output_path: String::from("/data/output"),
keep_directory_structure: None,
include: Vec::new(), include: Vec::new(),
cleanup: None, cleanup: None,
}, },

View file

@ -47,9 +47,21 @@ fn start_transcode_run(input_files: &Vec<PathBuf>, config: &Config) -> i32 {
error!("Failed to process file {}: {}", job.input, remarks); 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); 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( let job = TranscodeJob::new(
file.to_str().unwrap(), file.to_str().unwrap(),
@ -87,7 +99,27 @@ fn start_transcode_run(input_files: &Vec<PathBuf>, config: &Config) -> i32 {
continue; 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); info!("Finished processing file {}.", job.input);
success_count += 1; success_count += 1;