working single file copy

This commit is contained in:
brxken128 2023-01-17 16:57:44 +00:00
parent efc7843c24
commit 8b9f05f168
2 changed files with 82 additions and 103 deletions

View file

@ -1,4 +1,4 @@
use super::{context_menu_fs_info, FsInfo, ObjectType}; use super::{context_menu_fs_info, get_path_from_location_id, 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;
@ -8,8 +8,8 @@ pub struct FileCopierJob {}
#[derive(Serialize, Deserialize, Debug, Clone)] #[derive(Serialize, Deserialize, Debug, Clone)]
pub struct FileCopierJobState { pub struct FileCopierJobState {
pub root_path: PathBuf, pub target_path: PathBuf, // target dir prefix too
pub root_prefix: PathBuf, pub source_path: PathBuf,
pub root_type: ObjectType, pub root_type: ObjectType,
} }
@ -23,7 +23,7 @@ pub struct FileCopierJobInit {
#[derive(Serialize, Deserialize, Debug, Clone)] #[derive(Serialize, Deserialize, Debug, Clone)]
pub struct FileCopierJobStep { pub struct FileCopierJobStep {
pub fs_info: FsInfo, pub source_fs_info: FsInfo,
} }
const JOB_NAME: &str = "file_copier"; const JOB_NAME: &str = "file_copier";
@ -39,52 +39,27 @@ impl StatefulJob for FileCopierJob {
} }
async fn init(&self, ctx: WorkerContext, state: &mut JobState<Self>) -> Result<(), JobError> { async fn init(&self, ctx: WorkerContext, state: &mut JobState<Self>) -> Result<(), JobError> {
// let fs_info = context_menu_fs_info( let source_fs_info = context_menu_fs_info(
// &ctx.library_ctx.db, &ctx.library_ctx.db,
// state.init.location_id, state.init.source_location_id,
// state.init.path_id, state.init.source_path_id,
// ) )
// .await?; .await?;
// let root_prefix = if fs_info.obj_type == ObjectType::File { let mut full_target_path =
// let mut output_path = fs_info.obj_path.clone(); get_path_from_location_id(&ctx.library_ctx.db, state.init.target_location_id).await?;
// output_path.set_file_name( full_target_path.push(state.init.target_path.clone());
// 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(FileCopierJobState { state.data = Some(FileCopierJobState {
// root_path: fs_info.obj_path.clone(), target_path: full_target_path,
// root_prefix, source_path: source_fs_info.obj_path.clone(),
// root_type: fs_info.obj_type.clone(), root_type: source_fs_info.obj_type.clone(),
// }); });
// state.steps = VecDeque::new(); state.steps = VecDeque::new();
// state.steps.push_back(FileCopierJobStep { fs_info }); state.steps.push_back(FileCopierJobStep { source_fs_info });
// ctx.progress(vec![JobReportUpdate::TaskCount(state.steps.len())]); ctx.progress(vec![JobReportUpdate::TaskCount(state.steps.len())]);
Ok(()) Ok(())
} }
@ -94,70 +69,75 @@ impl StatefulJob for FileCopierJob {
ctx: WorkerContext, ctx: WorkerContext,
state: &mut JobState<Self>, state: &mut JobState<Self>,
) -> Result<(), JobError> { ) -> Result<(), JobError> {
// let step = state.steps[0].clone(); let step = state.steps[0].clone();
// let info = &step.fs_info; let info = &step.source_fs_info;
// let job_state = state.data.clone().ok_or(JobError::MissingData { let job_state = state.data.clone().ok_or(JobError::MissingData {
// value: String::from("job state"), value: String::from("job state"),
// })?; })?;
// match info.obj_type { match info.obj_type {
// ObjectType::File => { ObjectType::File => {
// let mut path = job_state.root_prefix.clone(); let mut path = job_state.target_path.clone();
// if job_state.root_type == ObjectType::Directory { if job_state.root_type == ObjectType::Directory {
// path.push( path.push(
// info.obj_path info.obj_path
// .strip_prefix(job_state.root_path.clone()) .strip_prefix(job_state.source_path.clone())
// .unwrap(), .unwrap(),
// ); );
// } } else {
path.push(info.obj_path.clone().file_name().unwrap());
}
// std::fs::copy(info.obj_path.clone(), path.clone())?; dbg!(info.obj_path.clone());
// } dbg!(path.clone());
// 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(FileCopierJobStep {
// fs_info: FsInfo {
// obj_id: None,
// obj_name: String::new(),
// obj_path: entry.path(),
// obj_type,
// },
// });
// let path_suffix = entry std::fs::copy(info.obj_path.clone(), path.clone())?;
// .path() }
// .strip_prefix(job_state.root_path.clone()) _ => todo!(), // ObjectType::Directory => {
// .unwrap() // for entry in std::fs::read_dir(info.obj_path.clone())? {
// .to_path_buf(); // let entry = entry?;
// if entry.metadata()?.is_dir() {
// let obj_type = ObjectType::Directory;
// state.steps.push_back(FileCopierJobStep {
// fs_info: FsInfo {
// obj_id: None,
// obj_name: String::new(),
// obj_path: entry.path(),
// obj_type,
// },
// });
// let mut path = job_state.root_prefix.clone(); // let path_suffix = entry
// path.push(path_suffix); // .path()
// std::fs::create_dir_all(path)?; // .strip_prefix(job_state.root_path.clone())
// } else { // .unwrap()
// let obj_type = ObjectType::File; // .to_path_buf();
// state.steps.push_back(FileCopierJobStep {
// 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())]); // let mut path = job_state.root_prefix.clone();
// } // path.push(path_suffix);
// } // std::fs::create_dir_all(path)?;
// }; // } else {
// let obj_type = ObjectType::File;
// state.steps.push_back(FileCopierJobStep {
// 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::CompletedTaskCount( // ctx.progress(vec![JobReportUpdate::TaskCount(state.steps.len())]);
// state.step_number + 1, // }
// )]); // }
};
ctx.progress(vec![JobReportUpdate::CompletedTaskCount(
state.step_number + 1,
)]);
Ok(()) Ok(())
} }

View file

@ -67,7 +67,6 @@ impl StatefulJob for FileCutterJob {
let source_info = &step.source_fs_info; let source_info = &step.source_fs_info;
let mut full_output = step.target_directory.clone(); let mut full_output = step.target_directory.clone();
full_output.push(source_info.obj_path.clone().file_name().unwrap()); full_output.push(source_info.obj_path.clone().file_name().unwrap());
dbg!(source_info.obj_path.clone()); dbg!(source_info.obj_path.clone());