Download full images with proper extensions

This commit is contained in:
Emi Simpson 2022-08-14 18:07:00 -04:00
parent 801745a198
commit 9226015fcc
Signed by: Emi
GPG key ID: A12F2C2FFDC3D847

View file

@ -16,6 +16,8 @@ use parse::{CreateArgs, Command, DownloadArgs};
use ::protobuf::Message;
use sanitise_file_name::sanitise_with_options;
use crate::protobuf::image::Format;
fn trim_url<'a>(base_url: &str, url: &'a str) -> Option<&'a str> {
if url.starts_with(base_url) {
let shortened = url
@ -199,7 +201,14 @@ fn download(server: &str, args: DownloadArgs) {
.unwrap_or(Cow::Borrowed("Unnamed-Album".as_ref())));
fs::create_dir_all(&dest_dir).expect("Failed to create destination directory");
for (indx, image) in index.images.into_iter().enumerate() {
let path = dest_dir.join(format!("{indx:03}.webp"));
let extension = image.format.enum_value().map(|f| match f {
Format::PNG => "png",
Format::WEBP => "webp",
Format::AVIF => "avif",
Format::JPG => "jpg",
Format::GIF => "gif",
}).unwrap_or(".unk");
let path = dest_dir.join(format!("{indx:03}.{extension}"));
let mut dest_file = OpenOptions::new()
.write(true)
.truncate(true)
@ -209,7 +218,7 @@ fn download(server: &str, args: DownloadArgs) {
let key = image.key.try_into().expect("Invalid key size");
let encrypted_thumbnail = upload::get_data(
&download_agent,
&format!("{}/{}.bin", server, image.thumb_url),
&format!("{}/{}.bin", server, image.full_url),
7680_000,
&mut download_buffer
).expect("Failed to retrieve image data");