Compare commits

..

No commits in common. "418ca68745a2d5fc3615be92b3982f72b98da60d" and "80ef41970f039844313fb36f9ea9f25b6416d94b" have entirely different histories.

7 changed files with 71 additions and 269 deletions

View File

@ -2,12 +2,12 @@
## Blob object ## Blob object
``` ```
file_name timestamp size hash file_name hash timestamp
``` ```
## Tree object ## Tree object
``` ```
folder_name timestamp folder_name
tree hash_path folder_name tree hash_path folder_name
blob hash_path file_name blob hash_path file_name
``` ```

View File

@ -1,11 +1,7 @@
use crate::commands::{status, config}; use crate::commands::{status, config};
use crate::services::req_props::ReqProps;
use crate::services::api::ApiError;
use crate::services::upload_file::UploadFile;
use crate::commands::status::{State, Obj};
pub fn push() { pub fn push() {
dbg!(status::get_all_staged()); dbg!(status::get_diff());
let remote = match config::get("remote") { let remote = match config::get("remote") {
Some(r) => r, Some(r) => r,
@ -14,149 +10,9 @@ pub fn push() {
std::process::exit(1); std::process::exit(1);
} }
}; };
let (staged_obj, new_obj, del_obj) = status::get_diff();
let staged_objs = status::get_all_staged();
// todo sort folder first
for obj in staged_objs {
if obj.otype == String::from("tree") {
dbg!("should push folder");
} else {
let push_factory = PushFactory.new(obj.clone());
match push_factory.can_push() {
PushState::Valid => push_factory.push(),
_ => todo!(),
}
}
}
// read index // read index
// if dir upload dir // if dir upload dir
} }
#[derive(Debug)]
enum PushState {
Valid,
Conflict,
Error,
}
trait PushChange {
fn can_push(&self) -> PushState;
fn push(&self);
}
struct New {
obj: Obj,
}
impl PushChange for New {
fn can_push(&self) -> PushState {
// check if exist on server
let file_infos = tokio::runtime::Runtime::new().unwrap().block_on(async {
let res = ReqProps::new()
.set_url(&self.obj.path.to_str().unwrap())
.getlastmodified()
.send_with_err()
.await;
match res {
Ok(data) => Ok(data),
Err(ApiError::IncorrectRequest(err)) => {
if err.status() == 404 {
Ok(vec![])
} else {
Err(())
}
},
Err(_) => Err(()),
}
});
if let Ok(infos) = file_infos {
if infos.len() == 0 {
// file doesn't exist on remote
PushState::Valid
} else {
// check date
PushState::Conflict
}
} else {
PushState::Error
}
}
fn push(&self) {
let obj = &self.obj;
tokio::runtime::Runtime::new().unwrap().block_on(async {
let res = UploadFile::new()
.set_url(obj.path.to_str().unwrap())
.set_file(obj.path.clone())
.send_with_err()
.await;
match res {
Err(ApiError::IncorrectRequest(err)) => {
eprintln!("fatal: error pushing file {}: {}", obj.name, err.status());
std::process::exit(1);
},
Err(ApiError::RequestError(_)) => {
eprintln!("fatal: request error pushing file {}", obj.name);
std::process::exit(1);
}
_ => (),
}
// todo manage err
// todo remove index
});
}
}
struct PushFactory;
impl PushFactory {
fn new(&self, obj: Obj) -> Box<dyn PushChange> {
match obj.state {
State::New => Box::new(New { obj: obj.clone() }),
State::Renamed => todo!(),
State::Modified => todo!(),
State::Deleted => todo!(),
State::Default => todo!(),
}
}
}
fn can_push_file(obj: Obj) -> PushState {
dbg!(obj.clone());
// check if exist on server
let file_infos = tokio::runtime::Runtime::new().unwrap().block_on(async {
let res = ReqProps::new()
.set_url(obj.path.to_str().unwrap())
.getlastmodified()
.send_with_err()
.await;
match res {
Ok(data) => Ok(data),
Err(ApiError::IncorrectRequest(err)) => {
if err.status() == 404 {
Ok(vec![])
} else {
Err(())
}
},
Err(_) => Err(()),
}
});
if let Ok(infos) = file_infos {
if infos.len() == 0 {
// file doesn't exist on remote
PushState::Valid
} else {
// check date
PushState::Conflict
}
} else {
PushState::Error
}
}

