Merge branch 'yaserde'

This commit is contained in:
Adam Cooper 2022-01-08 17:12:56 -05:00
commit 5bc4b24fd9
4 changed files with 1319 additions and 2 deletions

1
.gitignore vendored
View file

@ -1,3 +1,4 @@
/target
.env
Cargo.lock
example*.xml

1151
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

View file

@ -6,3 +6,10 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
dotenv = "0.15.0"
env_logger = "0.8.4"
log = "0.4.0"
reqwest = { version = "0.11", features = ["json"] }
tokio = { version = "1", features = ["full"] }
yaserde = "0.7.1"
yaserde_derive = "0.7.1"

View file

@ -1,3 +1,161 @@
fn main() {
println!("Hello, world!");
#[allow(unused)]
use env_logger::{Env, Target};
#[allow(unused)]
use log::{debug, error, info, log_enabled, warn};
use reqwest;
#[allow(unused)]
use std::io::{self, BufReader, Write};
#[allow(unused)]
use yaserde_derive::{YaDeserialize, YaSerialize};
#[allow(unused)]
fn publicise() {}
#[derive(Default, PartialEq, Debug, YaDeserialize)]
#[yaserde(
rename = "multistatus",
prefix = "d",
namespace = "d: DAV:",
namespace = "nc: http://nextcloud.org/ns",
namespace = "oc: http://owncloud.org/ns",
namespace = "s: http://sabredav.org/ns",
)]
pub struct Multistatus {
#[yaserde(prefix = "d")]
response: Vec<NextcloudResponse>,
}
#[derive(Default, PartialEq, Debug, YaDeserialize)]
#[yaserde(rename = "response", prefix = "d", namespace = "d: DAV:")]
pub struct NextcloudResponse {
#[yaserde(prefix = "d")]
href: Option<String>,
#[yaserde(prefix = "d")]
propstat: Vec<Propstat>,
}
#[derive(Default, PartialEq, Debug, YaDeserialize)]
#[yaserde(rename = "propstat", prefix = "d", namespace = "d: DAV:")]
pub struct Propstat {
#[yaserde(prefix = "d")]
prop: Option<Prop>,
#[yaserde(prefix = "d")]
status: Option<String>,
}
#[derive(Default, PartialEq, Debug, YaDeserialize)]
#[yaserde(rename = "prop", namespace = "d: DAV:", namespace = "oc: http://owncloud.org/ns")]
pub struct Prop {
#[yaserde(prefix = "d", rename = "getlastmodified")]
get_last_modified: Option<String>,
#[yaserde(prefix = "oc")]
permissions: Option<String>,
#[yaserde(prefix = "d", rename = "resourcetype")]
resource_type: Option<ResourceType>,
#[yaserde(prefix = "d", rename = "getetag")]
get_etag: Option<String>,
#[yaserde(prefix = "d", rename = "getcontentlength")]
get_content_length: Option<u32>,
#[yaserde(prefix = "d", rename = "getcontenttype")]
get_content_type: Option<String>,
#[yaserde(prefix = "oc", rename = "share-types")]
share_types: Vec<ShareType>,
}
#[derive(Default, PartialEq, Debug, YaDeserialize)]
#[yaserde(rename = "share-types", namespace = "oc: http://owncloud.org/ns")]
pub struct ShareType {
#[yaserde(rename = "share-type", prefix = "oc")]
share_type: u8,
}
#[derive(Default, PartialEq, Debug, YaDeserialize)]
#[yaserde(rename = "resourcetype", namespace = "d: DAV:")]
pub struct ResourceType {
#[yaserde(prefix = "d")]
collection: Vec<Collection>,
}
#[derive(Default, PartialEq, Debug, YaDeserialize)]
#[yaserde(rename = "collection", namespace = "d: DAV:")]
pub struct Collection {
#[yaserde(prefix = "d")]
collection: Option<String>,
}
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),
}
}
#[allow(unused)]
fn indent(size: usize) -> String {
const INDENT: &'static str = " ";
(0..size)
.map(|_| INDENT)
.fold(String::with_capacity(size * INDENT.len()), |r, s| r + s)
}
#[tokio::main]
async fn main() -> std::io::Result<()> {
use yaserde::de::from_str;
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/2019_test_public")
.await
.unwrap();
debug!("{:?}", folder_contents);
let result: Multistatus = from_str(&folder_contents).unwrap();
println!("{:?}", result);
Ok(())
}