From d9e0a55a84edeac23ae4657d09f1d3cef6b119ff Mon Sep 17 00:00:00 2001 From: Adam Cooper Date: Sun, 6 Mar 2022 18:45:16 -0500 Subject: [PATCH] [wip] Not sure exactly what's happening, but the POST requests are returning 200 --- Cargo.lock | 1 + Cargo.toml | 1 + src/main.rs | 47 ++++++++++++++++++++++++++++------------------- 3 files changed, 30 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1ae3c80..b088c82 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -586,6 +586,7 @@ dependencies = [ "log", "reqwest", "tokio", + "url", "yaserde", "yaserde_derive", ] diff --git a/Cargo.toml b/Cargo.toml index b015f68..048e418 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,5 +12,6 @@ env_logger = "0.8.4" log = "0.4.0" reqwest = { version = "0.11", features = ["json"] } tokio = { version = "1", features = ["full"] } +url = "2.2.2" yaserde = "0.7.1" yaserde_derive = "0.7.1" diff --git a/src/main.rs b/src/main.rs index dd4bc73..df18a03 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,13 +6,12 @@ use reqwest; use std::collections::HashMap; #[allow(unused)] use std::io::{self, BufReader, Write}; +use std::path::PathBuf; +use url::{Url, ParseError}; #[allow(unused)] use yaserde_derive::{YaDeserialize, YaSerialize}; use yaserde::de::from_str; -#[allow(unused)] -fn publicise() {} - #[derive(Default, PartialEq, Debug, YaDeserialize)] #[yaserde( rename = "multistatus", @@ -90,13 +89,13 @@ pub struct Collection { collection: String, } -async fn get_folder_contents(url_tail: &String, password: &String) -> Result> { +async fn get_folder_contents(url_tail: &str, password: &str) -> Result> { debug!("Entering: get_folder_contents()"); - let root_url = "https://theadamcooper.com"; + let root_url = Url::parse("https://theadamcooper.com")?; let method = reqwest::Method::from_bytes(b"PROPFIND").unwrap(); let client = reqwest::Client::new(); - let url = format!("{}{}", root_url, url_tail); - debug!("url: {}", url); + let url = root_url.join(url_tail)?; + debug!("get_folder_contents: url: {}", url); let body = String::from( r#" @@ -119,17 +118,18 @@ async fn get_folder_contents(url_tail: &String, password: &String) -> Result Result> { +async fn publicise_it(path: &str, password: &str) -> Result> { debug!("Entering: publicise()"); let url = "https://theadamcooper.com/nextcloud/ocs/v2.php/apps/files_sharing/api/v1/shares"; let method = reqwest::Method::POST; let client = reqwest::Client::new(); - let params = [("path", path.as_str()), ("shareType", "3")]; - debug!("url: {}", url); + let params = [("path", path), ("shareType", "3")]; + debug!("publicise_it: url: {}", url); + debug!("params: {:?}", params); let response_text = client .request(method, url) .basic_auth("adam", Some(password)) @@ -139,11 +139,11 @@ async fn publicise_it(path: &String, password: &String) -> Result Result> { +async fn traverse(mut result: Multistatus, password: &str) -> Result> { debug!("Entering: traverse()"); // Initialize the indexed "pointer" let mut current_index: usize = 0; @@ -170,7 +170,7 @@ async fn traverse(mut result: Multistatus, password: &String) -> Result Result std::io::Result<()> { env_logger::Builder::from_env(Env::default().default_filter_or("trace")) .target(Target::Stdout) .init(); - println!("Publicise it!"); + println!("Publicise it!\n\n"); let password: String = get_password().unwrap().trim().to_string(); let folder_contents: String = // get_folder_contents("/nextcloud/remote.php/dav/files/adam/test_public/2019_test_public") - get_folder_contents(&String::from("/nextcloud/remote.php/dav/files/adam/test_public"), &password) + get_folder_contents("/nextcloud/remote.php/dav/files/adam/test_public", &password) .await .unwrap(); - debug!("{:?}", folder_contents); + // debug!("{:?}", folder_contents); let mut result: Multistatus = from_str(&folder_contents).unwrap(); println!("{:?}", result); - let result: bool = traverse(result, &password).await.unwrap(); + let _ = traverse(result, &password).await.unwrap(); Ok(()) }