From 428d8555b0fbdbeaf7b6dd792ff94b071dd18f62 Mon Sep 17 00:00:00 2001 From: Hocuri Date: Sat, 5 Jun 2021 18:15:31 +0200 Subject: [PATCH 1/4] Add failing test --- src/blob.rs | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/src/blob.rs b/src/blob.rs index c371832611..95159bdb38 100644 --- a/src/blob.rs +++ b/src/blob.rs @@ -458,6 +458,7 @@ impl<'a> BlobObject<'a> { max_bytes: Option, encoded: &mut Vec, ) -> anyhow::Result { + // encode_img(img, encoded)?; // TODO this would solve the other bug, but will be a problem if the image needs to be rotated down but not scaled down if let Some(max_bytes) = max_bytes { encode_img(img, encoded)?; if encoded.len() > max_bytes { @@ -617,7 +618,7 @@ mod tests { use super::*; - use crate::test_utils::TestContext; + use crate::{message::Message, test_utils::TestContext}; #[async_std::test] async fn test_create() { @@ -915,4 +916,49 @@ mod tests { let avatar_cfg = t.get_config(Config::Selfavatar).await.unwrap(); assert_eq!(avatar_cfg, avatar_blob.to_str().map(|s| s.to_string())); } + + #[async_std::test] + async fn test_media_quality() -> anyhow::Result<()> { + for (media_quality_config, image_size) in vec![ + (Some("0"), 1000), // BALANCED_IMAGE_SIZE > 1000, the original image size, so the image is not scaled down + (Some("1"), WORSE_IMAGE_SIZE), + ] + .into_iter() + { + let alice = TestContext::new_alice().await; + let bob = TestContext::new_bob().await; + alice + .set_config(Config::MediaQuality, media_quality_config) + .await?; + + let file = alice.get_blobdir().join("file.jpg"); + let bytes = include_bytes!("../test-data/image/avatar1000x1000.jpg"); + File::create(&file).await?.write_all(bytes).await?; + + let img = image::open(&file)?; + assert_eq!(img.width(), 1000); + assert_eq!(img.height(), 1000); + + let mut msg = Message::new(Viewtype::Image); + msg.set_file(file.to_str().unwrap(), None); + let chat = alice.create_chat(&bob).await; + let sent = alice.send_msg(chat.id, &mut msg).await; + + let alice_msg = alice.get_last_msg().await; + assert_eq!(alice_msg.get_width() as u32, image_size); + assert_eq!(alice_msg.get_height() as u32, image_size); + let img = image::open(alice_msg.get_file(&alice).unwrap())?; + assert_eq!(img.width() as u32, image_size); + assert_eq!(img.height() as u32, image_size); + + bob.recv_msg(&sent).await; + let bob_msg = bob.get_last_msg().await; + assert_eq!(bob_msg.get_width() as u32, image_size); + assert_eq!(bob_msg.get_height() as u32, image_size); + let img = image::open(bob_msg.get_file(&bob).unwrap())?; + assert_eq!(img.width() as u32, image_size); + assert_eq!(img.height() as u32, image_size); + } + Ok(()) + } } From b1974b84e9479215a979aae6d7a4565a235ebd7e Mon Sep 17 00:00:00 2001 From: Hocuri Date: Sat, 5 Jun 2021 18:15:43 +0200 Subject: [PATCH 2/4] typo --- src/blob.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/blob.rs b/src/blob.rs index 95159bdb38..b356093cac 100644 --- a/src/blob.rs +++ b/src/blob.rs @@ -452,7 +452,7 @@ impl<'a> BlobObject<'a> { img.write_to(encoded, image::ImageFormat::Jpeg)?; Ok(()) } - fn encode_img_exceeds_bytes( + fn encoded_img_exceeds_bytes( context: &Context, img: &DynamicImage, max_bytes: Option, @@ -478,7 +478,7 @@ impl<'a> BlobObject<'a> { let exceeds_width = img.width() > img_wh || img.height() > img_wh; let do_scale = - exceeds_width || encode_img_exceeds_bytes(context, &img, max_bytes, &mut encoded)?; + exceeds_width || encoded_img_exceeds_bytes(context, &img, max_bytes, &mut encoded)?; let do_rotate = matches!(orientation, Ok(90) | Ok(180) | Ok(270)); if do_scale || do_rotate { @@ -501,7 +501,7 @@ impl<'a> BlobObject<'a> { loop { let new_img = img.thumbnail(img_wh, img_wh); - if encode_img_exceeds_bytes(context, &new_img, max_bytes, &mut encoded)? { + if encoded_img_exceeds_bytes(context, &new_img, max_bytes, &mut encoded)? { if img_wh < 20 { return Err(format_err!( "Failed to scale image to below {}B", From 4ca41fe43c9ed8a771c6b010e3917e79760c0ab2 Mon Sep 17 00:00:00 2001 From: Hocuri Date: Sat, 5 Jun 2021 18:15:46 +0200 Subject: [PATCH 3/4] Fix the bug --- src/blob.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/blob.rs b/src/blob.rs index b356093cac..8f81a7aa2c 100644 --- a/src/blob.rs +++ b/src/blob.rs @@ -512,6 +512,10 @@ impl<'a> BlobObject<'a> { img_wh = img_wh * 2 / 3; } else { + if encoded.is_empty() { + encode_img(&new_img, &mut encoded)?; + } + info!( context, "Final scaled-down image size: {}B ({}px)", From c183203577d8c0b397a3c6216d80c1f2d3112d06 Mon Sep 17 00:00:00 2001 From: Hocuri Date: Sat, 5 Jun 2021 18:15:59 +0200 Subject: [PATCH 4/4] Remove TODO for now --- src/blob.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/blob.rs b/src/blob.rs index 8f81a7aa2c..d8c7d56cf6 100644 --- a/src/blob.rs +++ b/src/blob.rs @@ -458,7 +458,6 @@ impl<'a> BlobObject<'a> { max_bytes: Option, encoded: &mut Vec, ) -> anyhow::Result { - // encode_img(img, encoded)?; // TODO this would solve the other bug, but will be a problem if the image needs to be rotated down but not scaled down if let Some(max_bytes) = max_bytes { encode_img(img, encoded)?; if encoded.len() > max_bytes {