push copy file
This commit is contained in:
parent
aced8b992a
commit
41c4796555
@ -12,6 +12,7 @@ pub mod rm_dir;
|
|||||||
pub mod deleted;
|
pub mod deleted;
|
||||||
pub mod modified;
|
pub mod modified;
|
||||||
pub mod moved;
|
pub mod moved;
|
||||||
|
pub mod copied;
|
||||||
|
|
||||||
pub fn push() {
|
pub fn push() {
|
||||||
// todo err when pushing new folder
|
// todo err when pushing new folder
|
||||||
|
83
src/commands/push/copied.rs
Normal file
83
src/commands/push/copied.rs
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
use std::path::PathBuf;
|
||||||
|
use std::io;
|
||||||
|
use crate::services::api::ApiError;
|
||||||
|
use crate::services::r#copy::Copy;
|
||||||
|
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 Copied {
|
||||||
|
pub obj: LocalObj,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PushChange for Copied {
|
||||||
|
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 = Copy::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 copying file {}: {}", obj.name, err.status());
|
||||||
|
std::process::exit(1);
|
||||||
|
},
|
||||||
|
Err(ApiError::RequestError(_)) => {
|
||||||
|
eprintln!("fatal: request error copying 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();
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
// download file with .distant at the end
|
||||||
|
fn conflict(&self) {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
}
|
@ -9,6 +9,7 @@ use crate::commands::push::rm_dir::RmDir;
|
|||||||
use crate::commands::push::deleted::Deleted;
|
use crate::commands::push::deleted::Deleted;
|
||||||
use crate::commands::push::modified::Modified;
|
use crate::commands::push::modified::Modified;
|
||||||
use crate::commands::push::moved::Moved;
|
use crate::commands::push::moved::Moved;
|
||||||
|
use crate::commands::push::copied::Copied;
|
||||||
use crate::store::object::blob::Blob;
|
use crate::store::object::blob::Blob;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@ -93,6 +94,7 @@ impl PushFactory {
|
|||||||
State::Modified => Box::new(Modified { obj }),
|
State::Modified => Box::new(Modified { obj }),
|
||||||
State::Deleted => Box::new(Deleted { obj }),
|
State::Deleted => Box::new(Deleted { obj }),
|
||||||
State::Moved => Box::new(Moved { obj }),
|
State::Moved => Box::new(Moved { obj }),
|
||||||
|
State::Copied => Box::new(Copied { obj }),
|
||||||
State::Default => todo!(),
|
State::Default => todo!(),
|
||||||
_ => todo!(),
|
_ => todo!(),
|
||||||
}
|
}
|
||||||
|
@ -5,3 +5,6 @@ pub mod req_props;
|
|||||||
pub mod upload_file;
|
pub mod upload_file;
|
||||||
pub mod delete_path;
|
pub mod delete_path;
|
||||||
pub mod downloader;
|
pub mod downloader;
|
||||||
|
pub mod r#move;
|
||||||
|
pub mod r#copy;
|
||||||
|
//pub mod bulk_upload;
|
||||||
|
61
src/services/copy.rs
Normal file
61
src/services/copy.rs
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
use reqwest::{Method, Response, Error, header::HeaderValue};
|
||||||
|
use crate::services::api::{ApiBuilder, ApiError};
|
||||||
|
use crate::clone::get_url_props;
|
||||||
|
use crate::commands::config;
|
||||||
|
|
||||||
|
pub struct Copy {
|
||||||
|
api_builder: ApiBuilder,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Copy {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Copy {
|
||||||
|
api_builder: ApiBuilder::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_url(&mut self, url: &str, destination: &str) -> &mut Copy {
|
||||||
|
self.api_builder.build_request(Method::from_bytes(b"COPY").unwrap(), url);
|
||||||
|
|
||||||
|
let remote = match config::get("remote") {
|
||||||
|
Some(r) => r,
|
||||||
|
None => {
|
||||||
|
eprintln!("fatal: unable to find a remote");
|
||||||
|
std::process::exit(1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let (host, username, root) = get_url_props(&remote);
|
||||||
|
let mut url = String::from(host);
|
||||||
|
url.push_str("/remote.php/dav/files/");
|
||||||
|
url.push_str(username.unwrap());
|
||||||
|
url.push_str(&root);
|
||||||
|
url.push_str("/");
|
||||||
|
if destination != "/" {
|
||||||
|
url.push_str(destination);
|
||||||
|
}
|
||||||
|
self.api_builder.set_header("Destination", HeaderValue::from_str(&url).unwrap());
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn send(&mut self) -> Result<Response, Error> {
|
||||||
|
self.api_builder.send().await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn overwrite(&mut self, overwrite: bool) -> &mut Copy {
|
||||||
|
self.api_builder.set_header("Overwrite", HeaderValue::from_str({
|
||||||
|
if overwrite { "T" } else { "F" }
|
||||||
|
}).unwrap());
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn send_with_err(&mut self) -> Result<(), ApiError> {
|
||||||
|
let res = tokio::runtime::Runtime::new().unwrap().block_on(async {
|
||||||
|
self.send().await
|
||||||
|
}).map_err(ApiError::RequestError)?;
|
||||||
|
if res.status().is_success() {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(ApiError::IncorrectRequest(res))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user