feat(service): include user and base_path in the service

This commit is contained in:
grimhilt 2024-10-13 18:11:11 +02:00
parent d0f2fafbbb
commit 3853d1fe27
3 changed files with 60 additions and 17 deletions

View File

@ -26,6 +26,7 @@ pub async fn exec(args: CloneArgs, config: Config) {
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();
@ -40,18 +41,15 @@ pub async fn exec(args: CloneArgs, config: Config) {
.enumerate()
.await
else {
todo!()
todo!("Enumerator has failed")
};
dbg!(&files);
dbg!(&folders);
for folder in folders {
dbg!(folder.abs_path());
// TODO
if folder.abs_path() == "/admin/tmp_test" {
if folder.abs_path() == "" {
continue;
}
dbg!(folder.obj_path());
fs::create_dir(folder.obj_path()).unwrap();
NsObject::from_local_path(&folder.obj_path())
.save()
@ -79,6 +77,20 @@ impl UrlProps<'_> {
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 {

View File

@ -50,6 +50,7 @@ pub struct Response {
pub href: String,
#[serde(rename = "propstat")]
propstat: Propstat,
href_prefix: Option<String>,
}
impl Response {
@ -58,9 +59,13 @@ impl Response {
}
pub fn abs_path(&self) -> &str {
dbg!(self.href_prefix.clone());
let path = self
.href
.strip_prefix("/remote.php/dav/files")
.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
@ -73,10 +78,9 @@ impl Response {
}
}
pub fn obj_path(&self, ) -> ObjPath {
pub fn obj_path(&self) -> ObjPath {
let mut path = self.abs_path();
// TODO
path = path.strip_prefix("/admin/tmp_test/").unwrap();
path = path.strip_prefix("/").unwrap();
to_obj_path(&PathBuf::from(path))
}
@ -147,8 +151,13 @@ impl<'a> ReqProps<'a> {
pub async fn send(&mut self) -> Result<Multistatus, reqwest::Error> {
let res = self.request.send().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");
multistatus
.responses
.iter_mut()
.for_each(|res| res.href_prefix = Some(self.request.service.href_prefix()));
Ok(multistatus)
}
}

View File

@ -38,6 +38,7 @@ impl ClientConfig {
pub struct Service {
/// http[s]://host.xz/remote.php/dav/files
url_base: String,
base_path: String,
user: String,
}
@ -51,7 +52,11 @@ impl From<&UrlProps<'_>> for Service {
url_base.push_str(url_props.domain);
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
.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> {
service: &'a Service,
pub service: &'a Service,
client: ClientConfig,
method: Option<Method>,
url: Option<String>,
@ -96,15 +117,16 @@ impl<'a> Request<'a> {
}
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"), {
let mut url = self.service.url_base.clone();
url.push_str(&self.url.clone().expect("An url must be set"));
url
})
.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()