mirror of
https://github.com/spacedriveapp/spacedrive
synced 2024-07-18 15:29:11 +00:00
working dir duplication
This commit is contained in:
parent
f81c1b35a8
commit
e86ed4e212
|
@ -2,12 +2,15 @@ use super::{context_menu_fs_info, FsInfo, ObjectType};
|
||||||
use crate::job::{JobError, JobReportUpdate, JobResult, JobState, StatefulJob, WorkerContext};
|
use crate::job::{JobError, JobReportUpdate, JobResult, JobState, StatefulJob, WorkerContext};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use specta::Type;
|
use specta::Type;
|
||||||
use std::{collections::VecDeque, hash::Hash};
|
use std::{collections::VecDeque, hash::Hash, path::PathBuf};
|
||||||
|
|
||||||
pub struct FileDuplicatorJob {}
|
pub struct FileDuplicatorJob {}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
pub struct FileDuplicatorJobState {}
|
pub struct FileDuplicatorJobState {
|
||||||
|
pub root_path: PathBuf,
|
||||||
|
pub root_type: ObjectType,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Hash, Type)]
|
#[derive(Serialize, Deserialize, Hash, Type)]
|
||||||
pub struct FileDuplicatorJobInit {
|
pub struct FileDuplicatorJobInit {
|
||||||
|
@ -15,7 +18,7 @@ pub struct FileDuplicatorJobInit {
|
||||||
pub path_id: i32,
|
pub path_id: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
pub struct FileDuplicatorJobStep {
|
pub struct FileDuplicatorJobStep {
|
||||||
pub fs_info: FsInfo,
|
pub fs_info: FsInfo,
|
||||||
}
|
}
|
||||||
|
@ -40,6 +43,11 @@ impl StatefulJob for FileDuplicatorJob {
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
state.data = Some(FileDuplicatorJobState {
|
||||||
|
root_path: fs_info.obj_path.clone(),
|
||||||
|
root_type: fs_info.obj_type.clone(),
|
||||||
|
});
|
||||||
|
|
||||||
state.steps = VecDeque::new();
|
state.steps = VecDeque::new();
|
||||||
state.steps.push_back(FileDuplicatorJobStep { fs_info });
|
state.steps.push_back(FileDuplicatorJobStep { fs_info });
|
||||||
|
|
||||||
|
@ -53,29 +61,96 @@ impl StatefulJob for FileDuplicatorJob {
|
||||||
ctx: WorkerContext,
|
ctx: WorkerContext,
|
||||||
state: &mut JobState<Self>,
|
state: &mut JobState<Self>,
|
||||||
) -> Result<(), JobError> {
|
) -> Result<(), JobError> {
|
||||||
let step = &state.steps[0];
|
let step = state.steps[0].clone();
|
||||||
let info = &step.fs_info;
|
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
|
||||||
|
};
|
||||||
|
|
||||||
match info.obj_type {
|
match info.obj_type {
|
||||||
ObjectType::File => {
|
ObjectType::File => {
|
||||||
let mut output_path = info.obj_path.clone();
|
let mut path = root_path.clone();
|
||||||
output_path.set_file_name(
|
|
||||||
info.obj_path
|
if job_state.root_type == ObjectType::Directory {
|
||||||
.clone()
|
path.push(
|
||||||
.file_stem()
|
info.obj_path
|
||||||
.unwrap()
|
.strip_prefix(job_state.root_path.clone())
|
||||||
.to_str()
|
.unwrap(),
|
||||||
.unwrap()
|
);
|
||||||
.to_string() + "-Copy" + "."
|
}
|
||||||
+ info
|
|
||||||
.obj_path
|
std::fs::copy(info.obj_path.clone(), path.clone())?;
|
||||||
.extension()
|
|
||||||
.map_or_else(|| "", |x| x.to_str().unwrap()),
|
|
||||||
);
|
|
||||||
std::fs::copy(info.obj_path.clone(), output_path)
|
|
||||||
}
|
}
|
||||||
ObjectType::Directory => todo!(),
|
ObjectType::Directory => {
|
||||||
}?;
|
for entry in std::fs::read_dir(info.obj_path.clone())? {
|
||||||
|
let entry = entry?;
|
||||||
|
if entry.metadata()?.is_dir() {
|
||||||
|
let obj_type = ObjectType::Directory;
|
||||||
|
state.steps.push_back(FileDuplicatorJobStep {
|
||||||
|
fs_info: FsInfo {
|
||||||
|
obj_id: None,
|
||||||
|
obj_name: String::new(),
|
||||||
|
obj_path: entry.path(),
|
||||||
|
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 {
|
||||||
|
fs_info: FsInfo {
|
||||||
|
obj_id: None,
|
||||||
|
obj_name: entry.file_name().to_str().unwrap().to_string(),
|
||||||
|
obj_path: entry.path(),
|
||||||
|
obj_type,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
ctx.progress(vec![JobReportUpdate::TaskCount(state.steps.len())]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
ctx.progress(vec![JobReportUpdate::CompletedTaskCount(
|
ctx.progress(vec![JobReportUpdate::CompletedTaskCount(
|
||||||
state.step_number + 1,
|
state.step_number + 1,
|
||||||
|
|
Loading…
Reference in a new issue