publicise-rs/src/main.rs

70 lines
2.1 KiB
Rust

extern crate xml;
#[allow(unused)]
// use dotenv::dotenv;
use env_logger::{ Env, Target };
#[allow(unused)]
use log::{debug, info, log_enabled, warn, error};
use reqwest;
#[allow(unused)]
use std::io::{self, Write};
#[allow(unused)]
use xml::reader::{EventReader, XmlEvent};
#[allow(unused)]
fn publicise() {
}
#[allow(unused)]
async fn get_folder_contents(password: String, url_tail: &str) -> Result<reqwest::Response, Box<dyn std::error::Error>> {
debug!("Entering: get_folder_contents()");
let root_url = "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 body = String::from(r#"<?xml version="1.0" encoding="UTF-8"?>
<d:propfind xmlns:d="DAV:">
<d:prop xmlns:oc="http://owncloud.org/ns">
<d:getlastmodified/>
<d:getcontentlength/>
<d:getcontenttype/>
<oc:permissions/>
<d:resourcetype/>
<d:getetag/>
<oc:share-types/>
</d:prop>
</d:propfind>"#);
let response = client.request(method, url)
.basic_auth("adam", Some(password))
.body(body)
.send()
.await?;
debug!("{:?}", response);
Ok(response)
}
fn get_password() -> Result<String, std::io::Error> {
print!("Nextcloud password: ");
io::stdout().flush().unwrap();
let mut buffer = String::new();
let stdin = io::stdin();
match stdin.read_line(&mut buffer) {
Ok(_) => Ok(buffer),
Err(error) => Err(error),
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::Builder::from_env(Env::default().default_filter_or("trace")).target(Target::Stdout).init();
println!("Publicise it!");
let password = get_password().unwrap();
debug!("Received password: {}", password);
let folder_contents = get_folder_contents(password, "/nextcloud/remote.php/dav/files/adam/public").await?;
debug!("{:?}", folder_contents);
Ok(())
}