add deletion of file

This commit is contained in:
grimhilt 2023-06-15 17:33:12 +02:00
parent 35adfab983
commit 49d83c2900
5 changed files with 93 additions and 65 deletions

View File

@ -23,18 +23,19 @@ pub fn add(files: Values<'_>) {
for file in file_vec {
let path = Path::new(file);
println!("{}", file);
match path.try_exists() {
Ok(true) => {
match path.exists() {
true => {
match writeln!(index_file, "{}", path.display()) {
Ok(()) => (),
Err(err) => eprintln!("{}", err),
}
},
Ok(false) => {
false => {
match writeln!(index_file, "{}", path.display()) {
Ok(()) => (),
Err(err) => eprintln!("{}", err),
}
// todo can be regex
},
Err(err) => {
eprintln!("Error: {}", err);
}
}
}

View File

@ -2,7 +2,10 @@ use crate::commands::{status, config};
use crate::services::req_props::ReqProps;
use crate::services::api::ApiError;
use crate::services::upload_file::UploadFile;
use crate::services::delete_path::DeletePath;
use crate::commands::status::{State, Obj};
use crate::store::object::{add_blob, rm_blob};
use crate::store::index;
pub fn push() {
dbg!(status::get_all_staged());
@ -24,6 +27,7 @@ pub fn push() {
let push_factory = PushFactory.new(obj.clone());
match push_factory.can_push() {
PushState::Valid => push_factory.push(),
PushState::Done => (),
_ => todo!(),
}
}
@ -35,6 +39,7 @@ pub fn push() {
#[derive(Debug)]
enum PushState {
Done,
Valid,
Conflict,
Error,
@ -77,7 +82,7 @@ impl PushChange for New {
// file doesn't exist on remote
PushState::Valid
} else {
// check date
// todo check date
PushState::Conflict
}
} else {
@ -105,9 +110,82 @@ impl PushChange for New {
}
_ => (),
}
// todo manage err
// todo remove index
});
// update tree
add_blob(&obj.path.clone(), "todo_date");
// remove index
index::rm_line(obj.path.to_str().unwrap());
}
}
struct Deleted {
obj: Obj,
}
impl PushChange for Deleted {
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::Done
} else {
// todo check date
//PushState::Conflict
PushState::Valid
}
} else {
PushState::Error
}
}
fn push(&self) {
let obj = &self.obj;
tokio::runtime::Runtime::new().unwrap().block_on(async {
let res = DeletePath::new()
.set_url(obj.path.to_str().unwrap())
.send_with_err()
.await;
match res {
Err(ApiError::IncorrectRequest(err)) => {
eprintln!("fatal: error deleting file {}: {}", obj.name, err.status());
std::process::exit(1);
},
Err(ApiError::RequestError(_)) => {
eprintln!("fatal: request error deleting file {}", obj.name);
std::process::exit(1);
}
_ => (),
}
});
// update tree
rm_blob(&obj.path.clone());
// remove index
index::rm_line(obj.path.to_str().unwrap());
}
}
@ -119,7 +197,7 @@ impl PushFactory {
State::New => Box::new(New { obj: obj.clone() }),
State::Renamed => todo!(),
State::Modified => todo!(),
State::Deleted => todo!(),
State::Deleted => Box::new(Deleted { obj: obj.clone() }),
State::Default => todo!(),
}
}

View File

@ -1,50 +0,0 @@
async fn send_propfind_request() -> Result<(), Box<dyn Error>> {
dotenv().ok();
let mut api_endpoint = env::var("HOST").unwrap().to_owned();
api_endpoint.push_str("/remote.php/dav/files/");
let username = env::var("USERNAME").unwrap();
api_endpoint.push_str(&username);
api_endpoint.push_str("/test");
let password = env::var("PASSWORD").unwrap();
// Create a reqwest client
let client = Client::new();
// Create the XML payload
let xml_payload = r#"<?xml version="1.0" encoding="UTF-8"?>
<d:propfind xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns" xmlns:nc="http://nextcloud.org/ns">
<d:prop>
<d:getlastmodified/>
<d:getcontentlength/>
<d:getcontenttype/>
<oc:permissions/>
<d:resourcetype/>
<d:getetag/>
) </d:prop>
</d:propfind>"#;
let mut headers = HeaderMap::new();
headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/xml"));
// Send the request
let response = client
.request(reqwest::Method::from_bytes(b"PROPFIND").unwrap(), api_endpoint)
.basic_auth(username, Some(password))
.headers(headers)
.body(xml_payload)
.send()
.await?;
// Handle the response
if response.status().is_success() {
let body = response.text().await?;
println!("Response body: {}", body);
} else {
println!("Request failed with status code: {}", response.status());
}
Ok(())
}

View File

@ -1,7 +1,7 @@
use crate::services::api::{ApiBuilder, ApiError};
use xml::reader::{EventReader, XmlEvent};
use std::io::{self, Cursor};
use reqwest::{Method, IntoUrl, Response, Error};
use std::io::Cursor;
use reqwest::{Method, Response, Error};
pub struct ReqProps {
api_builder: ApiBuilder,

View File

@ -1,9 +1,8 @@
use xml::reader::{EventReader, XmlEvent};
use std::fs::File;
use crate::services::api::{ApiBuilder, ApiError};
use std::path::PathBuf;
use std::io::{self, Read};
use reqwest::{Method, IntoUrl, Response, Error};
use std::io::{Read};
use reqwest::{Method, Response, Error};
pub struct UploadFile {
api_builder: ApiBuilder,