Compare commits

..

7 commits

Author SHA1 Message Date
54735fdf06 Merge branch '012-loose-ends' 2022-03-22 01:32:26 -04:00
3aa9b8d3d0 Add a README (#12) 2022-03-22 01:32:05 -04:00
50c36bbcbd Add WTF license (#9) 2022-03-22 01:17:19 -04:00
72062842e7 Panic if password script yields no password (#12)
This would happen, e.g., if the database is locked.
2022-03-22 01:10:21 -04:00
e77b79afae Improve URL handling (#11) 2022-03-22 00:47:02 -04:00
7d00d6f863 Remove hard-coded username instances (#9) 2022-03-21 09:30:43 -04:00
5bb7fc6eaf Clean up logging (#8) 2022-03-21 03:43:56 -04:00
3 changed files with 36 additions and 16 deletions

13
LICENSE Normal file
View file

@ -0,0 +1,13 @@
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.

5
README.md Normal file
View file

@ -0,0 +1,5 @@
### 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.

View file

@ -1,6 +1,6 @@
extern crate xdg;
use env_logger::{Env, Target};
use env_logger::Env;
use log::debug;
use reqwest;
use serde::Deserialize;
@ -109,10 +109,12 @@ 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);
}
@ -139,7 +141,7 @@ async fn get_folder_contents(url_tail: &str, config: &Config) -> Result<String,
);
let response_text = client
.request(method, url)
.basic_auth("adam", config.credentials.password.as_ref())
.basic_auth(config.credentials.username.as_str(), config.credentials.password.as_ref())
.body(body)
.send()
.await?
@ -149,9 +151,10 @@ async fn get_folder_contents(url_tail: &str, config: &Config) -> Result<String,
Ok(response_text)
}
async fn publicise_it(path: &str, password: &str) -> Result<bool, Box<dyn std::error::Error>> {
async fn publicise_it(path: &str, config: &Config) -> Result<bool, Box<dyn std::error::Error>> {
debug!("[publicise_it] Entering function...");
let url = "https://cloud.theadamcooper.com/ocs/v2.php/apps/files_sharing/api/v1/shares";
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 method = reqwest::Method::POST;
let client = reqwest::Client::new();
let params = [("path", path), ("shareType", "3")];
@ -159,7 +162,7 @@ async fn publicise_it(path: &str, password: &str) -> Result<bool, Box<dyn std::e
debug!("[publicise_it] params: {:?}", params);
let response_text = client
.request(method, url)
.basic_auth("adam", Some(password))
.basic_auth(config.credentials.username.as_str(), config.credentials.password.as_ref())
.header("OCS-APIRequest", "true")
.form(&params)
.send()
@ -200,13 +203,13 @@ async fn traverse(mut result: Multistatus, config: &Config) -> Result<bool, Box<
debug!("[traverse] Node...");
if !(&mut result.response[current_index].propstat[0].prop.share_types).contains(&ShareType{ share_type: 3 }) {
debug!("[traverse] it's not public");
let username = "adam";
let username = config.credentials.username.as_str();
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.credentials.password.as_ref().unwrap()).await.unwrap();
publicise_it(new_href, config).await.unwrap();
} else {
debug!("[traverse] it's already public");
}
@ -223,27 +226,26 @@ async fn traverse(mut result: Multistatus, config: &Config) -> Result<bool, Box<
#[tokio::main]
async fn main() -> std::io::Result<()> {
env_logger::Builder::from_env(Env::default().default_filter_or("debug"))
.target(Target::Stdout)
.init();
env_logger::Builder::from_env(Env::default()).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!("{:?}", &config);
debug!("[main] {:?}", &config);
let full_path = &(String::from("/remote.php/dav/files/") + &config.credentials.username + "/" + &config.paths.target);
let folder_contents: String =
get_folder_contents(full_path, &config)
.await
.unwrap();
let result: Multistatus = from_str(&folder_contents).unwrap();
debug!("{:?}", result);
debug!("[main] {:?}", result);
let _ = traverse(result, &config).await.unwrap();
Ok(())
}