feat(enumerator): started enumerator and improved service
This commit is contained in:
parent
d442bf689c
commit
adac2e6cda
0
src/services/downloader.rs
Normal file
0
src/services/downloader.rs
Normal file
91
src/services/enumerator.rs
Normal file
91
src/services/enumerator.rs
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
use super::{
|
||||||
|
req_props::{Props, ReqProps, Response},
|
||||||
|
service::Service,
|
||||||
|
};
|
||||||
|
use std::sync::Arc;
|
||||||
|
use tokio::sync::Mutex;
|
||||||
|
|
||||||
|
pub const DEFAULT_DEPTH: usize = 3;
|
||||||
|
|
||||||
|
pub struct Enumerator<'a> {
|
||||||
|
service: &'a Service,
|
||||||
|
path: String,
|
||||||
|
depth: usize,
|
||||||
|
properties: Vec<Props>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Enumerator<'a> {
|
||||||
|
pub fn new(service: &'a Service) -> Self {
|
||||||
|
Enumerator {
|
||||||
|
service,
|
||||||
|
path: String::new(),
|
||||||
|
depth: DEFAULT_DEPTH,
|
||||||
|
properties: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_path(mut self, path: String) -> Self {
|
||||||
|
self.path = path;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_depth(mut self, depth: usize) -> Self {
|
||||||
|
self.depth = depth;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_properties(mut self, properties: Vec<Props>) -> Self {
|
||||||
|
self.properties.extend(properties);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn enumerate(&self) -> Result<(Vec<Response>, Vec<Response>), std::io::Error> {
|
||||||
|
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
|
||||||
|
let files = Arc::new(Mutex::new(Vec::new()));
|
||||||
|
let folders = Arc::new(Mutex::new(Vec::new()));
|
||||||
|
let service = Arc::from(self.service.clone());
|
||||||
|
|
||||||
|
tx.send(self.path.clone());
|
||||||
|
|
||||||
|
let mut handles = vec![];
|
||||||
|
|
||||||
|
while let Some(path) = rx.recv().await {
|
||||||
|
let tx_clone = tx.clone();
|
||||||
|
let files_clone = Arc::clone(&files);
|
||||||
|
let folders_clone = Arc::clone(&folders);
|
||||||
|
let service_clone = Arc::clone(&service);
|
||||||
|
let properties = self.properties.clone();
|
||||||
|
|
||||||
|
let handle = tokio::task::spawn_blocking(move || async move {
|
||||||
|
let res = ReqProps::new(&service_clone)
|
||||||
|
.set_path(path)
|
||||||
|
// .set_depth(self.depth)
|
||||||
|
.get_properties(properties)
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
for obj in res.responses {
|
||||||
|
if obj.is_dir() {
|
||||||
|
todo!("Deal to have good href");
|
||||||
|
tx_clone.send(obj.href.clone());
|
||||||
|
folders_clone.lock().await.push(obj);
|
||||||
|
} else {
|
||||||
|
files_clone.lock().await.push(obj);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
handles.push(handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait for all tasks to complete
|
||||||
|
for handle in handles {
|
||||||
|
handle.await;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok((
|
||||||
|
Arc::try_unwrap(files).unwrap().into_inner(),
|
||||||
|
Arc::try_unwrap(folders).unwrap().into_inner(),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
@ -1,9 +1,9 @@
|
|||||||
use crate::services::service::Service;
|
use crate::services::service::{Request, Service};
|
||||||
use crate::store::object::Obj;
|
use crate::store::object::Obj;
|
||||||
use crate::config::config::Config;
|
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use serde_xml_rs::from_str;
|
use serde_xml_rs::from_str;
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
pub enum Props {
|
pub enum Props {
|
||||||
CreationDate,
|
CreationDate,
|
||||||
LastModified,
|
LastModified,
|
||||||
@ -25,7 +25,6 @@ pub enum Props {
|
|||||||
ContainedFileCountm,
|
ContainedFileCountm,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
#[derive(Deserialize, Debug)]
|
||||||
struct Propstat {
|
struct Propstat {
|
||||||
#[serde(rename = "prop")]
|
#[serde(rename = "prop")]
|
||||||
@ -41,19 +40,24 @@ struct Prop {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
#[derive(Deserialize, Debug)]
|
||||||
struct Response {
|
pub struct Response {
|
||||||
#[serde(rename = "href")]
|
#[serde(rename = "href")]
|
||||||
href: String,
|
pub href: String,
|
||||||
#[serde(rename = "propstat")]
|
#[serde(rename = "propstat")]
|
||||||
propstat: Propstat,
|
propstat: Propstat,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
impl Response {
|
||||||
struct Multistatus {
|
pub fn is_dir(&self) -> bool {
|
||||||
#[serde(rename = "response")]
|
todo!("is dir reponse")
|
||||||
responses: Vec<Response>,
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Debug)]
|
||||||
|
pub struct Multistatus {
|
||||||
|
#[serde(rename = "response")]
|
||||||
|
pub responses: Vec<Response>,
|
||||||
|
}
|
||||||
|
|
||||||
impl From<&Props> for &str {
|
impl From<&Props> for &str {
|
||||||
fn from(variant: &Props) -> Self {
|
fn from(variant: &Props) -> Self {
|
||||||
@ -65,23 +69,21 @@ impl From<&Props> for &str {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ReqProps {
|
pub struct ReqProps<'a> {
|
||||||
service: Service,
|
request: Request<'a>,
|
||||||
properties: Vec<Props>,
|
properties: Vec<Props>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ReqProps {
|
impl<'a> ReqProps<'a> {
|
||||||
pub fn new(path: &str) -> Self {
|
pub fn new(service: &'a Service) -> Self {
|
||||||
let mut service = Service::new();
|
|
||||||
service.propfind(path.to_owned());
|
|
||||||
ReqProps {
|
ReqProps {
|
||||||
service,
|
request: Request::new(service),
|
||||||
properties: Vec::new(),
|
properties: Vec::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_config(mut self, config: &Config) -> Self {
|
pub fn set_path(mut self, path: String) -> Self {
|
||||||
self.service.set_config(config.clone());
|
self.request.propfind(path);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -105,23 +107,16 @@ impl ReqProps {
|
|||||||
xml
|
xml
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn send(&mut self) {
|
pub async fn send(&mut self) -> Result<Multistatus, reqwest::Error> {
|
||||||
dbg!("send");
|
let res = self.request.send().await;
|
||||||
let res = self.service.send().await;
|
let xml = res.unwrap().text().await?;
|
||||||
dbg!("Sent");
|
|
||||||
let text = res.unwrap().text().await.unwrap();
|
|
||||||
self.parse(&text);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parse (&self, xml: &str) {
|
|
||||||
dbg!("Parsing");
|
|
||||||
let multistatus: Multistatus = from_str(&xml).unwrap();
|
let multistatus: Multistatus = from_str(&xml).unwrap();
|
||||||
dbg!(multistatus);
|
Ok(multistatus)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<Obj> for ReqProps {
|
// impl From<Obj> for ReqProps<'_> {
|
||||||
fn from(obj: Obj) -> Self {
|
// fn from(obj: Obj) -> Self {
|
||||||
ReqProps::new(obj.get_obj_path().to_str().unwrap())
|
// ReqProps::new(obj.get_obj_path().to_str().unwrap())
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use crate::config::config::Config;
|
use crate::commands::clone::UrlProps;
|
||||||
use reqwest::{Client, ClientBuilder};
|
|
||||||
use reqwest::{header::HeaderMap, Method, Url};
|
use reqwest::{header::HeaderMap, Method, Url};
|
||||||
|
use reqwest::{Client, ClientBuilder};
|
||||||
|
|
||||||
const USER_AGENT: &str = "Nextsync";
|
const USER_AGENT: &str = "Nextsync";
|
||||||
|
|
||||||
@ -34,20 +34,39 @@ impl ClientConfig {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
pub struct Service {
|
pub struct Service {
|
||||||
|
url_base: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<&UrlProps<'_>> for Service {
|
||||||
|
fn from(url_props: &UrlProps) -> Self {
|
||||||
|
todo!("Auth");
|
||||||
|
let mut url_base = if url_props.is_secure {
|
||||||
|
String::from("https://")
|
||||||
|
} else {
|
||||||
|
String::from("http://")
|
||||||
|
};
|
||||||
|
url_base.push_str(url_props.domain);
|
||||||
|
|
||||||
|
Service { url_base }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Request<'a> {
|
||||||
|
service: &'a Service,
|
||||||
client: ClientConfig,
|
client: ClientConfig,
|
||||||
config: Option<Config>,
|
|
||||||
method: Option<Method>,
|
method: Option<Method>,
|
||||||
url: Option<String>,
|
url: Option<String>,
|
||||||
headers: HeaderMap,
|
headers: HeaderMap,
|
||||||
body: Option<String>,
|
body: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Service {
|
impl<'a> Request<'a> {
|
||||||
pub fn new() -> Self {
|
pub fn new(service: &'a Service) -> Self {
|
||||||
Service {
|
Request {
|
||||||
|
service,
|
||||||
client: ClientConfig::new(),
|
client: ClientConfig::new(),
|
||||||
config: None,
|
|
||||||
method: None,
|
method: None,
|
||||||
url: None,
|
url: None,
|
||||||
headers: HeaderMap::new(),
|
headers: HeaderMap::new(),
|
||||||
@ -55,10 +74,6 @@ impl Service {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_config(&mut self, config: Config) {
|
|
||||||
self.config = Some(config);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn propfind(&mut self, url: String) -> &mut Self {
|
pub fn propfind(&mut self, url: String) -> &mut Self {
|
||||||
self.method = Some(Method::from_bytes(b"PROPFIND").expect("Cannot be invalid"));
|
self.method = Some(Method::from_bytes(b"PROPFIND").expect("Cannot be invalid"));
|
||||||
self.url = Some(url);
|
self.url = Some(url);
|
||||||
@ -66,21 +81,22 @@ impl Service {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn send(&mut self) -> Result<reqwest::Response, reqwest::Error> {
|
pub async fn send(&mut self) -> Result<reqwest::Response, reqwest::Error> {
|
||||||
let mut url = self
|
todo!()
|
||||||
.config
|
// let mut url = self
|
||||||
.clone()
|
// .config
|
||||||
.expect("A config must be provided to service")
|
// .clone()
|
||||||
.get_nsconfig()
|
// .expect("A config must be provided to service")
|
||||||
.get_remote("origin")
|
// .get_nsconfig()
|
||||||
.url
|
// .get_remote("origin")
|
||||||
.expect("An url must be set on the remote");
|
// .url
|
||||||
url.push_str(&self.url.clone().unwrap());
|
// .expect("An url must be set on the remote");
|
||||||
|
// url.push_str(&self.url.clone().unwrap());
|
||||||
|
|
||||||
self.client
|
// self.client
|
||||||
.build()
|
// .build()
|
||||||
.request(self.method.clone().expect("Method must be set"), url)
|
// .request(self.method.clone().expect("Method must be set"), url)
|
||||||
.bearer_auth("rK5ud2NmrR8p586Th7v272HRgUcZcEKIEluOGjzQQRj7gWMMAISFTiJcFnnmnNiu2VVlENks")
|
// .bearer_auth("rK5ud2NmrR8p586Th7v272HRgUcZcEKIEluOGjzQQRj7gWMMAISFTiJcFnnmnNiu2VVlENks")
|
||||||
.send()
|
// .send()
|
||||||
.await
|
// .await
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user