113 lines
3.4 KiB
Rust
113 lines
3.4 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, BufReader, Write};
|
|
#[allow(unused)]
|
|
use xml::reader::{EventReader, XmlEvent};
|
|
|
|
|
|
#[allow(unused)]
|
|
fn publicise() {
|
|
}
|
|
|
|
async fn get_folder_contents(password: String, url_tail: &str) -> Result<String, 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_text = client.request(method, url)
|
|
.basic_auth("adam", Some(password))
|
|
.body(body)
|
|
.send()
|
|
.await?
|
|
.text()
|
|
.await?;
|
|
debug!("{:?}", response_text);
|
|
Ok(response_text)
|
|
}
|
|
|
|
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),
|
|
}
|
|
}
|
|
|
|
fn indent(size: usize) -> String {
|
|
const INDENT: &'static str = " ";
|
|
(0..size).map(|_| INDENT)
|
|
.fold(String::with_capacity(size*INDENT.len()), |r, s| r + s)
|
|
}
|
|
|
|
fn parse_xml(xml_string: String) {
|
|
let xml_str = &xml_string;
|
|
let parser = EventReader::from_str(xml_str);
|
|
let mut depth = 0;
|
|
let mut resourcetype_element = false;
|
|
let mut share_type_element = false;
|
|
for e in parser {
|
|
match e {
|
|
Ok(XmlEvent::StartElement { name, .. }) => {
|
|
println!("{}+{}", indent(depth), name);
|
|
depth += 1;
|
|
if name.local_name.matches("resourcetype").collect::<Vec<&str>>().len() > 0 {
|
|
println!("{} Element: resourcetype", indent(depth));
|
|
resourcetype_element = true;
|
|
}
|
|
}
|
|
Ok(XmlEvent::Characters(s)) => {
|
|
println!("{} {}", indent(depth), s);
|
|
}
|
|
Ok(XmlEvent::EndElement { name }) => {
|
|
depth -= 1;
|
|
println!("{}-{}", indent(depth), name);
|
|
}
|
|
Err(e) => {
|
|
println!("Error: {}", e);
|
|
break;
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[tokio::main]
|
|
async fn main() -> std::io::Result<()> {
|
|
env_logger::Builder::from_env(Env::default().default_filter_or("trace")).target(Target::Stdout).init();
|
|
println!("Publicise it!");
|
|
let password = get_password().unwrap().trim().to_string();
|
|
debug!("Received password: {}[END]", password);
|
|
|
|
let folder_contents = get_folder_contents(password, "/nextcloud/remote.php/dav/files/adam/test_public").await.unwrap();
|
|
debug!("{:?}", folder_contents);
|
|
|
|
parse_xml(folder_contents);
|
|
Ok(())
|
|
}
|
|
|
|
// TO_BE_DELETED: Testing removal of SSH password.
|