From bdbaff020da320a8a6f2522f9f90a1da3e16bf4d Mon Sep 17 00:00:00 2001 From: brxken128 <77554505+brxken128@users.noreply.github.com> Date: Tue, 17 Jan 2023 12:40:07 +0000 Subject: [PATCH] cleanup duplicator job --- core/src/object/fs/duplicate.rs | 90 +++++++++++++++++---------------- 1 file changed, 46 insertions(+), 44 deletions(-) diff --git a/core/src/object/fs/duplicate.rs b/core/src/object/fs/duplicate.rs index dacc7f330..c5e12106d 100644 --- a/core/src/object/fs/duplicate.rs +++ b/core/src/object/fs/duplicate.rs @@ -9,6 +9,7 @@ pub struct FileDuplicatorJob {} #[derive(Serialize, Deserialize, Debug, Clone)] pub struct FileDuplicatorJobState { pub root_path: PathBuf, + pub root_prefix: PathBuf, pub root_type: ObjectType, } @@ -43,8 +44,38 @@ impl StatefulJob for FileDuplicatorJob { ) .await?; + let root_prefix = if fs_info.obj_type == ObjectType::File { + let mut output_path = fs_info.obj_path.clone(); + output_path.set_file_name( + fs_info + .obj_path + .file_stem() + .unwrap() + .to_str() + .unwrap() + .to_string() + "-Copy" + + &fs_info.obj_path.extension().map_or_else( + || String::from(""), + |x| String::from(".") + x.to_str().unwrap(), + ), + ); + output_path + } else { + let mut output_path = fs_info.obj_path.clone(); + output_path.set_file_name( + output_path + .file_stem() + .unwrap() + .to_str() + .unwrap() + .to_string() + "-Copy", + ); + output_path + }; + state.data = Some(FileDuplicatorJobState { root_path: fs_info.obj_path.clone(), + root_prefix, root_type: fs_info.obj_type.clone(), }); @@ -64,44 +95,13 @@ impl StatefulJob for FileDuplicatorJob { let step = state.steps[0].clone(); let info = &step.fs_info; - // temporary - let job_state = if let Some(st) = state.data.clone() { - st - } else { - return Err(JobError::CryptoError(sd_crypto::Error::MediaLengthParse)); - }; - - let mut root_path = if job_state.root_type == ObjectType::File { - let mut output_path = info.obj_path.clone(); - output_path.set_file_name( - info.obj_path - .file_stem() - .unwrap() - .to_str() - .unwrap() - .to_string() + "-Copy" - + &info.obj_path.extension().map_or_else( - || String::from(""), - |x| String::from(".") + x.to_str().unwrap(), - ), - ); - output_path - } else { - let mut output_path = job_state.root_path.clone(); - output_path.set_file_name( - output_path - .file_stem() - .unwrap() - .to_str() - .unwrap() - .to_string() + "-Copy", - ); - output_path - }; + let job_state = state.data.clone().ok_or(JobError::MissingData { + value: String::from("job state"), + })?; match info.obj_type { ObjectType::File => { - let mut path = root_path.clone(); + let mut path = job_state.root_prefix.clone(); if job_state.root_type == ObjectType::Directory { path.push( @@ -126,15 +126,6 @@ impl StatefulJob for FileDuplicatorJob { obj_type, }, }); - - let mut path = root_path.clone(); - path.push( - entry - .path() - .strip_prefix(job_state.root_path.clone()) - .unwrap(), - ); - std::fs::create_dir_all(path)?; } else { let obj_type = ObjectType::File; state.steps.push_back(FileDuplicatorJobStep { @@ -147,6 +138,17 @@ impl StatefulJob for FileDuplicatorJob { }); }; + let mut path_suffix = entry + .path() + .strip_prefix(job_state.root_path.clone()) + .unwrap() + .to_path_buf(); + path_suffix.set_file_name(""); + + let mut path = job_state.root_prefix.clone(); + path.push(path_suffix); + std::fs::create_dir_all(path)?; + ctx.progress(vec![JobReportUpdate::TaskCount(state.steps.len())]); } }