Compare commits
9 Commits
61531f664b
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
09f1b26a50 | ||
|
|
3853d1fe27 | ||
|
|
d0f2fafbbb | ||
|
|
487a94f829 | ||
|
|
6989e87a56 | ||
|
|
33d35aba49 | ||
|
|
adac2e6cda | ||
|
|
d442bf689c | ||
|
|
3af7a00b0f |
@@ -4,3 +4,4 @@ pub mod status;
|
||||
pub mod reset;
|
||||
pub mod push;
|
||||
pub mod test;
|
||||
pub mod clone;
|
||||
|
||||
190
src/commands/clone.rs
Normal file
190
src/commands/clone.rs
Normal file
@@ -0,0 +1,190 @@
|
||||
use super::init;
|
||||
use crate::config::config::Config;
|
||||
use crate::services::{downloader::Downloader, enumerator::Enumerator, service::Service};
|
||||
use crate::store::{
|
||||
nsobject::NsObject,
|
||||
structs,
|
||||
};
|
||||
use regex::Regex;
|
||||
use std::fs;
|
||||
use std::io::{self, BufRead};
|
||||
|
||||
pub struct CloneArgs {
|
||||
pub remote: String,
|
||||
pub depth: Option<String>,
|
||||
pub force_insecure: bool,
|
||||
}
|
||||
|
||||
pub async fn exec(args: CloneArgs, config: Config) {
|
||||
init::init(&config);
|
||||
structs::init(config.get_root_unsafe());
|
||||
|
||||
let mut url_props = get_url_props(&args.remote);
|
||||
|
||||
if args.force_insecure {
|
||||
url_props.is_secure = false;
|
||||
}
|
||||
|
||||
// Ask for webdav user
|
||||
if url_props.user == String::new() {
|
||||
println!("Please enter the username of the webdav instance: ");
|
||||
let stdin = io::stdin();
|
||||
url_props.user = stdin.lock().lines().next().unwrap().unwrap();
|
||||
}
|
||||
|
||||
let service = Service::from(&url_props);
|
||||
let Ok((files, folders)) = Enumerator::new(&service)
|
||||
.set_path(String::new()) // use base_path of the service
|
||||
// .set_depth(args.depth)
|
||||
.get_properties(vec![])
|
||||
.enumerate()
|
||||
.await
|
||||
else {
|
||||
todo!("Enumerator has failed")
|
||||
};
|
||||
dbg!(&files);
|
||||
dbg!(&folders);
|
||||
|
||||
for folder in folders {
|
||||
if folder.abs_path() == "" {
|
||||
continue;
|
||||
}
|
||||
fs::create_dir(folder.obj_path()).unwrap();
|
||||
NsObject::from_local_path(&folder.obj_path())
|
||||
.save()
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
let downloader = Downloader::new(&service)
|
||||
.set_files(
|
||||
files
|
||||
.into_iter()
|
||||
.map(|file| file.abs_path().to_string())
|
||||
.collect(),
|
||||
)
|
||||
.download()
|
||||
.await;
|
||||
}
|
||||
|
||||
pub struct UrlProps<'a> {
|
||||
pub is_secure: bool,
|
||||
pub domain: &'a str,
|
||||
pub path: &'a str,
|
||||
pub user: String,
|
||||
}
|
||||
|
||||
impl UrlProps<'_> {
|
||||
fn new() -> Self {
|
||||
UrlProps {
|
||||
is_secure: true,
|
||||
domain: "",
|
||||
path: "",
|
||||
user: String::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Generate the url with all the informations of the instance
|
||||
pub fn full_url(&self) -> String {
|
||||
format!(
|
||||
"{}@{}{}{}",
|
||||
self.user,
|
||||
match self.is_secure {
|
||||
true => "https://",
|
||||
false => "http://",
|
||||
},
|
||||
self.domain,
|
||||
self.path
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_url_props(url: &str) -> UrlProps {
|
||||
let mut url_props = UrlProps::new();
|
||||
|
||||
// Match protocol and domain
|
||||
let re = Regex::new(r"((?<protocol>https?)://)?(?<domain>[^/]*)").unwrap();
|
||||
let captures = re.captures(url).expect("fatal: invalid url");
|
||||
|
||||
// Assume is secure
|
||||
let protocol = captures.name("protocol").map_or("https", |m| m.as_str());
|
||||
|
||||
url_props.is_secure = protocol == "https";
|
||||
url_props.domain = captures
|
||||
.name("domain")
|
||||
.map(|m| m.as_str())
|
||||
.expect("fatal: domain not found");
|
||||
|
||||
// Get rest of url
|
||||
let end_of_domain_idx = captures
|
||||
.name("domain")
|
||||
.expect("Already unwraped before")
|
||||
.end();
|
||||
let rest_of_url = &url[end_of_domain_idx..];
|
||||
|
||||
// Try webdav url
|
||||
if let Some(rest_of_url) = rest_of_url.strip_prefix("/remote.php/dav/files") {
|
||||
let re = Regex::new(r"[^\/]*(?<path>.*)").unwrap();
|
||||
url_props.path = re
|
||||
.captures(rest_of_url)
|
||||
.expect("fatal: invalid webdav url")
|
||||
.name("path")
|
||||
.map_or("/", |m| m.as_str());
|
||||
return url_props;
|
||||
}
|
||||
|
||||
// Try 'dir' argument
|
||||
let re = Regex::new(r"\?dir=(?<path>[^&]*)").unwrap();
|
||||
if let Some(captures) = re.captures(rest_of_url) {
|
||||
url_props.path = captures.name("path").map_or("/", |m| m.as_str());
|
||||
return url_props;
|
||||
}
|
||||
|
||||
// Try path next to domain
|
||||
if rest_of_url.chars().nth(0).expect("fatal: invalid url") == '/' {
|
||||
url_props.path = rest_of_url;
|
||||
return url_props;
|
||||
}
|
||||
|
||||
panic!("fatal: invalid url (cannot found path)");
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
const DOMAIN: &str = "nextcloud.com";
|
||||
const SUBDOMAIN: &str = "nextcloud.example.com";
|
||||
|
||||
fn compare_url_props(url_to_test: &str, is_secure: bool, domain: &str) {
|
||||
let path = "/foo/bar";
|
||||
let url_props = get_url_props(url_to_test);
|
||||
assert_eq!(url_props.is_secure, is_secure);
|
||||
assert_eq!(url_props.domain, domain);
|
||||
assert_eq!(url_props.path, path);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_url_props_from_browser_test() {
|
||||
compare_url_props(
|
||||
"https://nextcloud.com/apps/files/?dir=/foo/bar&fileid=166666",
|
||||
true,
|
||||
DOMAIN,
|
||||
);
|
||||
compare_url_props(
|
||||
"https://nextcloud.com/apps/files/files/625?dir=/foo/bar",
|
||||
true,
|
||||
DOMAIN,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_url_props_direct_url_test() {
|
||||
compare_url_props("https://nextcloud.example.com/foo/bar", true, SUBDOMAIN);
|
||||
compare_url_props("http://nextcloud.example.com/foo/bar", false, SUBDOMAIN);
|
||||
compare_url_props("nextcloud.example.com/foo/bar", true, SUBDOMAIN);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_url_props_with_port_test() {
|
||||
compare_url_props("localhost:8080/foo/bar", true, "localhost:8080");
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,10 @@ pub struct InitArgs {}
|
||||
///
|
||||
/// This function will panic if it cannot create the mentioned files or directories.
|
||||
pub fn exec(_: InitArgs, config: Config) {
|
||||
init(&config);
|
||||
}
|
||||
|
||||
pub fn init(config: &Config) {
|
||||
let mut path: PathBuf = config.execution_path.clone();
|
||||
path.push(".nextsync");
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use crate::config::config::Config;
|
||||
use crate::services::req_props::{Props, ReqProps};
|
||||
use crate::store::object::Obj;
|
||||
use crate::store::{indexer::Indexer, structs};
|
||||
// use crate::services::req_props::{Props, ReqProps};
|
||||
// use crate::store::object::Obj;
|
||||
// use crate::store::{indexer::Indexer, structs};
|
||||
|
||||
pub struct TestArgs {}
|
||||
|
||||
@@ -18,9 +18,12 @@ pub fn exec(args: TestArgs, config: Config) {
|
||||
// println!("{:?}", config);
|
||||
|
||||
// Ok(())
|
||||
let req = ReqProps::new("")
|
||||
.set_config(&config)
|
||||
.get_properties(vec![Props::CreationDate, Props::GetLastModified])
|
||||
.send();
|
||||
dbg!(req);
|
||||
// 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;
|
||||
// });
|
||||
// dbg!(req);
|
||||
}
|
||||
|
||||
@@ -7,7 +7,8 @@ mod subcommands;
|
||||
mod utils;
|
||||
mod services;
|
||||
|
||||
fn main() {
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let app = Command::new("Nextsync")
|
||||
.version("1.0")
|
||||
.author("grimhilt")
|
||||
@@ -19,6 +20,7 @@ fn main() {
|
||||
subcommands::reset::create(),
|
||||
subcommands::push::create(),
|
||||
subcommands::test::create(),
|
||||
subcommands::clone::create(),
|
||||
]);
|
||||
// .setting(clap::AppSettings::SubcommandRequiredElseHelp);
|
||||
|
||||
@@ -31,6 +33,7 @@ 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((_, _)) => {}
|
||||
None => {}
|
||||
};
|
||||
|
||||
@@ -1,2 +1,5 @@
|
||||
pub mod service;
|
||||
pub mod downloader;
|
||||
pub mod download;
|
||||
pub mod enumerator;
|
||||
pub mod req_props;
|
||||
pub mod service;
|
||||
|
||||
42
src/services/download.rs
Normal file
42
src/services/download.rs
Normal file
@@ -0,0 +1,42 @@
|
||||
use crate::services::service::{Request, Service};
|
||||
use crate::store::nsobject::NsObject;
|
||||
use std::{fs::OpenOptions, io::Write};
|
||||
|
||||
pub struct Download<'a> {
|
||||
request: Request<'a>,
|
||||
obj_path: String,
|
||||
}
|
||||
|
||||
impl<'a> Download<'a> {
|
||||
pub fn new(service: &'a Service) -> Self {
|
||||
Download {
|
||||
request: Request::new(service),
|
||||
obj_path: String::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_obj_path(mut self, obj_path: String) -> Self {
|
||||
self.request.get(obj_path.clone());
|
||||
self.obj_path = obj_path
|
||||
.strip_prefix("/")
|
||||
.unwrap_or(&self.obj_path.clone())
|
||||
.to_string();
|
||||
self
|
||||
}
|
||||
|
||||
pub async fn send(&mut self) -> Result<(), reqwest::Error> {
|
||||
let res = self.request.send().await;
|
||||
let body = res.unwrap().bytes().await?;
|
||||
let mut file = OpenOptions::new()
|
||||
.write(true)
|
||||
.create(true)
|
||||
.open(self.obj_path.clone())
|
||||
.unwrap();
|
||||
|
||||
file.write_all(&body.to_vec()).unwrap();
|
||||
NsObject::from_local_path(&self.obj_path.clone().into())
|
||||
.save()
|
||||
.unwrap();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
26
src/services/downloader.rs
Normal file
26
src/services/downloader.rs
Normal file
@@ -0,0 +1,26 @@
|
||||
use super::{download::Download, service::Service};
|
||||
|
||||
pub struct Downloader<'a> {
|
||||
service: &'a Service,
|
||||
files: Vec<String>,
|
||||
}
|
||||
|
||||
impl<'a> Downloader<'a> {
|
||||
pub fn new(service: &'a Service) -> Self {
|
||||
Downloader {
|
||||
service,
|
||||
files: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_files(mut self, files: Vec<String>) -> Self {
|
||||
self.files = files;
|
||||
self
|
||||
}
|
||||
|
||||
pub async fn download(&self) {
|
||||
for file in self.files.clone() {
|
||||
Download::new(self.service).set_obj_path(file).send().await;
|
||||
}
|
||||
}
|
||||
}
|
||||
103
src/services/enumerator.rs
Normal file
103
src/services/enumerator.rs
Normal file
@@ -0,0 +1,103 @@
|
||||
use super::{
|
||||
req_props::{Props, ReqProps, Response},
|
||||
service::Service,
|
||||
};
|
||||
use crate::utils::path;
|
||||
use std::sync::Arc;
|
||||
use tokio::{sync::Mutex, task::JoinSet};
|
||||
|
||||
pub const DEFAULT_DEPTH: u16 = 3;
|
||||
|
||||
pub struct Enumerator<'a> {
|
||||
service: &'a Service,
|
||||
path: String,
|
||||
depth: u16,
|
||||
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: u16) -> 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());
|
||||
let tasks_active = Arc::new(Mutex::new(0));
|
||||
|
||||
tx.send(self.path.clone());
|
||||
|
||||
let mut taskSet = JoinSet::new();
|
||||
|
||||
loop {
|
||||
if let Ok(path) = rx.try_recv() {
|
||||
let current_depth = path::get_depth(&path);
|
||||
*tasks_active.lock().await += 1;
|
||||
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 depth = self.depth.clone();
|
||||
let tasks_active_clone = Arc::clone(&tasks_active);
|
||||
|
||||
let task = taskSet.spawn(async move {
|
||||
let res = ReqProps::new(&service_clone)
|
||||
.set_path(path.clone())
|
||||
.set_depth(depth)
|
||||
.get_properties(properties)
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
for obj in res.responses {
|
||||
if obj.is_dir() {
|
||||
// Avoid enumerating the same folder multiple times
|
||||
if obj.abs_path() != &path {
|
||||
// depth deeper than current + self.depth
|
||||
if obj.path_depth() >= current_depth + depth {
|
||||
tx_clone.send(obj.abs_path().to_owned()).unwrap();
|
||||
}
|
||||
folders_clone.lock().await.push(obj);
|
||||
}
|
||||
} else {
|
||||
files_clone.lock().await.push(obj);
|
||||
}
|
||||
}
|
||||
*tasks_active_clone.lock().await -= 1;
|
||||
});
|
||||
} else if *tasks_active.lock().await <= 0 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
taskSet.join_all().await;
|
||||
|
||||
Ok((
|
||||
Arc::try_unwrap(files).unwrap().into_inner(),
|
||||
Arc::try_unwrap(folders).unwrap().into_inner(),
|
||||
))
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,19 @@
|
||||
use crate::services::service::Service;
|
||||
use crate::store::object::Obj;
|
||||
use crate::config::config::Config;
|
||||
use crate::services::service::{Request, Service};
|
||||
use crate::store::structs::{to_obj_path, ObjPath};
|
||||
use crate::utils::path;
|
||||
use serde::Deserialize;
|
||||
use serde_xml_rs::from_str;
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum Props {
|
||||
CreationDate,
|
||||
GetLastModified,
|
||||
GetETag,
|
||||
GetContentType,
|
||||
LastModified,
|
||||
ETag,
|
||||
ContentType,
|
||||
RessourceType,
|
||||
GetContentLength,
|
||||
GetContentLanguage,
|
||||
ContentLength,
|
||||
ContentLanguage,
|
||||
DisplayName,
|
||||
FileId,
|
||||
Permissions,
|
||||
@@ -23,33 +27,101 @@ pub enum Props {
|
||||
ContainedFileCountm,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct Propstat {
|
||||
#[serde(rename = "prop")]
|
||||
prop: Prop,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct Prop {
|
||||
#[serde(rename = "getlastmodified")]
|
||||
last_modified: Option<String>,
|
||||
#[serde(rename = "getcontentlength")]
|
||||
content_length: Option<u64>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct Response {
|
||||
#[serde(rename = "href")]
|
||||
pub href: String,
|
||||
#[serde(rename = "propstat")]
|
||||
propstat: Propstat,
|
||||
href_prefix: Option<String>,
|
||||
}
|
||||
|
||||
impl Response {
|
||||
pub fn is_dir(&self) -> bool {
|
||||
self.href.ends_with("/")
|
||||
}
|
||||
|
||||
pub fn abs_path(&self) -> &str {
|
||||
dbg!(self.href_prefix.clone());
|
||||
let path = self
|
||||
.href
|
||||
.strip_prefix(&format!(
|
||||
"/remote.php/dav/files/{}",
|
||||
self.href_prefix.clone().unwrap_or_default()
|
||||
))
|
||||
.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 obj_path(&self) -> ObjPath {
|
||||
let mut path = self.abs_path();
|
||||
path = path.strip_prefix("/").unwrap();
|
||||
to_obj_path(&PathBuf::from(path))
|
||||
}
|
||||
|
||||
pub fn path_depth(&self) -> u16 {
|
||||
path::get_depth(self.abs_path())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct Multistatus {
|
||||
#[serde(rename = "response")]
|
||||
pub responses: Vec<Response>,
|
||||
}
|
||||
|
||||
impl From<&Props> for &str {
|
||||
fn from(variant: &Props) -> Self {
|
||||
match variant {
|
||||
Props::CreationDate => "<d:creationdate />",
|
||||
Props::GetLastModified => "<d:getlastmodified />",
|
||||
Props::LastModified => "<d:getlastmodified />",
|
||||
_ => todo!("Props conversion not implemented"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ReqProps {
|
||||
service: Service,
|
||||
pub struct ReqProps<'a> {
|
||||
request: Request<'a>,
|
||||
properties: Vec<Props>,
|
||||
}
|
||||
|
||||
impl ReqProps {
|
||||
pub fn new(path: &str) -> Self {
|
||||
let mut service = Service::new();
|
||||
service.propfind(path.to_owned());
|
||||
impl<'a> ReqProps<'a> {
|
||||
pub fn new(service: &'a Service) -> Self {
|
||||
ReqProps {
|
||||
service,
|
||||
request: Request::new(service),
|
||||
properties: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_config(mut self, config: &Config) -> Self {
|
||||
self.service.set_config(config.clone());
|
||||
pub fn set_path(mut self, path: String) -> Self {
|
||||
self.request.propfind(path);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn set_depth(mut self, depth: u16) -> Self {
|
||||
self.request.headers.insert("Depth", depth.into());
|
||||
self
|
||||
}
|
||||
|
||||
@@ -73,13 +145,22 @@ impl ReqProps {
|
||||
xml
|
||||
}
|
||||
|
||||
pub fn send(&mut self) {
|
||||
self.service.send();
|
||||
pub async fn send(&mut self) -> Result<Multistatus, reqwest::Error> {
|
||||
let res = self.request.send().await;
|
||||
let xml = res.unwrap().text().await?;
|
||||
dbg!(&xml);
|
||||
let mut multistatus: Multistatus =
|
||||
from_str(&xml).expect("Failed to unwrap xml response from req_props");
|
||||
multistatus
|
||||
.responses
|
||||
.iter_mut()
|
||||
.for_each(|res| res.href_prefix = Some(self.request.service.href_prefix()));
|
||||
Ok(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::config::config::Config;
|
||||
use reqwest::blocking::{Client, ClientBuilder};
|
||||
use reqwest::{header::HeaderMap, Method, Url};
|
||||
use crate::commands::clone::UrlProps;
|
||||
use reqwest::{header::HeaderMap, Method};
|
||||
use reqwest::{Client, ClientBuilder, RequestBuilder};
|
||||
|
||||
const USER_AGENT: &str = "Nextsync";
|
||||
|
||||
@@ -34,20 +34,70 @@ impl ClientConfig {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Service {
|
||||
client: ClientConfig,
|
||||
config: Option<Config>,
|
||||
method: Option<Method>,
|
||||
url: Option<String>,
|
||||
headers: HeaderMap,
|
||||
body: Option<String>,
|
||||
/// http[s]://host.xz/remote.php/dav/files
|
||||
url_base: String,
|
||||
base_path: String,
|
||||
user: String,
|
||||
}
|
||||
|
||||
impl From<&UrlProps<'_>> for Service {
|
||||
fn from(url_props: &UrlProps) -> Self {
|
||||
let mut url_base = if url_props.is_secure {
|
||||
String::from("https://")
|
||||
} else {
|
||||
String::from("http://")
|
||||
};
|
||||
url_base.push_str(url_props.domain);
|
||||
url_base.push_str("/remote.php/dav/files");
|
||||
|
||||
Service {
|
||||
url_base,
|
||||
base_path: url_props.path.to_string(),
|
||||
user: url_props.user.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Service {
|
||||
pub fn new() -> Self {
|
||||
Service {
|
||||
fn authenticate(&self, request: RequestBuilder) -> RequestBuilder {
|
||||
request
|
||||
.bearer_auth("rK5ud2NmrR8p586Th7v272HRgUcZcEKIEluOGjzQQRj7gWMMAISFTiJcFnnmnNiu2VVlENks")
|
||||
}
|
||||
|
||||
fn build_url(&self, url: &str) -> String {
|
||||
let mut final_url = self.url_base.clone();
|
||||
final_url.push_str("/");
|
||||
final_url.push_str(&self.user);
|
||||
final_url.push_str(&self.base_path);
|
||||
final_url.push_str(url);
|
||||
final_url
|
||||
}
|
||||
|
||||
/// Return the prefix of a href
|
||||
/// /user/base_path/path -> /user/base_path
|
||||
pub fn href_prefix(&self) -> String {
|
||||
let mut prefix = self.user.clone();
|
||||
prefix.push_str(&self.base_path);
|
||||
prefix
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Request<'a> {
|
||||
pub service: &'a Service,
|
||||
client: ClientConfig,
|
||||
method: Option<Method>,
|
||||
url: Option<String>,
|
||||
pub headers: HeaderMap,
|
||||
body: Option<String>,
|
||||
}
|
||||
|
||||
impl<'a> Request<'a> {
|
||||
pub fn new(service: &'a Service) -> Self {
|
||||
Request {
|
||||
service,
|
||||
client: ClientConfig::new(),
|
||||
config: None,
|
||||
method: None,
|
||||
url: None,
|
||||
headers: HeaderMap::new(),
|
||||
@@ -55,31 +105,47 @@ impl Service {
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn send(&mut self) {
|
||||
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());
|
||||
pub fn get(&mut self, url: String) -> &mut Self {
|
||||
self.method = Some(Method::GET);
|
||||
self.url = Some(url);
|
||||
self
|
||||
}
|
||||
|
||||
dbg!(self
|
||||
.client
|
||||
pub async fn send(&mut self) -> Result<reqwest::Response, reqwest::Error> {
|
||||
dbg!(self.service.build_url(&self.url.clone().expect("An url must be set")));
|
||||
self.service
|
||||
.authenticate(
|
||||
self.client
|
||||
.build()
|
||||
.request(self.method.clone().expect("Method must be set"), url,)
|
||||
.bearer_auth("rK5ud2NmrR8p586Th7v272HRgUcZcEKIEluOGjzQQRj7gWMMAISFTiJcFnnmnNiu2VVlENks")
|
||||
.send());
|
||||
.request(
|
||||
self.method.clone().expect("Method must be set"),
|
||||
self.service
|
||||
.build_url(&self.url.clone().expect("An url must be set")),
|
||||
)
|
||||
.headers(self.headers.clone()),
|
||||
)
|
||||
.send()
|
||||
.await
|
||||
// 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)
|
||||
// .send()
|
||||
// .await
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,10 @@ use crate::store::{
|
||||
object::{Obj, ObjMetadata, ObjType},
|
||||
structs::{NsObjPath, ObjPath},
|
||||
};
|
||||
use std::fs::{self, File, OpenOptions};
|
||||
use std::io::{self, Write};
|
||||
use std::sync::OnceLock;
|
||||
use std::time::UNIX_EPOCH;
|
||||
|
||||
type NsObjectChilds = Vec<Box<NsObject>>;
|
||||
|
||||
@@ -81,12 +84,96 @@ impl NsObject {
|
||||
/// * if it is a Tree obj after an empty line there will be the definition
|
||||
/// of its subobjs (one line by subobj) *
|
||||
/// obj_type + hash
|
||||
pub fn save(&self) -> Result<(), ()> {
|
||||
pub fn save(&self) -> io::Result<()> {
|
||||
if !self.get_obj_path().exists() {
|
||||
self.delete_nsobj();
|
||||
} else {
|
||||
dbg!(self.get_nsobj_path());
|
||||
if self.get_nsobj_path().exists() {
|
||||
self.edit_nsobj();
|
||||
} else {
|
||||
self.create_nsobj()?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn delete_nsobj(&self) {
|
||||
todo!("Delete nsobj");
|
||||
// delete current obj
|
||||
// delete reference on parent
|
||||
} else {
|
||||
}
|
||||
|
||||
fn create_nsobj(&self) -> io::Result<()> {
|
||||
let nsobj_path = self.get_nsobj_path();
|
||||
let mut file = {
|
||||
let mut nsobj_dir = nsobj_path.clone();
|
||||
nsobj_dir.pop();
|
||||
if !nsobj_dir.exists() {
|
||||
std::fs::create_dir_all(nsobj_dir)?;
|
||||
}
|
||||
File::create(&nsobj_path)?
|
||||
};
|
||||
|
||||
// Write type
|
||||
file.write(&[self.obj_type.clone().into()])?;
|
||||
|
||||
if self.obj_type == ObjType::Blob {
|
||||
if let Some(metadata) = self.get_metadata() {
|
||||
// Write size
|
||||
file.write(&metadata.size.to_le_bytes())?;
|
||||
|
||||
// Write modified
|
||||
file.write(
|
||||
&metadata
|
||||
.modified
|
||||
.expect(&format!(
|
||||
"Expect 'modified' in metadata of {} to save obj",
|
||||
self.get_obj_path().as_path().display()
|
||||
))
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_secs()
|
||||
.to_le_bytes(),
|
||||
)?;
|
||||
} else {
|
||||
todo!("Cannot load metadata")
|
||||
}
|
||||
}
|
||||
|
||||
file.write_all(b"\n")?;
|
||||
// Write Path
|
||||
file.write_all(self.get_obj_path().to_str().unwrap().as_bytes())?;
|
||||
file.write_all(b"\n")?;
|
||||
|
||||
file.flush()?;
|
||||
|
||||
// Save itself inside its parent
|
||||
let mut parent_path = self.get_obj_path().clone();
|
||||
parent_path.pop();
|
||||
let parent_obj = NsObject::from_local_path(&parent_path);
|
||||
parent_obj.add_child(&self)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn edit_nsobj(&self) {
|
||||
todo!("Edit nsobj");
|
||||
}
|
||||
|
||||
|
||||
fn add_child(&self, child: &NsObject) -> io::Result<()> {
|
||||
let mut file = OpenOptions::new()
|
||||
.write(true)
|
||||
.create(true)
|
||||
.append(true)
|
||||
.open(self.get_nsobj_path())?;
|
||||
|
||||
let child_type = &[child.obj_type.clone().into()];
|
||||
let child_path = child.get_obj_path().to_str().unwrap().as_bytes();
|
||||
file.write(child_type)?;
|
||||
file.write(child_path)?;
|
||||
file.write(b"\n")?;
|
||||
file.flush()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -3,18 +3,18 @@ use crate::store::{
|
||||
structs::{NsObjPath, ObjPath},
|
||||
};
|
||||
use crate::utils::path;
|
||||
use std::env;
|
||||
use std::fs::{self, File, OpenOptions};
|
||||
use std::io::{self, Write};
|
||||
use std::path::PathBuf;
|
||||
use std::sync::OnceLock;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
use std::{
|
||||
env, fmt, fs, io,
|
||||
path::PathBuf,
|
||||
sync::OnceLock,
|
||||
time::SystemTime,
|
||||
};
|
||||
|
||||
const MAX_SIZE_TO_USE_HASH: u64 = 12 * 1024 * 1024;
|
||||
|
||||
pub struct ObjMetadata {
|
||||
size: u64,
|
||||
modified: Option<SystemTime>,
|
||||
pub size: u64,
|
||||
pub modified: Option<SystemTime>,
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Clone, Debug)]
|
||||
@@ -25,6 +25,12 @@ pub enum ObjType {
|
||||
Tree,
|
||||
}
|
||||
|
||||
impl fmt::Display for ObjType {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{:?}", self)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ObjType> for u8 {
|
||||
fn from(variant: ObjType) -> Self {
|
||||
variant as u8
|
||||
@@ -92,10 +98,12 @@ impl Obj {
|
||||
if !self.obj_path.exists() {
|
||||
return ObjStatus::Deleted;
|
||||
}
|
||||
if self.obj_type != ObjType::Tree && nsobj.obj_type != ObjType::Tree {
|
||||
if *self != nsobj {
|
||||
return ObjStatus::Modified;
|
||||
}
|
||||
todo!("Read status");
|
||||
}
|
||||
ObjStatus::Unchanged
|
||||
})
|
||||
}
|
||||
|
||||
@@ -164,75 +172,7 @@ impl Obj {
|
||||
}
|
||||
|
||||
pub fn save(&self) -> io::Result<()> {
|
||||
let nsobj_path = self.get_nsobj_path();
|
||||
let mut file = {
|
||||
if nsobj_path.exists() {
|
||||
todo!("Edition of object")
|
||||
} else {
|
||||
let mut nsobj_dir = nsobj_path.clone();
|
||||
nsobj_dir.pop();
|
||||
if !nsobj_dir.exists() {
|
||||
std::fs::create_dir_all(nsobj_dir)?;
|
||||
}
|
||||
File::create(&nsobj_path)?
|
||||
}
|
||||
};
|
||||
|
||||
// Write type
|
||||
file.write(&[self.obj_type.clone().into()])?;
|
||||
|
||||
if self.obj_type == ObjType::Blob {
|
||||
if let Some(metadata) = self.get_metadata() {
|
||||
// Write size
|
||||
file.write(&metadata.size.to_le_bytes())?;
|
||||
|
||||
// Write modified
|
||||
file.write(
|
||||
&metadata
|
||||
.modified
|
||||
.expect(&format!(
|
||||
"Expect 'modified' in metadata of {} to save obj",
|
||||
self.cpy_path()
|
||||
))
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_secs()
|
||||
.to_le_bytes(),
|
||||
)?;
|
||||
} else {
|
||||
todo!("Cannot load metadata")
|
||||
}
|
||||
}
|
||||
|
||||
file.write_all(b"\n")?;
|
||||
// Write Path
|
||||
file.write_all(self.obj_path.to_str().unwrap().as_bytes())?;
|
||||
file.write_all(b"\n")?;
|
||||
|
||||
file.flush()?;
|
||||
|
||||
// Save itself inside its parent
|
||||
let mut parent_path = self.obj_path.clone();
|
||||
parent_path.pop();
|
||||
let parent_obj = Obj::from_local_path(&parent_path);
|
||||
parent_obj.add_child(&self)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn add_child(&self, child: &Obj) -> io::Result<()> {
|
||||
let mut file = OpenOptions::new()
|
||||
.write(true)
|
||||
.create(true)
|
||||
.append(true)
|
||||
.open(self.get_nsobj_path())?;
|
||||
|
||||
let child_type = &[child.get_obj_type().clone().into()];
|
||||
let child_path = child.obj_path.to_str().unwrap().as_bytes();
|
||||
file.write(child_type)?;
|
||||
file.write(child_path)?;
|
||||
file.write(b"\n")?;
|
||||
file.flush()?;
|
||||
self.get_nsobj().save()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -259,7 +199,13 @@ impl Obj {
|
||||
impl PartialEq<NsObject> for Obj {
|
||||
fn eq(&self, other: &NsObject) -> bool {
|
||||
if self.obj_type != other.obj_type {
|
||||
eprintln!("Trying to compare different obj type");
|
||||
eprintln!(
|
||||
"{}",
|
||||
format!(
|
||||
"Trying to compare different obj type ({} != {})",
|
||||
self.obj_type, other.obj_type
|
||||
)
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -102,7 +102,16 @@ impl Into<ObjPath> for PathBuf {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
impl Into<ObjPath> for String {
|
||||
fn into(self) -> ObjPath {
|
||||
ObjPath {
|
||||
path: self.into(),
|
||||
abs: OnceLock::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct NsObjPath {
|
||||
path: PathBuf,
|
||||
}
|
||||
|
||||
@@ -4,3 +4,4 @@ pub mod reset;
|
||||
pub mod status;
|
||||
pub mod push;
|
||||
pub mod test;
|
||||
pub mod clone;
|
||||
|
||||
58
src/subcommands/clone.rs
Normal file
58
src/subcommands/clone.rs
Normal file
@@ -0,0 +1,58 @@
|
||||
use clap::{Arg, ArgAction, ArgMatches, Command};
|
||||
|
||||
use crate::commands;
|
||||
use crate::config::config::Config;
|
||||
|
||||
pub fn create() -> Command {
|
||||
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.")
|
||||
)
|
||||
.arg(
|
||||
Arg::new("depth")
|
||||
.short('d')
|
||||
.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")
|
||||
.short('f')
|
||||
.long("force-insecure")
|
||||
.action(ArgAction::SetTrue)
|
||||
.help("Force the connection to nextcloud to be in http (not https)")
|
||||
)
|
||||
.arg(
|
||||
Arg::new("directory")
|
||||
.required(false)
|
||||
.num_args(1)
|
||||
.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
|
||||
")
|
||||
}
|
||||
|
||||
pub async fn handler(args: &ArgMatches) {
|
||||
if let Some(remote) = args.get_one::<String>("remote") {
|
||||
commands::clone::exec(
|
||||
commands::clone::CloneArgs {
|
||||
remote: remote.to_string(),
|
||||
depth: args.get_one::<String>("depth").cloned(),
|
||||
force_insecure: *args.get_one::<bool>("force_insecure").unwrap(),
|
||||
},
|
||||
Config::from(args.get_one::<String>("directory")),
|
||||
)
|
||||
.await;
|
||||
}
|
||||
}
|
||||
@@ -54,3 +54,8 @@ pub fn normalize_path<P: AsRef<Path>>(path: P) -> PathBuf {
|
||||
}
|
||||
normalized
|
||||
}
|
||||
|
||||
/// Calculate depth of a path
|
||||
pub fn get_depth(path: &str) -> u16 {
|
||||
path.split("/").count() as u16
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ fn compare_vect(vec1: Vec<Obj>, vec2: Vec<&str>, config: &Config) {
|
||||
}
|
||||
|
||||
fn status_expected(config: &Config, staged: Vec<&str>, not_staged: Vec<&str>) {
|
||||
let res = get_obj_changes(&DEFAULT_STATUS_ARG, config);
|
||||
let res = get_obj_changes(config);
|
||||
|
||||
assert_eq!(res.staged.len(), staged.len());
|
||||
assert_eq!(res.not_staged.len(), not_staged.len());
|
||||
|
||||
Reference in New Issue
Block a user