View File

@ -18,7 +18,7 @@ enum RemoveSide {
#[derive(PartialEq)] #[derive(PartialEq)]
#[derive(Debug)] #[derive(Debug)]
#[derive(Clone)] #[derive(Clone)]
pub enum State { enum State {
Default, Default,
New, New,
Renamed, Renamed,
@ -42,23 +42,10 @@ pub fn status() {
#[derive(Debug)] #[derive(Debug)]
#[derive(Clone)] #[derive(Clone)]
pub struct Obj { pub struct Obj {
pub otype: String, otype: String,
pub name: String, name: String,
pub path: PathBuf, path: PathBuf,
pub state: State, state: State,
}
pub fn get_all_staged() -> Vec<Obj> {
// todo opti getting staged and then finding differences ?
// todo opti return folder
let (mut new_objs, mut del_objs) = get_diff();
let mut renamed_objs = get_renamed(&mut new_objs, &mut del_objs);
// get copy, modified
let mut objs = new_objs;
objs.append(&mut del_objs);
objs.append(&mut renamed_objs);
let staged_objs = get_staged(&mut objs);
staged_objs
} }
fn get_renamed(new_obj: &mut Vec<Obj>, del_obj: &mut Vec<Obj>) -> Vec<Obj> { fn get_renamed(new_obj: &mut Vec<Obj>, del_obj: &mut Vec<Obj>) -> Vec<Obj> {
@ -75,6 +62,7 @@ fn get_staged(objs: &mut Vec<Obj>) -> Vec<Obj> {
let nextsync_path = utils::path::nextsync().unwrap(); let nextsync_path = utils::path::nextsync().unwrap();
if let Ok(entries) = store::index::read_line(nextsync_path.clone()) { if let Ok(entries) = store::index::read_line(nextsync_path.clone()) {
for entry in entries { for entry in entries {
// todo hash this
indexes.insert(entry.unwrap()); indexes.insert(entry.unwrap());
} }
} }
@ -100,7 +88,7 @@ fn get_staged(objs: &mut Vec<Obj>) -> Vec<Obj> {
staged_objs staged_objs
} }
fn get_diff() -> (Vec<Obj>, Vec<Obj>) { pub fn get_diff() -> (Vec<Obj>, Vec<Obj>) {
let mut hashes = HashMap::new(); let mut hashes = HashMap::new();
let mut objs: Vec<String> = vec![]; let mut objs: Vec<String> = vec![];
@ -131,7 +119,7 @@ fn get_diff() -> (Vec<Obj>, Vec<Obj>) {
while obj_to_analyse.len() > 0 { while obj_to_analyse.len() > 0 {
let cur_obj = obj_to_analyse.pop().unwrap(); let cur_obj = obj_to_analyse.pop().unwrap();
let cur_path = PathBuf::from(&cur_obj); let cur_path = PathBuf::from(&cur_obj);
dbg!(cur_path.clone());
let obj_path = root.clone().join(cur_path.clone()); let obj_path = root.clone().join(cur_path.clone());
if obj_path.is_dir() { if obj_path.is_dir() {
@ -161,26 +149,17 @@ fn get_diff() -> (Vec<Obj>, Vec<Obj>) {
}).collect(); }).collect();
let new_objs: Vec<Obj> = objs.iter().map(|x| { let new_objs: Vec<Obj> = objs.iter().map(|x| {
let p = PathBuf::from(x.to_string()); // todo otype and name
// todo name
Obj { Obj {
otype: get_type(p.clone()), otype: String::from(""),
name: x.to_string(), name: x.to_string(),
path: p, path: PathBuf::from(x.to_string()),
state: State::New state: State::New
} }
}).collect(); }).collect();
(new_objs, del_objs) (new_objs, del_objs)
} }
fn get_type(p: PathBuf) -> String {
if p.is_dir() {
String::from("tree")
} else {
String::from("blob")
}
}
fn add_to_hashmap(lines: Lines<BufReader<File>>, hashes: &mut HashMap<String, Obj>, path: PathBuf) { fn add_to_hashmap(lines: Lines<BufReader<File>>, hashes: &mut HashMap<String, Obj>, path: PathBuf) {
for line in lines { for line in lines {
if let Ok(ip) = line { if let Ok(ip) = line {

View File

@ -2,4 +2,3 @@ pub mod api;
pub mod list_folders; pub mod list_folders;
pub mod download_files; pub mod download_files;
pub mod req_props; pub mod req_props;
pub mod upload_file;

View File

@ -4,7 +4,6 @@ use reqwest::{Response, Error, IntoUrl, Method};
use std::env; use std::env;
use dotenv::dotenv; use dotenv::dotenv;
#[derive(Debug)]
pub enum ApiError { pub enum ApiError {
IncorrectRequest(reqwest::Response), IncorrectRequest(reqwest::Response),
EmptyError(reqwest::Error), EmptyError(reqwest::Error),
@ -29,24 +28,6 @@ impl ApiBuilder {
self self
} }
pub fn build_request(&mut self, method: Method, path: &str) -> &mut ApiBuilder {
dotenv().ok();
// todo remove env
let host = env::var("HOST").unwrap();
let username = env::var("USERNAME").unwrap();
let root = env::var("ROOT").unwrap();
let mut url = String::from(host);
url.push_str("/remote.php/dav/files/");
url.push_str(&username);
url.push_str("/");
url.push_str(&root);
url.push_str("/");
url.push_str(path);
dbg!(url.clone());
self.request = Some(self.client.request(method, url));
self
}
fn set_auth(&mut self) -> &mut ApiBuilder { fn set_auth(&mut self) -> &mut ApiBuilder {
// todo if not exist // todo if not exist
dotenv().ok(); dotenv().ok();
@ -77,20 +58,6 @@ impl ApiBuilder {
self self
} }
pub fn set_body(&mut self, body: Vec<u8>) -> &mut ApiBuilder {
match self.request.take() {
None => {
eprintln!("fatal: incorrect request");
std::process::exit(1);
},
Some(req) => {
self.request = Some(req.body(body));
}
}
self
}
pub async fn send(&mut self) -> Result<Response, Error> { pub async fn send(&mut self) -> Result<Response, Error> {
self.set_auth(); self.set_auth();
match self.request.take() { match self.request.take() {

View File

@ -10,19 +10,15 @@ pub struct ReqProps {
} }
impl ReqProps { impl ReqProps {
pub fn new() -> Self { pub fn new<U: IntoUrl>(url: U) -> Self {
ReqProps { ReqProps {
api_builder: ApiBuilder::new(), api_builder: ApiBuilder::new()
.set_request(Method::from_bytes(b"PROPFIND").unwrap(), url),
xml_list: vec![], xml_list: vec![],
xml_payload: String::new(), xml_payload: String::new(),
} }
} }
pub fn set_url(&mut self, url: &str) -> &mut ReqProps {
self.api_builder.build_request(Method::from_bytes(b"PROPFIND").unwrap(), url);
self
}
pub fn getlastmodified(&mut self) -> &mut ReqProps { pub fn getlastmodified(&mut self) -> &mut ReqProps {
self.xml_list.push(String::from("getlastmodified")); self.xml_list.push(String::from("getlastmodified"));
self.xml_payload.push_str(r#"<d:getlastmodified/>"#); self.xml_payload.push_str(r#"<d:getlastmodified/>"#);
@ -72,16 +68,34 @@ impl ReqProps {
self.api_builder.send().await self.api_builder.send().await
} }
pub async fn send_with_err(&mut self) -> Result<Vec<String>, ApiError> { pub async fn send_with_err(&mut self) -> Result<String, ApiError> {
let res = self.send().await.map_err(ApiError::RequestError)?; let res = self.send().await.map_err(ApiError::RequestError)?;
if res.status().is_success() { if res.status().is_success() {
let body = res.text().await.map_err(ApiError::EmptyError)?; let body = res.text().await.map_err(ApiError::EmptyError)?;
Ok(self.parse(body)) Ok(body)
} else { } else {
Err(ApiError::IncorrectRequest(res)) Err(ApiError::IncorrectRequest(res))
} }
} }
pub async fn send_with_res(&mut self) -> Vec<String> {
match self.send_with_err().await {
Ok(body) => self.parse(body),
Err(ApiError::IncorrectRequest(err)) => {
eprintln!("fatal: {}", err.status());
std::process::exit(1);
},
Err(ApiError::EmptyError(_)) => {
eprintln!("Failed to get body");
vec![]
}
Err(ApiError::RequestError(err)) => {
eprintln!("fatal: {}", err);
std::process::exit(1);
}
}
}
pub fn parse(&self, xml: String) -> Vec<String> { pub fn parse(&self, xml: String) -> Vec<String> {
let cursor = Cursor::new(xml); let cursor = Cursor::new(xml);
let parser = EventReader::new(cursor); let parser = EventReader::new(cursor);

View File

@ -1,47 +1,34 @@
use xml::reader::{EventReader, XmlEvent}; async fn upload_file(path: &str) -> Result<(), Box<dyn Error>> {
use std::fs::File; dotenv().ok();
use crate::services::api::{ApiBuilder, ApiError};
use std::path::PathBuf;
use std::io::{self, Read};
use reqwest::{Method, IntoUrl, Response, Error};
pub struct UploadFile { let mut api_endpoint = env::var("HOST").unwrap().to_owned();
api_builder: ApiBuilder, api_endpoint.push_str("/remote.php/dav/files/");
} let username = env::var("USERNAME").unwrap();
api_endpoint.push_str(&username);
impl UploadFile { api_endpoint.push_str("/test/ok");
pub fn new() -> Self { let password = env::var("PASSWORD").unwrap();
UploadFile {
api_builder: ApiBuilder::new(), let mut file = File::open("./file.test")?;
} let mut buffer = Vec::new();
} file.read_to_end(&mut buffer)?;
pub fn set_url(&mut self, url: &str) -> &mut UploadFile { // Create a reqwest client
self.api_builder.build_request(Method::PUT, url); let client = Client::new();
self
} // Send the request
let response = client
pub fn set_file(&mut self, path: PathBuf) -> &mut UploadFile { .request(reqwest::Method::PUT, api_endpoint)
// todo large file .basic_auth(username, Some(password))
// todo small files .body(buffer)
let mut file = File::open(path).unwrap(); .send()
let mut buffer = Vec::new(); .await?;
file.read_to_end(&mut buffer).unwrap();
self.api_builder.set_body(buffer); // Handle the response
self if response.status().is_success() {
} let body = response.text().await?;
println!("Response body: {}", body);
pub async fn send(&mut self) -> Result<Response, Error> { } else {
self.api_builder.send().await println!("Request failed with status code: {}", response.status());
} }
Ok(())
pub async fn send_with_err(&mut self) -> Result<String, ApiError> {
let res = self.send().await.map_err(ApiError::RequestError)?;
if res.status().is_success() {
let body = res.text().await.map_err(ApiError::EmptyError)?;
Ok(body)
} else {
Err(ApiError::IncorrectRequest(res))
}
}
} }