cleanup duplicator job

This commit is contained in:
brxken128 2023-01-17 12:40:07 +00:00
parent e86ed4e212
commit bd8b6446aa

View file

@ -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())]);
}
}