Compare commits
No commits in common. "33d35aba496cd3003e4e83137f164ee2d5d82ef7" and "d442bf689cdbd4e5bedd59cd83ff97d67d9dab85" have entirely different histories.
33d35aba49
...
d442bf689c
@ -1,41 +1,19 @@
|
||||
use crate::config::config::Config;
|
||||
use crate::services::{service::Service, enumerator::Enumerator};
|
||||
use regex::Regex;
|
||||
|
||||
pub struct CloneArgs {
|
||||
pub remote: String,
|
||||
pub depth: Option<String>,
|
||||
pub force_insecure: bool,
|
||||
}
|
||||
|
||||
pub async fn exec(args: CloneArgs, config: Config) {
|
||||
let mut url_props = get_url_props(&args.remote);
|
||||
|
||||
if args.force_insecure {
|
||||
url_props.is_secure = false;
|
||||
pub fn exec(args: CloneArgs, config: Config) {
|
||||
get_url_props(&args.remote);
|
||||
}
|
||||
|
||||
let service = Service::from(&url_props);
|
||||
let Ok((files, folders)) = Enumerator::new(&service)
|
||||
.set_path(url_props.path.to_string())
|
||||
// .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,
|
||||
struct UrlProps<'a> {
|
||||
is_secure: bool,
|
||||
domain: &'a str,
|
||||
path: &'a str,
|
||||
}
|
||||
|
||||
impl UrlProps<'_> {
|
||||
|
@ -18,12 +18,12 @@ pub fn exec(args: TestArgs, config: Config) {
|
||||
// println!("{:?}", config);
|
||||
|
||||
// Ok(())
|
||||
// tokio::runtime::Runtime::new().unwrap().block_on(async {
|
||||
tokio::runtime::Runtime::new().unwrap().block_on(async {
|
||||
|
||||
// let mut req = ReqProps::new("")
|
||||
// .set_config(&config)
|
||||
// .get_properties(vec![Props::CreationDate, Props::LastModified]);
|
||||
// req.send().await;
|
||||
// });
|
||||
let mut req = ReqProps::new("")
|
||||
.set_config(&config)
|
||||
.get_properties(vec![Props::CreationDate, Props::LastModified]);
|
||||
req.send().await;
|
||||
});
|
||||
// dbg!(req);
|
||||
}
|
||||
|
@ -7,8 +7,7 @@ mod subcommands;
|
||||
mod utils;
|
||||
mod services;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
fn main() {
|
||||
let app = Command::new("Nextsync")
|
||||
.version("1.0")
|
||||
.author("grimhilt")
|
||||
@ -33,7 +32,7 @@ async fn main() {
|
||||
Some(("reset", args)) => subcommands::reset::handler(args),
|
||||
Some(("push", args)) => subcommands::push::handler(args),
|
||||
Some(("test", args)) => subcommands::test::handler(args),
|
||||
Some(("clone", args)) => subcommands::clone::handler(args).await,
|
||||
Some(("clone", args)) => subcommands::clone::handler(args),
|
||||
Some((_, _)) => {}
|
||||
None => {}
|
||||
};
|
||||
|
@ -1,4 +1,2 @@
|
||||
pub mod downloader;
|
||||
pub mod enumerator;
|
||||
pub mod req_props;
|
||||
pub mod service;
|
||||
pub mod req_props;
|
||||
|
@ -1,91 +0,0 @@
|
||||
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::{Request, Service};
|
||||
use crate::services::service::Service;
|
||||
use crate::store::object::Obj;
|
||||
use crate::config::config::Config;
|
||||
use serde::Deserialize;
|
||||
use serde_xml_rs::from_str;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum Props {
|
||||
CreationDate,
|
||||
LastModified,
|
||||
@ -25,6 +25,7 @@ pub enum Props {
|
||||
ContainedFileCountm,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct Propstat {
|
||||
#[serde(rename = "prop")]
|
||||
@ -40,24 +41,19 @@ struct Prop {
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct Response {
|
||||
struct Response {
|
||||
#[serde(rename = "href")]
|
||||
pub href: String,
|
||||
href: String,
|
||||
#[serde(rename = "propstat")]
|
||||
propstat: Propstat,
|
||||
}
|
||||
|
||||
impl Response {
|
||||
pub fn is_dir(&self) -> bool {
|
||||
todo!("is dir reponse")
|
||||
}
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct Multistatus {
|
||||
#[serde(rename = "response")]
|
||||
responses: Vec<Response>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct Multistatus {
|
||||
#[serde(rename = "response")]
|
||||
pub responses: Vec<Response>,
|
||||
}
|
||||
|
||||
impl From<&Props> for &str {
|
||||
fn from(variant: &Props) -> Self {
|
||||
@ -69,21 +65,23 @@ impl From<&Props> for &str {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ReqProps<'a> {
|
||||
request: Request<'a>,
|
||||
pub struct ReqProps {
|
||||
service: Service,
|
||||
properties: Vec<Props>,
|
||||
}
|
||||
|
||||
impl<'a> ReqProps<'a> {
|
||||
pub fn new(service: &'a Service) -> Self {
|
||||
impl ReqProps {
|
||||
pub fn new(path: &str) -> Self {
|
||||
let mut service = Service::new();
|
||||
service.propfind(path.to_owned());
|
||||
ReqProps {
|
||||
request: Request::new(service),
|
||||
service,
|
||||
properties: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_path(mut self, path: String) -> Self {
|
||||
self.request.propfind(path);
|
||||
pub fn set_config(mut self, config: &Config) -> Self {
|
||||
self.service.set_config(config.clone());
|
||||
self
|
||||
}
|
||||
|
||||
@ -107,16 +105,23 @@ impl<'a> ReqProps<'a> {
|
||||
xml
|
||||
}
|
||||
|
||||
pub async fn send(&mut self) -> Result<Multistatus, reqwest::Error> {
|
||||
let res = self.request.send().await;
|
||||
let xml = res.unwrap().text().await?;
|
||||
pub async fn send(&mut self) {
|
||||
dbg!("send");
|
||||
let res = self.service.send().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();
|
||||
Ok(multistatus)
|
||||
dbg!(multistatus);
|
||||
}
|
||||
}
|
||||
|
||||
// impl From<Obj> for ReqProps<'_> {
|
||||
// fn from(obj: Obj) -> Self {
|
||||
// ReqProps::new(obj.get_obj_path().to_str().unwrap())
|
||||
// }
|
||||
// }
|
||||
impl From<Obj> for ReqProps {
|
||||
fn from(obj: Obj) -> Self {
|
||||
ReqProps::new(obj.get_obj_path().to_str().unwrap())
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
use crate::commands::clone::UrlProps;
|
||||
use reqwest::{header::HeaderMap, Method, Url};
|
||||
use crate::config::config::Config;
|
||||
use reqwest::{Client, ClientBuilder};
|
||||
use reqwest::{header::HeaderMap, Method, Url};
|
||||
|
||||
const USER_AGENT: &str = "Nextsync";
|
||||
|
||||
@ -34,39 +34,20 @@ impl ClientConfig {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
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,
|
||||
config: Option<Config>,
|
||||
method: Option<Method>,
|
||||
url: Option<String>,
|
||||
headers: HeaderMap,
|
||||
body: Option<String>,
|
||||
}
|
||||
|
||||
impl<'a> Request<'a> {
|
||||
pub fn new(service: &'a Service) -> Self {
|
||||
Request {
|
||||
service,
|
||||
impl Service {
|
||||
pub fn new() -> Self {
|
||||
Service {
|
||||
client: ClientConfig::new(),
|
||||
config: None,
|
||||
method: None,
|
||||
url: None,
|
||||
headers: HeaderMap::new(),
|
||||
@ -74,6 +55,10 @@ impl<'a> Request<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_config(&mut self, config: Config) {
|
||||
self.config = Some(config);
|
||||
}
|
||||
|
||||
pub fn propfind(&mut self, url: String) -> &mut Self {
|
||||
self.method = Some(Method::from_bytes(b"PROPFIND").expect("Cannot be invalid"));
|
||||
self.url = Some(url);
|
||||
@ -81,22 +66,21 @@ impl<'a> Request<'a> {
|
||||
}
|
||||
|
||||
pub async fn send(&mut self) -> Result<reqwest::Response, reqwest::Error> {
|
||||
todo!()
|
||||
// let mut url = self
|
||||
// .config
|
||||
// .clone()
|
||||
// .expect("A config must be provided to service")
|
||||
// .get_nsconfig()
|
||||
// .get_remote("origin")
|
||||
// .url
|
||||
// .expect("An url must be set on the remote");
|
||||
// url.push_str(&self.url.clone().unwrap());
|
||||
let mut url = self
|
||||
.config
|
||||
.clone()
|
||||
.expect("A config must be provided to service")
|
||||
.get_nsconfig()
|
||||
.get_remote("origin")
|
||||
.url
|
||||
.expect("An url must be set on the remote");
|
||||
url.push_str(&self.url.clone().unwrap());
|
||||
|
||||
// self.client
|
||||
// .build()
|
||||
// .request(self.method.clone().expect("Method must be set"), url)
|
||||
// .bearer_auth("rK5ud2NmrR8p586Th7v272HRgUcZcEKIEluOGjzQQRj7gWMMAISFTiJcFnnmnNiu2VVlENks")
|
||||
// .send()
|
||||
// .await
|
||||
self.client
|
||||
.build()
|
||||
.request(self.method.clone().expect("Method must be set"), url)
|
||||
.bearer_auth("rK5ud2NmrR8p586Th7v272HRgUcZcEKIEluOGjzQQRj7gWMMAISFTiJcFnnmnNiu2VVlENks")
|
||||
.send()
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
@ -1,16 +1,18 @@
|
||||
use clap::{Arg, ArgMatches, Command};
|
||||
use clap::{Arg, Command, ArgMatches};
|
||||
|
||||
use crate::commands;
|
||||
use crate::config::config::Config;
|
||||
|
||||
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")
|
||||
.arg(
|
||||
Arg::new("remote")
|
||||
.required(true)
|
||||
.num_args(1)
|
||||
.value_name("REMOTE")
|
||||
.help("The repository to clone from. See the NEXTSYNC URLS section below for more information on specifying repositories.")
|
||||
//.help(_desc)
|
||||
)
|
||||
.arg(
|
||||
Arg::new("depth")
|
||||
@ -18,13 +20,7 @@ pub fn create() -> Command {
|
||||
.long("depth")
|
||||
.required(false)
|
||||
.num_args(1)
|
||||
.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)")
|
||||
//.help(&depth_desc)
|
||||
)
|
||||
.arg(
|
||||
Arg::new("directory")
|
||||
@ -33,25 +29,17 @@ pub fn create() -> Command {
|
||||
.value_name("DIRECTORY")
|
||||
)
|
||||
.about("Clone a repository into a new directory")
|
||||
.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
|
||||
")
|
||||
.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")
|
||||
}
|
||||
|
||||
pub async fn handler(args: &ArgMatches) {
|
||||
pub 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") {
|
||||
commands::clone::exec(
|
||||
commands::clone::CloneArgs {
|
||||
commands::clone::exec(commands::clone::CloneArgs {
|
||||
remote: remote.to_string(),
|
||||
depth: args.get_one::<String>("depth").cloned(),
|
||||
force_insecure: args.contains_id("force_insecure"),
|
||||
},
|
||||
Config::from(args.get_one::<String>("directory")),
|
||||
)
|
||||
.await;
|
||||
}, Config::new());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user