Compare commits

...

2 Commits

Author SHA1 Message Date
grimhilt
33d35aba49 feat(clone): created command clone 2024-09-15 21:27:03 +02:00
grimhilt
adac2e6cda feat(enumerator): started enumerator and improved service 2024-09-15 21:26:49 +02:00
9 changed files with 228 additions and 89 deletions

View File

@ -1,19 +1,41 @@
use crate::config::config::Config; use crate::config::config::Config;
use crate::services::{service::Service, enumerator::Enumerator};
use regex::Regex; use regex::Regex;
pub struct CloneArgs { pub struct CloneArgs {
pub remote: String, pub remote: String,
pub depth: Option<String>, pub depth: Option<String>,
pub force_insecure: bool,
} }
pub fn exec(args: CloneArgs, config: Config) { pub async fn exec(args: CloneArgs, config: Config) {
get_url_props(&args.remote); let mut url_props = get_url_props(&args.remote);
if args.force_insecure {
url_props.is_secure = false;
} }
struct UrlProps<'a> { let service = Service::from(&url_props);
is_secure: bool, let Ok((files, folders)) = Enumerator::new(&service)
domain: &'a str, .set_path(url_props.path.to_string())
path: &'a str, // .set_depth(args.depth)
.get_properties(vec![])
.enumerate().await else { todo!()};
for folder in folders {
// create folder
// save folder
}
// let downloader = Downloader::new(&service, config.get_root_unsafe())
// .set_files(files.map(|file| todo!()))
// .download();
}
pub struct UrlProps<'a> {
pub is_secure: bool,
pub domain: &'a str,
pub path: &'a str,
} }
impl UrlProps<'_> { impl UrlProps<'_> {

View File

@ -18,12 +18,12 @@ pub fn exec(args: TestArgs, config: Config) {
// println!("{:?}", config); // println!("{:?}", config);
// Ok(()) // Ok(())
tokio::runtime::Runtime::new().unwrap().block_on(async { // tokio::runtime::Runtime::new().unwrap().block_on(async {
let mut req = ReqProps::new("") // let mut req = ReqProps::new("")
.set_config(&config) // .set_config(&config)
.get_properties(vec![Props::CreationDate, Props::LastModified]); // .get_properties(vec![Props::CreationDate, Props::LastModified]);
req.send().await; // req.send().await;
}); // });
// dbg!(req); // dbg!(req);
} }

View File

@ -7,7 +7,8 @@ mod subcommands;
mod utils; mod utils;
mod services; mod services;
fn main() { #[tokio::main]
async fn main() {
let app = Command::new("Nextsync") let app = Command::new("Nextsync")
.version("1.0") .version("1.0")
.author("grimhilt") .author("grimhilt")
@ -32,7 +33,7 @@ fn main() {
Some(("reset", args)) => subcommands::reset::handler(args), Some(("reset", args)) => subcommands::reset::handler(args),
Some(("push", args)) => subcommands::push::handler(args), Some(("push", args)) => subcommands::push::handler(args),
Some(("test", args)) => subcommands::test::handler(args), Some(("test", args)) => subcommands::test::handler(args),
Some(("clone", args)) => subcommands::clone::handler(args), Some(("clone", args)) => subcommands::clone::handler(args).await,
Some((_, _)) => {} Some((_, _)) => {}
None => {} None => {}
}; };

View File

@ -1,2 +1,4 @@
pub mod service; pub mod downloader;
pub mod enumerator;
pub mod req_props; pub mod req_props;
pub mod service;

View File

View 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(),
))
}
}

View File

@ -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())
} // }
} // }

View File

@ -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
} }
} }

View File

@ -1,18 +1,16 @@
use clap::{Arg, Command, ArgMatches}; use clap::{Arg, ArgMatches, Command};
use crate::commands; use crate::commands;
use crate::config::config::Config; use crate::config::config::Config;
pub fn create() -> Command { pub fn create() -> Command {
// let remote_desc = sized_str(&format!("The repository to clone from. See the NEXTSYNC URLS section below for more information on specifying repositories."));
// let depth_desc = sized_str(&format!("Depth of the recursive fetch of object properties. This value should be lower when there are a lot of files per directory and higher when there are a lot of subdirectories with fewer files. (Default: {})", clone::DEPTH));
Command::new("clone") Command::new("clone")
.arg( .arg(
Arg::new("remote") Arg::new("remote")
.required(true) .required(true)
.num_args(1) .num_args(1)
.value_name("REMOTE") .value_name("REMOTE")
//.help(_desc) .help("The repository to clone from. See the NEXTSYNC URLS section below for more information on specifying repositories.")
) )
.arg( .arg(
Arg::new("depth") Arg::new("depth")
@ -20,7 +18,13 @@ pub fn create() -> Command {
.long("depth") .long("depth")
.required(false) .required(false)
.num_args(1) .num_args(1)
//.help(&depth_desc) .help(format!("Depth of the recursive fetch of object properties. This value should be lower when there are a lot of files per directory and higher when there are a lot of subdirectories with fewer files. (Default: {})", crate::services::enumerator::DEFAULT_DEPTH))
)
.arg(
Arg::new("force_insecure")
.long("force-insecure")
.short('f')
.help("Force the connection to nextcloud to be in http (not https)")
) )
.arg( .arg(
Arg::new("directory") Arg::new("directory")
@ -29,17 +33,25 @@ pub fn create() -> Command {
.value_name("DIRECTORY") .value_name("DIRECTORY")
) )
.about("Clone a repository into a new directory") .about("Clone a repository into a new directory")
.after_help("NEXTSYNC URLS\nThe following syntaxes may be used:\n\t- user@host.xz/path/to/repo\n\t- http[s]://host.xz/apps/files/?dir=/path/to/repo&fileid=111111\n\t- [http[s]://]host.xz/remote.php/dav/files/user/path/to/repo\n") .after_help("
NEXTSYNC URLS
The following syntaxes may be used:
- [http[s]://]host.xz/apps/files/?dir=/path/to/repo&fileid=111111
- [http[s]://]host.xz/path/to/repo
- [http[s]://]host.xz/remote.php/dav/files/user/path/to/repo
")
} }
pub fn handler(args: &ArgMatches) { pub async fn handler(args: &ArgMatches) {
if let Some(val) = args.get_one::<String>("directory") {
// global::global::set_dir_path(String::from(val.to_string()));
}
if let Some(remote) = args.get_one::<String>("remote") { if let Some(remote) = args.get_one::<String>("remote") {
commands::clone::exec(commands::clone::CloneArgs { commands::clone::exec(
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(),
}, Config::new()); force_insecure: args.contains_id("force_insecure"),
},
Config::from(args.get_one::<String>("directory")),
)
.await;
} }
} }