feat(service): include user and base_path in the service
This commit is contained in:
parent
d0f2fafbbb
commit
3853d1fe27
@ -26,6 +26,7 @@ pub async fn exec(args: CloneArgs, config: Config) {
|
|||||||
url_props.is_secure = false;
|
url_props.is_secure = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ask for webdav user
|
||||||
if url_props.user == String::new() {
|
if url_props.user == String::new() {
|
||||||
println!("Please enter the username of the webdav instance: ");
|
println!("Please enter the username of the webdav instance: ");
|
||||||
let stdin = io::stdin();
|
let stdin = io::stdin();
|
||||||
@ -40,18 +41,15 @@ pub async fn exec(args: CloneArgs, config: Config) {
|
|||||||
.enumerate()
|
.enumerate()
|
||||||
.await
|
.await
|
||||||
else {
|
else {
|
||||||
todo!()
|
todo!("Enumerator has failed")
|
||||||
};
|
};
|
||||||
dbg!(&files);
|
dbg!(&files);
|
||||||
dbg!(&folders);
|
dbg!(&folders);
|
||||||
|
|
||||||
for folder in folders {
|
for folder in folders {
|
||||||
dbg!(folder.abs_path());
|
if folder.abs_path() == "" {
|
||||||
// TODO
|
|
||||||
if folder.abs_path() == "/admin/tmp_test" {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
dbg!(folder.obj_path());
|
|
||||||
fs::create_dir(folder.obj_path()).unwrap();
|
fs::create_dir(folder.obj_path()).unwrap();
|
||||||
NsObject::from_local_path(&folder.obj_path())
|
NsObject::from_local_path(&folder.obj_path())
|
||||||
.save()
|
.save()
|
||||||
@ -79,6 +77,20 @@ impl UrlProps<'_> {
|
|||||||
user: String::new(),
|
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 {
|
pub fn get_url_props(url: &str) -> UrlProps {
|
||||||
|
@ -50,6 +50,7 @@ pub struct Response {
|
|||||||
pub href: String,
|
pub href: String,
|
||||||
#[serde(rename = "propstat")]
|
#[serde(rename = "propstat")]
|
||||||
propstat: Propstat,
|
propstat: Propstat,
|
||||||
|
href_prefix: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Response {
|
impl Response {
|
||||||
@ -58,9 +59,13 @@ impl Response {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn abs_path(&self) -> &str {
|
pub fn abs_path(&self) -> &str {
|
||||||
|
dbg!(self.href_prefix.clone());
|
||||||
let path = self
|
let path = self
|
||||||
.href
|
.href
|
||||||
.strip_prefix("/remote.php/dav/files")
|
.strip_prefix(&format!(
|
||||||
|
"/remote.php/dav/files/{}",
|
||||||
|
self.href_prefix.clone().unwrap_or_default()
|
||||||
|
))
|
||||||
.expect(&format!(
|
.expect(&format!(
|
||||||
"Unexpected result when requesting props. Cannot strip from {}",
|
"Unexpected result when requesting props. Cannot strip from {}",
|
||||||
self.href
|
self.href
|
||||||
@ -73,10 +78,9 @@ impl Response {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn obj_path(&self, ) -> ObjPath {
|
pub fn obj_path(&self) -> ObjPath {
|
||||||
let mut path = self.abs_path();
|
let mut path = self.abs_path();
|
||||||
// TODO
|
path = path.strip_prefix("/").unwrap();
|
||||||
path = path.strip_prefix("/admin/tmp_test/").unwrap();
|
|
||||||
to_obj_path(&PathBuf::from(path))
|
to_obj_path(&PathBuf::from(path))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -147,8 +151,13 @@ impl<'a> ReqProps<'a> {
|
|||||||
pub async fn send(&mut self) -> Result<Multistatus, reqwest::Error> {
|
pub async fn send(&mut self) -> Result<Multistatus, reqwest::Error> {
|
||||||
let res = self.request.send().await;
|
let res = self.request.send().await;
|
||||||
let xml = res.unwrap().text().await?;
|
let xml = res.unwrap().text().await?;
|
||||||
let multistatus: Multistatus =
|
dbg!(&xml);
|
||||||
|
let mut multistatus: Multistatus =
|
||||||
from_str(&xml).expect("Failed to unwrap xml response from req_props");
|
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)
|
Ok(multistatus)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -38,6 +38,7 @@ impl ClientConfig {
|
|||||||
pub struct Service {
|
pub struct Service {
|
||||||
/// http[s]://host.xz/remote.php/dav/files
|
/// http[s]://host.xz/remote.php/dav/files
|
||||||
url_base: String,
|
url_base: String,
|
||||||
|
base_path: String,
|
||||||
user: String,
|
user: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,7 +52,11 @@ impl From<&UrlProps<'_>> for Service {
|
|||||||
url_base.push_str(url_props.domain);
|
url_base.push_str(url_props.domain);
|
||||||
url_base.push_str("/remote.php/dav/files");
|
url_base.push_str("/remote.php/dav/files");
|
||||||
|
|
||||||
Service { url_base, user: url_props.user.clone() }
|
Service {
|
||||||
|
url_base,
|
||||||
|
base_path: url_props.path.to_string(),
|
||||||
|
user: url_props.user.clone(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,10 +65,26 @@ impl Service {
|
|||||||
request
|
request
|
||||||
.bearer_auth("rK5ud2NmrR8p586Th7v272HRgUcZcEKIEluOGjzQQRj7gWMMAISFTiJcFnnmnNiu2VVlENks")
|
.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(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 struct Request<'a> {
|
||||||
service: &'a Service,
|
pub service: &'a Service,
|
||||||
client: ClientConfig,
|
client: ClientConfig,
|
||||||
method: Option<Method>,
|
method: Option<Method>,
|
||||||
url: Option<String>,
|
url: Option<String>,
|
||||||
@ -96,15 +117,16 @@ 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> {
|
||||||
|
dbg!(self.service.build_url(&self.url.clone().expect("An url must be set")));
|
||||||
self.service
|
self.service
|
||||||
.authenticate(
|
.authenticate(
|
||||||
self.client
|
self.client
|
||||||
.build()
|
.build()
|
||||||
.request(self.method.clone().expect("Method must be set"), {
|
.request(
|
||||||
let mut url = self.service.url_base.clone();
|
self.method.clone().expect("Method must be set"),
|
||||||
url.push_str(&self.url.clone().expect("An url must be set"));
|
self.service
|
||||||
url
|
.build_url(&self.url.clone().expect("An url must be set")),
|
||||||
})
|
)
|
||||||
.headers(self.headers.clone()),
|
.headers(self.headers.clone()),
|
||||||
)
|
)
|
||||||
.send()
|
.send()
|
||||||
|
Loading…
Reference in New Issue
Block a user