Upload width and height of image with other data

This commit is contained in:
Emi Simpson 2022-11-09 17:45:21 -05:00
parent dd2748cbe0
commit faa8776740
Signed by: Emi
GPG key ID: A12F2C2FFDC3D847
3 changed files with 15 additions and 10 deletions

View file

@ -14,4 +14,6 @@ message Image {
string thumb_url = 3; string thumb_url = 3;
string blurhash = 4; string blurhash = 4;
Format format = 5; Format format = 5;
uint32 width = 6;
uint32 height = 7;
} }

View file

@ -96,16 +96,17 @@ fn create(server: &str, args: CreateArgs) {
.inspect(|r| if r.is_ok() { print!("\x1b[32mDone!\n\x1b[37m├─\x1b[0m Thumbnailing... ") }) .inspect(|r| if r.is_ok() { print!("\x1b[32mDone!\n\x1b[37m├─\x1b[0m Thumbnailing... ") })
.inspect(|_| drop(stdout().flush())) .inspect(|_| drop(stdout().flush()))
.map(|r| r.and_then(|(path, raw_dat)| { .map(|r| r.and_then(|(path, raw_dat)| {
let (full, thumbnail, blurhash, format) = thumbnailing::thumbnail(&raw_dat) let (full, w, h, thumbnail, blurhash, format) = thumbnailing::thumbnail(&raw_dat)
.map_err(|_| AviaryUploadError::ImageFormatError(path.to_owned()))?; .map_err(|_| AviaryUploadError::ImageFormatError(path.to_owned()))?;
Ok((full.map(Either::Right).unwrap_or(Either::Left(raw_dat)), thumbnail, blurhash, format)) Ok((full.map(Either::Right).unwrap_or(Either::Left(raw_dat)), w, h, thumbnail, blurhash, format))
})) }))
.inspect(|r| if r.is_ok() { print!("\x1b[32mDone!\n\x1b[37m├─\x1b[0m Encrypting... ")}) .inspect(|r| if r.is_ok() { print!("\x1b[32mDone!\n\x1b[37m├─\x1b[0m Encrypting... ")})
.inspect(|_| drop(stdout().flush())) .inspect(|_| drop(stdout().flush()))
.map_ok(|(raw_dat, thumbnail, blurhash, format)| { .map_ok(|(raw_dat, w, h, thumbnail, blurhash, format)| {
let key = crypto::make_key(); let key = crypto::make_key();
( (
key, key,
w, h,
crypto::encrypt(&key, &crypto::NONCE_A, &raw_dat), crypto::encrypt(&key, &crypto::NONCE_A, &raw_dat),
crypto::encrypt(&key, &crypto::NONCE_B, &thumbnail), crypto::encrypt(&key, &crypto::NONCE_B, &thumbnail),
blurhash, blurhash,
@ -114,27 +115,27 @@ 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, format)| .map(|r| r.and_then(|(key, w, h, full_img, thumb, blurhash, format)|
upload::put_data(&agent, server, &thumb) upload::put_data(&agent, server, &thumb)
.and_then(|thumb_url| .and_then(|thumb_url|
upload::put_data(&agent, server, &full_img) upload::put_data(&agent, server, &full_img)
.map(|full_url| (key, full_url, thumb_url, blurhash, format))) .map(|full_url| (key, w, h, full_url, thumb_url, blurhash, format)))
.map_err(AviaryUploadError::from_upload_error) .map_err(AviaryUploadError::from_upload_error)
)) ))
.map(|r| r.and_then(|(key, full_url, thumb_url, blurhash, format)| { .map(|r| r.and_then(|(key, w, h, full_url, thumb_url, blurhash, format)| {
let full_trimmed = trim_url(server, &full_url); let full_trimmed = trim_url(server, &full_url);
let thmb_trimmed = trim_url(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, format)) Ok((key, w, h, full_url.to_owned(), thmb_url.to_owned(), blurhash, format))
} else { } else {
Err(AviaryUploadError::ServerError(format!("Received bad response from server: {}", full_url))) Err(AviaryUploadError::ServerError(format!("Received bad response from server: {}", full_url)))
} }
})) }))
.inspect(|r| if r.is_ok() { println!("\x1b[32mDone!\n")}) .inspect(|r| if r.is_ok() { println!("\x1b[32mDone!\n")})
.map_ok(|(key, full_url, thumb_url, blurhash, format)| protobuf::image::Image { .map_ok(|(key, width, height, full_url, thumb_url, blurhash, format)| protobuf::image::Image {
key: key.into(), key: key.into(),
format: format.into(), format: format.into(),
full_url, thumb_url, blurhash, width, height, full_url, thumb_url, blurhash,
special_fields: Default::default() special_fields: Default::default()
}) })
.collect::<Result<Vec<_>, _>>() .collect::<Result<Vec<_>, _>>()

View file

@ -4,7 +4,7 @@ use webp::{Encoder, WebPMemory};
use crate::protobuf::image::Format; use crate::protobuf::image::Format;
pub fn thumbnail(bytes: &[u8]) -> ImageResult<(Option<WebPMemory>, WebPMemory, String, Format)> { pub fn thumbnail(bytes: &[u8]) -> ImageResult<(Option<WebPMemory>, u32, u32, WebPMemory, String, Format)> {
let original_image_encoded = Reader::new(Cursor::new(bytes)) let original_image_encoded = Reader::new(Cursor::new(bytes))
.with_guessed_format().expect("IO errors impossible with Cursor"); .with_guessed_format().expect("IO errors impossible with Cursor");
let original_format = original_image_encoded.format(); let original_format = original_image_encoded.format();
@ -41,6 +41,8 @@ pub fn thumbnail(bytes: &[u8]) -> ImageResult<(Option<WebPMemory>, WebPMemory, S
Ok(( Ok((
recoded_original, recoded_original,
original_image.width(),
original_image.height(),
Encoder::from_image(&DynamicImage::ImageRgba8(scaled)) Encoder::from_image(&DynamicImage::ImageRgba8(scaled))
.expect("Unexpected difficulty interpretting thumbnail") .expect("Unexpected difficulty interpretting thumbnail")
.encode(50.0), .encode(50.0),