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 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();
|
|
|
|
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() {
|
|
|
|
println!("[FATAL] The was a problem accessing some of the files you provided!");
|
|
|
|
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:");
|
|
|
|
nonexistant.iter().for_each(|path| println!("{}", path.display()));
|
|
|
|
}
|
|
|
|
if !noread.is_empty() {
|
|
|
|
println!("\nWe found these files, but didn't have permission to open them:");
|
|
|
|
noread.iter().for_each(|path| println!("{}", path.display()));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let agent = upload::get_agent();
|
|
|
|
let full_server = if args.server.starts_with("http") {
|
|
|
|
Cow::Borrowed(&args.server)
|
|
|
|
} else {
|
|
|
|
Cow::Owned(format!("https://{}", args.server))
|
|
|
|
};
|
2022-08-12 01:41:18 +00:00
|
|
|
let index_url = files.into_iter()
|
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))
|
|
|
|
)
|
|
|
|
.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))
|
|
|
|
}))
|
|
|
|
.map_ok(|(raw_dat, thumbnail, blurhash)| {
|
|
|
|
let key = crypto::make_key();
|
|
|
|
(
|
|
|
|
key,
|
|
|
|
crypto::encrypt(&key, &raw_dat),
|
|
|
|
crypto::encrypt(&key, &thumbnail),
|
|
|
|
blurhash
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.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 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);
|
|
|
|
upload::put_data(&agent, &*full_server, &encrypted_index)
|
|
|
|
.map_err(|e| AviaryError::from_upload_error(e))
|
|
|
|
.map(|url| format!("{}#{}", url.trim(), &encoded_key))
|
|
|
|
})
|
2022-08-12 00:00:13 +00:00
|
|
|
.unwrap();
|
2022-08-12 01:41:18 +00:00
|
|
|
println!("Success! {}", index_url);
|
2022-08-12 00:00:13 +00:00
|
|
|
}
|
|
|
|
}
|