[wip] No errors, may have a working version here.

This commit is contained in:
Adam Cooper 2022-02-27 03:13:49 -05:00
parent 062d973d8a
commit 6292b5dddf

View file

@ -90,10 +90,8 @@ pub struct Collection {
collection: String,
}
async fn get_folder_contents(url_tail: &String) -> Result<String, Box<dyn std::error::Error>> {
async fn get_folder_contents(url_tail: &String, password: &String) -> Result<String, Box<dyn std::error::Error>> {
debug!("Entering: get_folder_contents()");
let password = get_password().unwrap().trim().to_string();
debug!("Received password: {}[END]", password);
let root_url = "https://theadamcooper.com";
let method = reqwest::Method::from_bytes(b"PROPFIND").unwrap();
let client = reqwest::Client::new();
@ -125,8 +123,28 @@ async fn get_folder_contents(url_tail: &String) -> Result<String, Box<dyn std::e
Ok(response_text)
}
async fn publicise_it(path: &String, password: &String) -> Result<bool, Box<dyn std::error::Error>> {
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 response_text = client
.request(method, url)
.basic_auth("adam", Some(password))
.header("OCS-APIRequest", "true")
.form(&params)
.send()
.await?
.text()
.await?;
debug!("{:?}", response_text);
Ok(true)
}
#[tokio::main]
async fn traverse(mut result: Multistatus) {
async fn traverse(mut result: Multistatus, password: &String) {
debug!("Entering: traverse()");
// Initialize the indexed "pointer"
let mut current_index: usize = 0;
@ -139,8 +157,9 @@ async fn traverse(mut result: Multistatus) {
// Depth first traversal
while current_index < (&mut result.response).len() {
debug!("current_index: {:?}", current_index);
let mut etag = (&mut result.response[current_index].propstat[0].prop.get_etag).clone();
// If result.response[current_index] has not been visited
if !visited_items.contains_key(&mut result.response[current_index].propstat[0].prop.get_etag) {
if !visited_items.contains_key(&etag) {
debug!("Fresh item...");
// if it's a collection
if !(&mut result.response[current_index].propstat[0].prop.resource_type.collection).is_empty() {
@ -148,6 +167,7 @@ async fn traverse(mut result: Multistatus) {
// Get the contents XML
let folder_contents: String = get_folder_contents(
&result.response[current_index].href, // change to mutable borrow if necessary
password,
)
.await
.unwrap();
@ -162,9 +182,10 @@ async fn traverse(mut result: Multistatus) {
// else it's a node. if it's not public, publicise it.
if !(&mut result.response[current_index].propstat[0].prop.share_types).contains(&ShareType{ share_type: 3 }) {
println!("it's not public");
publicise_it(&result.response[current_index].href, password).await.unwrap();
}
}
// also add debugging statements in this function throughout!
visited_items.insert(etag, true);
}
current_index += 1;
}
@ -198,11 +219,10 @@ async fn main() -> std::io::Result<()> {
.init();
println!("Publicise it!");
// TODO: Call get_password here. Maybe get_password should then store the password in the
// environment?
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"))
get_folder_contents(&String::from("/nextcloud/remote.php/dav/files/adam/test_public"), &password)
.await
.unwrap();
debug!("{:?}", folder_contents);
@ -210,6 +230,6 @@ async fn main() -> std::io::Result<()> {
let mut result: Multistatus = from_str(&folder_contents).unwrap();
println!("{:?}", result);
traverse(result);
traverse(result, &password);
Ok(())
}