2022-08-12 00:00:13 +00:00
|
|
|
mod parse;
|
|
|
|
mod crypto;
|
|
|
|
mod upload;
|
|
|
|
mod thumbnailing;
|
2022-08-12 01:41:18 +00:00
|
|
|
mod protobuf;
|
|
|
|
mod errors;
|
2022-08-12 00:00:13 +00:00
|
|
|
|
|
|
|
use std::borrow::Cow;
|
2022-08-12 01:41:18 +00:00
|
|
|
use std::io::Read;
|
2022-08-12 00:00:13 +00:00
|
|
|
use std::fs::File;
|
2022-08-12 01:41:18 +00:00
|
|
|
use std::path::Path;
|
2022-08-12 00:00:13 +00:00
|
|
|
|
2022-08-12 01:41:18 +00:00
|
|
|
use errors::AviaryError;
|
2022-08-12 00:00:13 +00:00
|
|
|
use itertools::{Itertools, Either};
|
2022-08-12 20:14:30 +00:00
|
|
|
use parse::{CreateArgs, Command, DownloadArgs};
|
2022-08-12 01:41:18 +00:00
|
|
|
use ::protobuf::Message;
|
2022-08-12 00:00:13 +00:00
|
|
|
|
|
|
|
fn trim_url<'a>(base_url: &str, url: &'a str) -> Option<&'a str> {
|
|
|
|
if url.starts_with(base_url) {
|
|
|
|
let shortened = url
|
|
|
|
.trim()
|
|
|
|
.trim_start_matches(base_url)
|
|
|
|
.trim_matches('/')
|
|
|
|
.trim_end_matches(".bin");
|
|
|
|
if shortened.len() > 50 {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(shortened)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let args: parse::Args = argh::from_env();
|
2022-08-12 20:14:30 +00:00
|
|
|
match args.command {
|
|
|
|
Command::Create(create_args) => create(&args.server, create_args),
|
|
|
|
Command::Download(download_args) => download(&args.server, download_args),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create(server: &str, args: CreateArgs) {
|
2022-08-12 03:00:15 +00:00
|
|
|
print!("Checking files...");
|
2022-08-12 00:00:13 +00:00
|
|
|
let (files, errors): (Vec<_>, Vec<_>) = args.images.iter()
|
|
|
|
.map(Path::new)
|
|
|
|
.map(|path| File::open(path)
|
|
|
|
.map(|file| (path, file))
|
2022-08-12 01:41:18 +00:00
|
|
|
.map_err(|e| AviaryError::from_open_error(e.kind(), &path))
|
|
|
|
)
|
2022-08-12 00:00:13 +00:00
|
|
|
.partition_result();
|
|
|
|
|
|
|
|
if !errors.is_empty() {
|
2022-08-12 03:00:15 +00:00
|
|
|
println!(" \x1b[31mError!\x1b[0m");
|
2022-08-12 00:00:13 +00:00
|
|
|
let (nonexistant, noread): (Vec<_>, Vec<_>) = errors.iter()
|
|
|
|
.partition_map(|e| match e {
|
|
|
|
AviaryError::FileDNE(path) => Either::Left(path),
|
|
|
|
AviaryError::ReadPermissionDenied(path) => Either::Right(path),
|
|
|
|
other => panic!("This error should not be possible! {other:?}")
|
|
|
|
});
|
|
|
|
if !nonexistant.is_empty() {
|
|
|
|
println!("\nWe didn't see any files at the following locations:");
|
2022-08-12 03:00:15 +00:00
|
|
|
nonexistant.iter().for_each(|path| println!("\x1b[37m- \x1b[31m{}", path.display()));
|
2022-08-12 00:00:13 +00:00
|
|
|
}
|
|
|
|
if !noread.is_empty() {
|
2022-08-12 03:00:15 +00:00
|
|
|
println!("\x1b[0m\nWe found these files, but didn't have permission to open them:");
|
|
|
|
noread.iter().for_each(|path| println!("\x1b[37m- \x1b[31m{}", path.display()));
|
2022-08-12 00:00:13 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-08-12 03:00:15 +00:00
|
|
|
println!(" \x1b[32mDone!\n");
|
2022-08-12 00:00:13 +00:00
|
|
|
let agent = upload::get_agent();
|
2022-08-12 20:14:30 +00:00
|
|
|
let full_server = if server.starts_with("http") {
|
|
|
|
Cow::Borrowed(server)
|
2022-08-12 00:00:13 +00:00
|
|
|
} else {
|
2022-08-12 20:14:30 +00:00
|
|
|
Cow::Owned(format!("https://{}", server))
|
2022-08-12 00:00:13 +00:00
|
|
|
};
|
2022-08-12 01:41:18 +00:00
|
|
|
let index_url = files.into_iter()
|
2022-08-12 03:00:15 +00:00
|
|
|
.inspect(|(path, _)| print!("\x1b[36m{}\x1b[0m\n\x1b[37m├─\x1b[0m Reading... ", path.display()))
|
2022-08-12 00:00:13 +00:00
|
|
|
.map(|(path, mut file)|
|
|
|
|
(|| {
|
|
|
|
let mut buff = Vec::with_capacity(file.metadata()?.len() as usize);
|
|
|
|
file.read_to_end(&mut buff)?;
|
|
|
|
Ok((path, buff))
|
|
|
|
})().map_err(|e| AviaryError::StreamReadError(path.to_owned(), e))
|
|
|
|
)
|
2022-08-12 03:00:15 +00:00
|
|
|
.inspect(|r| if r.is_ok() { print!("\x1b[32mDone!\n\x1b[37m├─\x1b[0m Thumbnailing... ") })
|
2022-08-12 00:00:13 +00:00
|
|
|
.map(|r| r.and_then(|(path, raw_dat)| {
|
|
|
|
let (thumbnail, blurhash) = thumbnailing::thumbnail(&raw_dat)
|
|
|
|
.map_err(|_| AviaryError::ImageFormatError(path.to_owned()))?;
|
|
|
|
Ok((raw_dat, thumbnail, blurhash))
|
|
|
|
}))
|
2022-08-12 03:00:15 +00:00
|
|
|
.inspect(|r| if r.is_ok() { print!("\x1b[32mDone!\n\x1b[37m├─\x1b[0m Encrypting... ")})
|
2022-08-12 00:00:13 +00:00
|
|
|
.map_ok(|(raw_dat, thumbnail, blurhash)| {
|
|
|
|
let key = crypto::make_key();
|
|
|
|
(
|
|
|
|
key,
|
|
|
|
crypto::encrypt(&key, &raw_dat),
|
|
|
|
crypto::encrypt(&key, &thumbnail),
|
|
|
|
blurhash
|
|
|
|
)
|
|
|
|
})
|
2022-08-12 03:00:15 +00:00
|
|
|
.inspect(|r| if r.is_ok() { print!("\x1b[32mDone!\n\x1b[37m└─\x1b[0m Uploading... ")})
|
2022-08-12 00:00:13 +00:00
|
|
|
.map(|r| r.and_then(|(key, full_img, thumb, blurhash)|
|
|
|
|
upload::put_data(&agent, &*full_server, &thumb)
|
|
|
|
.and_then(|thumb_url|
|
|
|
|
upload::put_data(&agent, &*full_server, &full_img)
|
|
|
|
.map(|full_url| (key, full_url, thumb_url, blurhash)))
|
2022-08-12 01:41:18 +00:00
|
|
|
.map_err(AviaryError::from_upload_error)
|
2022-08-12 00:00:13 +00:00
|
|
|
))
|
|
|
|
.map(|r| r.and_then(|(key, full_url, thumb_url, blurhash)| {
|
|
|
|
let full_trimmed = trim_url(&*full_server, &full_url);
|
|
|
|
let thmb_trimmed = trim_url(&*full_server, &thumb_url);
|
|
|
|
if let (Some(full_url), Some(thmb_url)) = (full_trimmed, thmb_trimmed) {
|
|
|
|
Ok((key, full_url.to_owned(), thmb_url.to_owned(), blurhash))
|
|
|
|
} else {
|
|
|
|
Err(AviaryError::ServerError(format!("Received bad response from server: {}", full_url)))
|
|
|
|
}
|
|
|
|
}))
|
2022-08-12 03:00:15 +00:00
|
|
|
.inspect(|r| if r.is_ok() { println!("\x1b[32mDone!\n")})
|
2022-08-12 01:41:18 +00:00
|
|
|
.map_ok(|(key, full_url, thumb_url, blurhash)| protobuf::image::Image {
|
|
|
|
key: key.into(),
|
|
|
|
full_url, thumb_url, blurhash,
|
|
|
|
special_fields: Default::default()
|
|
|
|
})
|
2022-08-12 00:00:13 +00:00
|
|
|
.collect::<Result<Vec<_>, _>>()
|
2022-08-12 01:41:18 +00:00
|
|
|
.and_then(|image_info|{
|
|
|
|
let index = protobuf::index::Index {
|
|
|
|
images: image_info,
|
|
|
|
title: args.title,
|
|
|
|
desc: None,
|
|
|
|
special_fields: Default::default()
|
|
|
|
};
|
|
|
|
let index_key = crypto::make_key();
|
|
|
|
let encrypted_index = crypto::encrypt(
|
|
|
|
&index_key,
|
|
|
|
&index.write_to_bytes()
|
|
|
|
.expect("Error serializing protocol buffers")
|
|
|
|
);
|
|
|
|
let encoded_key = base64::encode(index_key);
|
2022-08-12 03:00:15 +00:00
|
|
|
print!("\x1b[0mUploading index... ");
|
2022-08-12 01:41:18 +00:00
|
|
|
upload::put_data(&agent, &*full_server, &encrypted_index)
|
|
|
|
.map_err(|e| AviaryError::from_upload_error(e))
|
2022-08-12 03:00:15 +00:00
|
|
|
.map(|url| format!("{}#{}", url.trim().trim_end_matches(".bin"), &encoded_key))
|
|
|
|
});
|
|
|
|
match index_url {
|
|
|
|
Ok(url) =>
|
|
|
|
println!("\x1b[32mDone!\n\n\x1b[34mYour gallery is: \x1b[1;0m{}", url),
|
|
|
|
Err(e) =>
|
|
|
|
print!("{}", e),
|
|
|
|
}
|
2022-08-12 00:00:13 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-12 20:14:30 +00:00
|
|
|
|
|
|
|
fn download(server: &str, args: DownloadArgs) {
|
|
|
|
drop(server);
|
|
|
|
drop(args);
|
|
|
|
todo!("Unimplemented... yet")
|
|
|
|
}
|