Compare commits
No commits in common. "main" and "006-configuration" have entirely different histories.
main
...
006-config
3 changed files with 58 additions and 39 deletions
13
LICENSE
13
LICENSE
|
@ -1,13 +0,0 @@
|
|||
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
|
||||
Version 2, December 2004
|
||||
|
||||
Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
|
||||
|
||||
Everyone is permitted to copy and distribute verbatim or modified
|
||||
copies of this license document, and changing it is allowed as long
|
||||
as the name is changed.
|
||||
|
||||
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
|
||||
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
||||
|
||||
0. You just DO WHAT THE FUCK YOU WANT TO.
|
|
@ -1,5 +0,0 @@
|
|||
### publicise-rs
|
||||
|
||||
_Publicise_ is an application which traverses a Nextcloud folder and creates a public share for each file.
|
||||
|
||||
The application was originally devised as a way to share photos with people without Instagram accounts, and as an exploration of Rust.
|
79
src/main.rs
79
src/main.rs
|
@ -1,16 +1,21 @@
|
|||
extern crate xdg;
|
||||
|
||||
use env_logger::Env;
|
||||
use log::debug;
|
||||
#[allow(unused)]
|
||||
use env_logger::{Env, Target};
|
||||
#[allow(unused)]
|
||||
use log::{debug, error, info, log_enabled, warn};
|
||||
use reqwest;
|
||||
use serde::Deserialize;
|
||||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
use std::io::{self, Write};
|
||||
#[allow(unused)]
|
||||
use std::process::Command;
|
||||
use std::str;
|
||||
use toml;
|
||||
use url::Url;
|
||||
use yaserde_derive::YaDeserialize;
|
||||
#[allow(unused)]
|
||||
use yaserde_derive::{YaDeserialize, YaSerialize};
|
||||
use yaserde::de::from_str;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
|
@ -50,6 +55,7 @@ pub struct Multistatus {
|
|||
#[yaserde(rename = "response", prefix = "d", namespace = "d: DAV:")]
|
||||
pub struct NextcloudResponse {
|
||||
#[yaserde(prefix = "d")]
|
||||
// href: Option<String>,
|
||||
href: String,
|
||||
#[yaserde(prefix = "d")]
|
||||
propstat: Vec<Propstat>,
|
||||
|
@ -109,15 +115,14 @@ pub struct Collection {
|
|||
}
|
||||
|
||||
fn init() -> Result<Config, Box<dyn std::error::Error>> {
|
||||
debug!("[init] Entering function...");
|
||||
let xdg_dirs = xdg::BaseDirectories::with_prefix("publicise-rs").unwrap();
|
||||
let config_path = xdg_dirs.find_config_file("config.toml").unwrap();
|
||||
let config_file_contents = fs::read_to_string(config_path).unwrap();
|
||||
let config: Config = toml::from_str(&config_file_contents).unwrap();
|
||||
debug!("[init] config: {:?}", config);
|
||||
return Ok(config);
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
async fn get_folder_contents(url_tail: &str, config: &Config) -> Result<String, Box<dyn std::error::Error>> {
|
||||
debug!("[get_folder_contents] Entering function...");
|
||||
let root_url = Url::parse(&config.paths.root)?;
|
||||
|
@ -141,7 +146,7 @@ async fn get_folder_contents(url_tail: &str, config: &Config) -> Result<String,
|
|||
);
|
||||
let response_text = client
|
||||
.request(method, url)
|
||||
.basic_auth(config.credentials.username.as_str(), config.credentials.password.as_ref())
|
||||
.basic_auth("adam", config.credentials.password.as_ref())
|
||||
.body(body)
|
||||
.send()
|
||||
.await?
|
||||
|
@ -151,10 +156,10 @@ async fn get_folder_contents(url_tail: &str, config: &Config) -> Result<String,
|
|||
Ok(response_text)
|
||||
}
|
||||
|
||||
async fn publicise_it(path: &str, config: &Config) -> Result<bool, Box<dyn std::error::Error>> {
|
||||
#[allow(unused)]
|
||||
async fn publicise_it(path: &str, password: &str) -> Result<bool, Box<dyn std::error::Error>> {
|
||||
debug!("[publicise_it] Entering function...");
|
||||
let base_url = Url::parse(config.paths.root.as_str())?;
|
||||
let url = base_url.join("ocs/v2.php/apps/files_sharing/api/v1/shares")?;
|
||||
let url = "https://cloud.theadamcooper.com/ocs/v2.php/apps/files_sharing/api/v1/shares";
|
||||
let method = reqwest::Method::POST;
|
||||
let client = reqwest::Client::new();
|
||||
let params = [("path", path), ("shareType", "3")];
|
||||
|
@ -162,7 +167,7 @@ async fn publicise_it(path: &str, config: &Config) -> Result<bool, Box<dyn std::
|
|||
debug!("[publicise_it] params: {:?}", params);
|
||||
let response_text = client
|
||||
.request(method, url)
|
||||
.basic_auth(config.credentials.username.as_str(), config.credentials.password.as_ref())
|
||||
.basic_auth("adam", Some(password))
|
||||
.header("OCS-APIRequest", "true")
|
||||
.form(¶ms)
|
||||
.send()
|
||||
|
@ -173,43 +178,54 @@ async fn publicise_it(path: &str, config: &Config) -> Result<bool, Box<dyn std::
|
|||
Ok(true)
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
async fn traverse(mut result: Multistatus, config: &Config) -> Result<bool, Box<dyn std::error::Error>> {
|
||||
debug!("[traverse] Entering function...");
|
||||
// Initialize the indexed "pointer"
|
||||
let mut current_index: usize = 0;
|
||||
// Initialize the hashmap of visited items (by etag?)
|
||||
let mut visited_items = HashMap::new();
|
||||
let href_key = (&mut result.response[current_index].href).clone();
|
||||
visited_items.insert(href_key, true);
|
||||
current_index += 1;
|
||||
|
||||
// Depth first traversal
|
||||
while current_index < (&mut result.response).len() {
|
||||
debug!("[traverse] current_index: {:?}", current_index);
|
||||
debug!("[traverse] current href: {}", &result.response[current_index].href);
|
||||
let href_key = (&mut result.response[current_index].href).clone();
|
||||
// If result.response[current_index] has not been visited
|
||||
if !visited_items.contains_key(&href_key) {
|
||||
debug!("[traverse] Fresh item...");
|
||||
// if it's a collection
|
||||
if !(&mut result.response[current_index].propstat[0].prop.resource_type.collection).is_empty() {
|
||||
debug!("[traverse] Collection...");
|
||||
// Get the contents XML
|
||||
let folder_contents: String = get_folder_contents(
|
||||
&result.response[current_index].href,
|
||||
&result.response[current_index].href, // change to mutable borrow if necessary
|
||||
config,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
// Parse the contents XML into Multistatus
|
||||
let mut new_result: Multistatus = from_str(&String::from(folder_contents)).unwrap();
|
||||
debug!("[traverse] Parsed:\n{:?}", new_result);
|
||||
// Append the NextcloudResponse vector to result.response
|
||||
result.response.append(&mut new_result.response);
|
||||
debug!("[traverse] new vector length: {}", &result.response.len());
|
||||
} else {
|
||||
debug!("[traverse] Node...");
|
||||
// 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 }) {
|
||||
debug!("[traverse] it's not public");
|
||||
let username = config.credentials.username.as_str();
|
||||
// Search for username and lop.
|
||||
let username = "adam";
|
||||
let username_seg_string = format!("/{}/", username);
|
||||
let username_seg = username_seg_string.as_str();
|
||||
let index = &result.response[current_index].href.find(username_seg).unwrap_or(0);
|
||||
let new_index = index + username.len() + 2;
|
||||
let new_href = &result.response[current_index].href[new_index..];
|
||||
publicise_it(new_href, config).await.unwrap();
|
||||
publicise_it(new_href, config.credentials.password.as_ref().unwrap()).await.unwrap();
|
||||
} else {
|
||||
debug!("[traverse] it's already public");
|
||||
}
|
||||
|
@ -224,28 +240,49 @@ async fn traverse(mut result: Multistatus, config: &Config) -> Result<bool, Box<
|
|||
Ok(true)
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
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<()> {
|
||||
env_logger::Builder::from_env(Env::default()).init();
|
||||
|
||||
env_logger::Builder::from_env(Env::default().default_filter_or("debug"))
|
||||
.target(Target::Stdout)
|
||||
.init();
|
||||
println!("Publicise it!\n\n");
|
||||
|
||||
let mut config = init().unwrap();
|
||||
if config.credentials.password == None {
|
||||
let output = Command::new(&config.credentials.password_script.as_ref().unwrap()).output().unwrap();
|
||||
if output.stdout.len() == 0 {
|
||||
panic!("[main] Failed to acquire password from provided script.");
|
||||
}
|
||||
config.credentials.password = Some(String::from_utf8(output.stdout).unwrap());
|
||||
}
|
||||
debug!("[main] {:?}", &config);
|
||||
|
||||
debug!("{:?}", &config);
|
||||
let full_path = &(String::from("/remote.php/dav/files/") + &config.credentials.username + "/" + &config.paths.target);
|
||||
let folder_contents: String =
|
||||
let folder_contents: String =
|
||||
get_folder_contents(full_path, &config)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let result: Multistatus = from_str(&folder_contents).unwrap();
|
||||
debug!("[main] {:?}", result);
|
||||
debug!("{:?}", result);
|
||||
|
||||
let _ = traverse(result, &config).await.unwrap();
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue