push move file

This commit is contained in:
grimhilt
2023-08-25 16:09:28 +02:00
parent d476622305
commit d323ae3070
6 changed files with 184 additions and 19 deletions

View File

@@ -11,14 +11,15 @@ pub mod new_dir;
pub mod rm_dir;
pub mod deleted;
pub mod modified;
pub mod moved;
pub fn push() {
// todo err when pushing new folder
// todo
let _remote = match config::get("remote") {
Some(r) => r,
None => {
eprintln!("fatal: no remote set in configuration");
// todo debug
//std::process::exit(1);
String::new()
}

View File

@@ -0,0 +1,86 @@
use std::path::PathBuf;
use std::io;
use crate::services::api::ApiError;
use crate::services::r#move::Move;
use crate::services::req_props::ReqProps;
use crate::commands::status::LocalObj;
use crate::commands::push::push_factory::{PushState, PushChange, PushFlowState};
use crate::store::object::blob::Blob;
use crate::utils::path::path_buf_to_string;
pub struct Moved {
pub obj: LocalObj,
}
impl PushChange for Moved {
fn can_push(&self, whitelist: &mut Option<PathBuf>) -> PushState {
match self.flow(&self.obj, whitelist.clone()) {
PushFlowState::Whitelisted => PushState::Done,
PushFlowState::NotOnRemote => PushState::Valid,
PushFlowState::RemoteIsNewer => PushState::Conflict,
PushFlowState::LocalIsNewer => PushState::Conflict,
PushFlowState::Error => PushState::Error,
}
}
fn push(&self) -> io::Result<()> {
let obj = &self.obj;
let res = Move::new()
.set_url(
&path_buf_to_string(obj.path_from.clone().unwrap()),
obj.path.to_str().unwrap())
.send_with_err();
match res {
Err(ApiError::IncorrectRequest(err)) => {
eprintln!("fatal: error moving file {}: {}", obj.name, err.status());
std::process::exit(1);
},
Err(ApiError::RequestError(_)) => {
eprintln!("fatal: request error moving file {}", obj.name);
std::process::exit(1);
}
_ => (),
}
// get lastmodified props to update it
let props = ReqProps::new()
.set_url(obj.path.to_str().unwrap())
.getlastmodified()
.send_req_single();
let prop = match props {
Ok(o) => o,
Err(ApiError::IncorrectRequest(err)) => {
eprintln!("fatal: {}", err.status());
std::process::exit(1);
},
Err(ApiError::EmptyError(_)) => {
eprintln!("Failed to get body");
std::process::exit(1);
}
Err(ApiError::RequestError(err)) => {
eprintln!("fatal: {}", err);
std::process::exit(1);
},
Err(ApiError::Unexpected(_)) => todo!()
};
let lastmodified = prop.lastmodified.unwrap().timestamp_millis();
// delete source and create destination blob
if let Err(err) = Blob::new(obj.path.clone()).create(&lastmodified.to_string(), false) {
eprintln!("err: creating ref of {}: {}", obj.name.clone(), err);
}
if let Err(err) = Blob::new(obj.path_from.clone().unwrap()).rm() {
eprintln!("err: removing ref of {}: {}", obj.name.clone(), err);
}
Ok(())
}
// download file with .distant at the end
fn conflict(&self) {
todo!()
}
}

View File

@@ -67,7 +67,7 @@ impl PushChange for New {
let lastmodified = prop.lastmodified.unwrap().timestamp_millis();
// create new blob
Blob::new(obj.path.clone()).create(&lastmodified.to_string(), true)?;
Blob::new(obj.path.clone()).create(&lastmodified.to_string(), false)?;
Ok(())
}

View File

@@ -2,13 +2,14 @@ use std::path::PathBuf;
use std::io;
use crate::commands::status::{State, LocalObj};
use crate::services::api::ApiError;
use crate::store::object;
use crate::services::req_props::ReqProps;
use crate::commands::push::new::New;
use crate::commands::push::new_dir::NewDir;
use crate::commands::push::rm_dir::RmDir;
use crate::commands::push::deleted::Deleted;
use crate::commands::push::modified::Modified;
use crate::commands::push::moved::Moved;
use crate::store::object::blob::Blob;
#[derive(Debug)]
pub enum PushState {
@@ -39,6 +40,7 @@ pub trait PushChange {
}
fn flow(&self, obj: &LocalObj, whitelist: Option<PathBuf>) -> PushFlowState {
// todo moved: from same file, destination doesn't exist but parent do
if self.is_whitelisted(obj, whitelist) {
return PushFlowState::Whitelisted;
}
@@ -69,7 +71,9 @@ pub trait PushChange {
};
// check if remote is newest
let last_sync_ts = object::get_timestamp(obj.path.to_str().unwrap().to_string()).unwrap();
let last_sync_ts = Blob::new(obj.path.clone())
.saved_remote_ts()
.parse::<i64>().unwrap();
let remote_ts = obj_data.lastmodified.unwrap().timestamp_millis();
if last_sync_ts < remote_ts {
@@ -88,6 +92,7 @@ impl PushFactory {
State::New => Box::new(New { obj }),
State::Modified => Box::new(Modified { obj }),
State::Deleted => Box::new(Deleted { obj }),
State::Moved => Box::new(Moved { obj }),
State::Default => todo!(),
_ => todo!(),
}