Send full url to subcommands

This commit is contained in:
Emi Simpson 2022-08-13 21:55:53 -04:00
parent 04417a96a4
commit b3a6c51ff8
Signed by: Emi
GPG key ID: A12F2C2FFDC3D847

View file

@ -5,8 +5,8 @@ mod thumbnailing;
mod protobuf; mod protobuf;
mod errors; mod errors;
use std::borrow::Cow;
use std::io::Read; use std::io::Read;
use std::{borrow::Cow, io::stdout};
use std::fs::File; use std::fs::File;
use std::path::Path; use std::path::Path;
@ -34,9 +34,15 @@ fn trim_url<'a>(base_url: &str, url: &'a str) -> Option<&'a str> {
fn main() { fn main() {
let args: parse::Args = argh::from_env(); let args: parse::Args = argh::from_env();
let server_no_trailing_slash = args.server.trim_end_matches('/');
let full_server = if server_no_trailing_slash.starts_with("http") {
Cow::Borrowed(server_no_trailing_slash)
} else {
Cow::Owned(format!("https://{}", server_no_trailing_slash))
};
match args.command { match args.command {
Command::Create(create_args) => create(&args.server, create_args), Command::Create(create_args) => create(&*full_server, create_args),
Command::Download(download_args) => download(&args.server, download_args), Command::Download(download_args) => download(&*full_server, download_args),
} }
} }
@ -69,11 +75,6 @@ fn create(server: &str, args: CreateArgs) {
} else { } else {
println!(" \x1b[32mDone!\n"); println!(" \x1b[32mDone!\n");
let agent = upload::get_agent(); let agent = upload::get_agent();
let full_server = if server.starts_with("http") {
Cow::Borrowed(server)
} else {
Cow::Owned(format!("https://{}", server))
};
let index_url = files.into_iter() let index_url = files.into_iter()
.inspect(|(path, _)| print!("\x1b[36m{}\x1b[0m\n\x1b[37m├─\x1b[0m Reading... ", path.display())) .inspect(|(path, _)| print!("\x1b[36m{}\x1b[0m\n\x1b[37m├─\x1b[0m Reading... ", path.display()))
.inspect(|_| drop(stdout().flush())) .inspect(|_| drop(stdout().flush()))
@ -105,15 +106,15 @@ fn create(server: &str, args: CreateArgs) {
.inspect(|r| if r.is_ok() { print!("\x1b[32mDone!\n\x1b[37m└─\x1b[0m Uploading... ")}) .inspect(|r| if r.is_ok() { print!("\x1b[32mDone!\n\x1b[37m└─\x1b[0m Uploading... ")})
.inspect(|_| drop(stdout().flush())) .inspect(|_| drop(stdout().flush()))
.map(|r| r.and_then(|(key, full_img, thumb, blurhash)| .map(|r| r.and_then(|(key, full_img, thumb, blurhash)|
upload::put_data(&agent, &*full_server, &thumb) upload::put_data(&agent, server, &thumb)
.and_then(|thumb_url| .and_then(|thumb_url|
upload::put_data(&agent, &*full_server, &full_img) upload::put_data(&agent, server, &full_img)
.map(|full_url| (key, full_url, thumb_url, blurhash))) .map(|full_url| (key, full_url, thumb_url, blurhash)))
.map_err(AviaryError::from_upload_error) .map_err(AviaryError::from_upload_error)
)) ))
.map(|r| r.and_then(|(key, full_url, thumb_url, blurhash)| { .map(|r| r.and_then(|(key, full_url, thumb_url, blurhash)| {
let full_trimmed = trim_url(&*full_server, &full_url); let full_trimmed = trim_url(server, &full_url);
let thmb_trimmed = trim_url(&*full_server, &thumb_url); let thmb_trimmed = trim_url(server, &thumb_url);
if let (Some(full_url), Some(thmb_url)) = (full_trimmed, thmb_trimmed) { if let (Some(full_url), Some(thmb_url)) = (full_trimmed, thmb_trimmed) {
Ok((key, full_url.to_owned(), thmb_url.to_owned(), blurhash)) Ok((key, full_url.to_owned(), thmb_url.to_owned(), blurhash))
} else { } else {
@ -142,7 +143,7 @@ fn create(server: &str, args: CreateArgs) {
); );
let encoded_key = base64::encode(index_key); let encoded_key = base64::encode(index_key);
print!("\x1b[0mUploading index... "); print!("\x1b[0mUploading index... ");
upload::put_data(&agent, &*full_server, &encrypted_index) upload::put_data(&agent, server, &encrypted_index)
.map_err(|e| AviaryError::from_upload_error(e)) .map_err(|e| AviaryError::from_upload_error(e))
.map(|url| format!("{}#{}", url.trim().trim_end_matches(".bin"), &encoded_key)) .map(|url| format!("{}#{}", url.trim().trim_end_matches(".bin"), &encoded_key))
}); });