cleaning clone

This commit is contained in:
grimhilt
2023-06-17 15:36:18 +02:00
parent b16058b4d3
commit 7cfd572ad0
6 changed files with 118 additions and 48 deletions

View File

@@ -1,6 +1,7 @@
use reqwest::Client;
use reqwest::RequestBuilder;
use reqwest::{Response, Error, IntoUrl, Method};
use crate::utils::api::ApiProps;
use std::env;
use dotenv::dotenv;
@@ -25,7 +26,7 @@ impl ApiBuilder {
}
}
pub fn set_request<U: IntoUrl>(mut self, method: Method, url: U) -> ApiBuilder {
pub fn set_request<U: IntoUrl>(&mut self, method: Method, url: U) -> &mut ApiBuilder {
self.request = Some(self.client.request(method, url));
self
}
@@ -48,13 +49,14 @@ impl ApiBuilder {
self
}
pub fn build_request_remote(&mut self, meth: Method, path: &str) -> &mut ApiBuilder {
dotenv().ok();
let host = env::var("HOST").unwrap();
let mut url = String::from(host);
pub fn set_req(&mut self, meth: Method, p: &str, api_props: &ApiProps) -> &mut ApiBuilder {
let mut url = String::from(&api_props.host);
url.push_str("/remote.php/dav/files/");
url.push_str("/");
url.push_str(path);
dbg!(url.clone());
url.push_str(&api_props.username);
url.push_str(&api_props.root);
url.push_str("/");
url.push_str(p);
self.request = Some(self.client.request(meth, url));
self
}

View File

@@ -1,26 +1,26 @@
use crate::services::api::{ApiBuilder, ApiError};
use std::path::PathBuf;
use reqwest::{Method, Response, Error};
use crate::utils::api::get_local_path_t;
use crate::utils::api::{get_local_path_t, ApiProps};
use std::fs::OpenOptions;
use std::io::{self, Write};
pub struct DownloadFiles {
api_builder: ApiBuilder,
path: String,
relative_ps: String,
}
impl DownloadFiles {
pub fn new() -> Self {
DownloadFiles {
api_builder: ApiBuilder::new(),
path: String::from(""),
relative_ps: String::from(""),
}
}
pub fn set_url_with_remote(&mut self, url: &str) -> &mut DownloadFiles {
self.path = get_local_path_t(url.clone()).strip_prefix("/").unwrap().to_string();
self.api_builder.build_request_remote(Method::GET, url);
pub fn set_url(&mut self, relative_ps: &str, api_props: &ApiProps) -> &mut DownloadFiles {
self.relative_ps = relative_ps.to_string();
self.api_builder.set_req(Method::from_bytes(b"PROPFIND").unwrap(), relative_ps, api_props);
self
}
@@ -38,8 +38,8 @@ impl DownloadFiles {
}
}
pub async fn save(&mut self, local_path: PathBuf) -> Result<(), ApiError> {
let p = local_path.join(PathBuf::from(self.path.clone()));
pub async fn save(&mut self, ref_p: PathBuf) -> Result<(), ApiError> {
let p = ref_p.join(PathBuf::from(self.relative_ps.clone()));
let res = self.send().await.map_err(ApiError::RequestError)?;
if res.status().is_success() {
let body = res.bytes().await.map_err(ApiError::EmptyError)?;
@@ -53,6 +53,7 @@ impl DownloadFiles {
}
fn write_file(path: PathBuf, content: &Vec<u8>) -> io::Result<()> {
dbg!(path.clone());
let mut f = OpenOptions::new()
.write(true)
.create(true)

View File

@@ -1,16 +1,20 @@
use crate::services::api::{ApiBuilder, ApiError};
use crate::utils::api::{ApiProps, get_relative_s};
use xml::reader::{EventReader, XmlEvent};
use std::io::Cursor;
use reqwest::{Method, IntoUrl, Response, Error};
use reqwest::{Method, Response, Error};
#[derive(Debug)]
pub struct FolderContent {
pub href: Option<String>,
pub relative_s: Option<String>,
}
impl Clone for FolderContent {
fn clone(&self) -> Self {
FolderContent {
href: self.href.clone(),
relative_s: self.relative_s.clone(),
}
}
}
@@ -19,6 +23,7 @@ impl FolderContent {
fn new() -> Self {
FolderContent {
href: None,
relative_s: None,
}
}
}
@@ -26,17 +31,29 @@ impl FolderContent {
pub struct ListFolders {
api_builder: ApiBuilder,
xml_balises: Vec<String>,
api_props: Option<ApiProps>
}
impl ListFolders {
pub fn new<U: IntoUrl>(url: U) -> Self {
pub fn new() -> Self {
ListFolders {
api_builder: ApiBuilder::new()
.set_request(Method::from_bytes(b"PROPFIND").unwrap(), url),
api_builder: ApiBuilder::new(),
xml_balises: vec![],
api_props: None,
}
}
pub fn set_url(&mut self, url: &str) -> &mut ListFolders {
self.api_builder.build_request(Method::from_bytes(b"PROPFIND").unwrap(), url);
self
}
pub fn set_request(&mut self, p: &str, api_props: &ApiProps) -> &mut ListFolders {
self.api_props = Some(api_props.clone());
self.api_builder.set_req(Method::from_bytes(b"PROPFIND").unwrap(), p, api_props);
self
}
pub fn gethref(&mut self) -> &mut ListFolders {
self.xml_balises.push(String::from("href"));
self
@@ -93,7 +110,11 @@ impl ListFolders {
Ok(XmlEvent::Characters(text)) => {
if !text.trim().is_empty() && should_get {
match val.unwrap().as_str() {
"href" => content.href = Some(text),
"href" => {
content.href = Some(text.clone());
content.relative_s = Some(get_relative_s(text, &(self.api_props.clone().unwrap())));
dbg!(content.relative_s.clone());
},
_ => (),
}
val = iter.next()