feat(enumerator): try to have an aux functions for the task
This commit is contained in:
parent
33d35aba49
commit
6989e87a56
@ -20,7 +20,9 @@ pub async fn exec(args: CloneArgs, config: Config) {
|
|||||||
.set_path(url_props.path.to_string())
|
.set_path(url_props.path.to_string())
|
||||||
// .set_depth(args.depth)
|
// .set_depth(args.depth)
|
||||||
.get_properties(vec![])
|
.get_properties(vec![])
|
||||||
.enumerate().await else { todo!()};
|
.enumerate().await else { todo!() };
|
||||||
|
dbg!(&files);
|
||||||
|
dbg!(&folders);
|
||||||
|
|
||||||
for folder in folders {
|
for folder in folders {
|
||||||
// create folder
|
// create folder
|
||||||
|
@ -2,15 +2,16 @@ use super::{
|
|||||||
req_props::{Props, ReqProps, Response},
|
req_props::{Props, ReqProps, Response},
|
||||||
service::Service,
|
service::Service,
|
||||||
};
|
};
|
||||||
|
use crate::utils::path;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use tokio::sync::Mutex;
|
use tokio::{task, sync::{mpsc::UnboundedSender, Mutex}};
|
||||||
|
|
||||||
pub const DEFAULT_DEPTH: usize = 3;
|
pub const DEFAULT_DEPTH: u16 = 2;
|
||||||
|
|
||||||
pub struct Enumerator<'a> {
|
pub struct Enumerator<'a> {
|
||||||
service: &'a Service,
|
service: &'a Service,
|
||||||
path: String,
|
path: String,
|
||||||
depth: usize,
|
depth: u16,
|
||||||
properties: Vec<Props>,
|
properties: Vec<Props>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -29,7 +30,7 @@ impl<'a> Enumerator<'a> {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_depth(mut self, depth: usize) -> Self {
|
pub fn set_depth(mut self, depth: u16) -> Self {
|
||||||
self.depth = depth;
|
self.depth = depth;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
@ -44,43 +45,34 @@ impl<'a> Enumerator<'a> {
|
|||||||
let files = Arc::new(Mutex::new(Vec::new()));
|
let files = Arc::new(Mutex::new(Vec::new()));
|
||||||
let folders = Arc::new(Mutex::new(Vec::new()));
|
let folders = Arc::new(Mutex::new(Vec::new()));
|
||||||
let service = Arc::from(self.service.clone());
|
let service = Arc::from(self.service.clone());
|
||||||
|
let tasks_active = Arc::new(Mutex::new(0));
|
||||||
|
|
||||||
tx.send(self.path.clone());
|
tx.send(self.path.clone());
|
||||||
|
|
||||||
let mut handles = vec![];
|
let mut handles = vec![];
|
||||||
|
|
||||||
while let Some(path) = rx.recv().await {
|
loop {
|
||||||
let tx_clone = tx.clone();
|
dbg!(*tasks_active.lock().await);
|
||||||
let files_clone = Arc::clone(&files);
|
if let Ok(path) = rx.try_recv() {
|
||||||
let folders_clone = Arc::clone(&folders);
|
handles.push(enumerator_task(EnumeratorTask{
|
||||||
let service_clone = Arc::clone(&service);
|
path,
|
||||||
let properties = self.properties.clone();
|
depth: self.depth.clone(),
|
||||||
|
tx: tx.clone(),
|
||||||
let handle = tokio::task::spawn_blocking(move || async move {
|
files: Arc::clone(&files),
|
||||||
let res = ReqProps::new(&service_clone)
|
folders: Arc::clone(&folders),
|
||||||
.set_path(path)
|
properties: self.properties.clone(),
|
||||||
// .set_depth(self.depth)
|
service: self.service,
|
||||||
.get_properties(properties)
|
tasks_active: Arc::clone(&tasks_active),
|
||||||
.send()
|
}));
|
||||||
.await
|
} else if *tasks_active.lock().await <= 0 {
|
||||||
.unwrap();
|
dbg!("brek");
|
||||||
|
break;
|
||||||
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
|
// Wait for all tasks to complete
|
||||||
for handle in handles {
|
for handle in handles {
|
||||||
handle.await;
|
let _ = handle.await;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok((
|
Ok((
|
||||||
@ -89,3 +81,46 @@ impl<'a> Enumerator<'a> {
|
|||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct EnumeratorTask<'a> {
|
||||||
|
path: String,
|
||||||
|
depth: u16,
|
||||||
|
tx: UnboundedSender<String>,
|
||||||
|
files: Arc<Mutex<Vec<Response>>>,
|
||||||
|
folders: Arc<Mutex<Vec<Response>>>,
|
||||||
|
properties: Vec<Props>,
|
||||||
|
service: &'a Service,
|
||||||
|
tasks_active: Arc<Mutex<u16>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn enumerator_task<'a, 'b>(data: EnumeratorTask<'a, 'b>) -> task::JoinHandle<()> {
|
||||||
|
let current_depth = path::get_depth(&data.path);
|
||||||
|
*data.tasks_active.lock().await += 1;
|
||||||
|
|
||||||
|
tokio::task::spawn(async move {
|
||||||
|
let res = ReqProps::new(data.service)
|
||||||
|
.set_path(data.path.clone())
|
||||||
|
// .set_depth(self.depth)
|
||||||
|
.get_properties(data.properties)
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
dbg!(&res);
|
||||||
|
for obj in res.responses {
|
||||||
|
if obj.is_dir() {
|
||||||
|
// Avoid enumerating the same folder multiple times
|
||||||
|
if obj.abs_path() != data.path {
|
||||||
|
// depth deeper than current + self.depth
|
||||||
|
if obj.path_depth() > current_depth + data.depth {
|
||||||
|
data.tx.send(obj.abs_path().to_owned()).unwrap();
|
||||||
|
}
|
||||||
|
data.folders.lock().await.push(obj);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
data.files.lock().await.push(obj);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*data.tasks_active.lock().await -= 1;
|
||||||
|
})
|
||||||
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
use crate::services::service::{Request, Service};
|
use crate::services::service::{Request, Service};
|
||||||
use crate::store::object::Obj;
|
use crate::store::object::Obj;
|
||||||
|
use crate::utils::path;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use serde_xml_rs::from_str;
|
use serde_xml_rs::from_str;
|
||||||
|
|
||||||
@ -49,7 +50,27 @@ pub struct Response {
|
|||||||
|
|
||||||
impl Response {
|
impl Response {
|
||||||
pub fn is_dir(&self) -> bool {
|
pub fn is_dir(&self) -> bool {
|
||||||
todo!("is dir reponse")
|
self.href.ends_with("/")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn abs_path(&self) -> &str {
|
||||||
|
let path = self
|
||||||
|
.href
|
||||||
|
.strip_prefix("/remote.php/dav/files")
|
||||||
|
.expect(&format!(
|
||||||
|
"Unexpected result when requesting props. Cannot strip from {}",
|
||||||
|
self.href
|
||||||
|
));
|
||||||
|
|
||||||
|
if path.ends_with('/') {
|
||||||
|
path.strip_suffix('/').expect("Checked before")
|
||||||
|
} else {
|
||||||
|
path
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn path_depth(&self) -> u16 {
|
||||||
|
path::get_depth(self.abs_path())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use crate::commands::clone::UrlProps;
|
use crate::commands::clone::UrlProps;
|
||||||
use reqwest::{header::HeaderMap, Method, Url};
|
use reqwest::{header::HeaderMap, Method, Url};
|
||||||
use reqwest::{Client, ClientBuilder};
|
use reqwest::{Client, ClientBuilder, RequestBuilder};
|
||||||
|
|
||||||
const USER_AGENT: &str = "Nextsync";
|
const USER_AGENT: &str = "Nextsync";
|
||||||
|
|
||||||
@ -36,23 +36,31 @@ impl ClientConfig {
|
|||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Service {
|
pub struct Service {
|
||||||
|
/// http[s]://host.xz/remote.php/dav/files
|
||||||
url_base: String,
|
url_base: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<&UrlProps<'_>> for Service {
|
impl From<&UrlProps<'_>> for Service {
|
||||||
fn from(url_props: &UrlProps) -> Self {
|
fn from(url_props: &UrlProps) -> Self {
|
||||||
todo!("Auth");
|
|
||||||
let mut url_base = if url_props.is_secure {
|
let mut url_base = if url_props.is_secure {
|
||||||
String::from("https://")
|
String::from("https://")
|
||||||
} else {
|
} else {
|
||||||
String::from("http://")
|
String::from("http://")
|
||||||
};
|
};
|
||||||
url_base.push_str(url_props.domain);
|
url_base.push_str(url_props.domain);
|
||||||
|
url_base.push_str("/remote.php/dav/files");
|
||||||
|
|
||||||
Service { url_base }
|
Service { url_base }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Service {
|
||||||
|
fn authenticate(&self, request: RequestBuilder) -> RequestBuilder {
|
||||||
|
request
|
||||||
|
.bearer_auth("rK5ud2NmrR8p586Th7v272HRgUcZcEKIEluOGjzQQRj7gWMMAISFTiJcFnnmnNiu2VVlENks")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Request<'a> {
|
pub struct Request<'a> {
|
||||||
service: &'a Service,
|
service: &'a Service,
|
||||||
client: ClientConfig,
|
client: ClientConfig,
|
||||||
@ -81,7 +89,17 @@ impl<'a> Request<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn send(&mut self) -> Result<reqwest::Response, reqwest::Error> {
|
pub async fn send(&mut self) -> Result<reqwest::Response, reqwest::Error> {
|
||||||
todo!()
|
self.service
|
||||||
|
.authenticate(self.client.build().request(
|
||||||
|
self.method.clone().expect("Method must be set"),
|
||||||
|
{
|
||||||
|
let mut url = self.service.url_base.clone();
|
||||||
|
url.push_str(&self.url.clone().expect("An url must be set"));
|
||||||
|
url
|
||||||
|
},
|
||||||
|
))
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
// let mut url = self
|
// let mut url = self
|
||||||
// .config
|
// .config
|
||||||
// .clone()
|
// .clone()
|
||||||
@ -95,7 +113,6 @@ impl<'a> Request<'a> {
|
|||||||
// 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")
|
|
||||||
// .send()
|
// .send()
|
||||||
// .await
|
// .await
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use clap::{Arg, ArgMatches, Command};
|
use clap::{Arg, ArgAction, ArgMatches, Command};
|
||||||
|
|
||||||
use crate::commands;
|
use crate::commands;
|
||||||
use crate::config::config::Config;
|
use crate::config::config::Config;
|
||||||
@ -22,8 +22,9 @@ pub fn create() -> Command {
|
|||||||
)
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::new("force_insecure")
|
Arg::new("force_insecure")
|
||||||
.long("force-insecure")
|
|
||||||
.short('f')
|
.short('f')
|
||||||
|
.long("force-insecure")
|
||||||
|
.action(ArgAction::SetTrue)
|
||||||
.help("Force the connection to nextcloud to be in http (not https)")
|
.help("Force the connection to nextcloud to be in http (not https)")
|
||||||
)
|
)
|
||||||
.arg(
|
.arg(
|
||||||
@ -48,7 +49,7 @@ pub async fn handler(args: &ArgMatches) {
|
|||||||
commands::clone::CloneArgs {
|
commands::clone::CloneArgs {
|
||||||
remote: remote.to_string(),
|
remote: remote.to_string(),
|
||||||
depth: args.get_one::<String>("depth").cloned(),
|
depth: args.get_one::<String>("depth").cloned(),
|
||||||
force_insecure: args.contains_id("force_insecure"),
|
force_insecure: *args.get_one::<bool>("force_insecure").unwrap(),
|
||||||
},
|
},
|
||||||
Config::from(args.get_one::<String>("directory")),
|
Config::from(args.get_one::<String>("directory")),
|
||||||
)
|
)
|
||||||
|
@ -54,3 +54,8 @@ pub fn normalize_path<P: AsRef<Path>>(path: P) -> PathBuf {
|
|||||||
}
|
}
|
||||||
normalized
|
normalized
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Calculate depth of a path
|
||||||
|
pub fn get_depth(path: &str) -> u16 {
|
||||||
|
path.split("/").count() as u16
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user