Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions src/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,19 +409,19 @@ impl<'a> BlobObject<'a> {
// images.
let do_scale = exceeds_max_bytes
|| is_avatar
&& (exceeds_wh
|| exif.is_some() && {
if mem::take(&mut add_white_bg) {
self::add_white_bg(&mut img);
}
encoded_img_exceeds_bytes(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

encoded_img_exceeds_bytes() already recodes the image, that's why it takes &mut encoded, this encoded is then passed to BlobObject::create_and_deduplicate_from_bytes(). Maybe rename it to just encode_img_ex() to avoid any wrong assumptions about what it does? Or, if i'm wrong, adding a test for the scenario being fixed will help, w/o tests we always get regressions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function does add a file-size-check to encode_img, that returns false or true.

core/src/blob.rs

Lines 653 to 673 in f43b3b9

fn encoded_img_exceeds_bytes(
context: &Context,
img: &DynamicImage,
fmt: ImageOutputFormat,
max_bytes: usize,
encoded: &mut Vec<u8>,
) -> anyhow::Result<bool> {
encode_img(img, fmt, encoded)?;
if encoded.len() > max_bytes {
info!(
context,
"Image size {}B ({}x{}px) exceeds {}B, need to scale down.",
encoded.len(),
img.width(),
img.height(),
max_bytes,
);
return Ok(true);
}
Ok(false)
}

So, i think the name makes sense as it is.

I added a comment that does explain that it actually changes the image (in the resizing-loop, where it is primarily used) to make that easier to notice, and added another comment explaining why the image will be encoded here.

context,
&img,
ofmt.clone(),
max_bytes,
&mut encoded,
)?
});
&& (exceeds_wh || exif.is_some() || {
// Check if the file-size of the avatar would be too large with a white background added to it.
if mem::take(&mut add_white_bg) {
self::add_white_bg(&mut img);
}
encoded_img_exceeds_bytes(
context,
&img,
ofmt.clone(),
max_bytes,
&mut encoded,
)?
});

if do_scale {
loop {
Expand All @@ -443,6 +443,7 @@ impl<'a> BlobObject<'a> {
img.thumbnail(img_wh, img_wh)
};

// This also encodes the image for use, not only for checking the file-size.
if encoded_img_exceeds_bytes(
context,
&new_img,
Expand Down
Loading