mirror of
https://github.com/chatmail/core.git
synced 2026-05-01 20:36:31 +03:00
fix: Don't remove file extension when recoding avatars
There was a bug that file extensions were removed when recoding an avatar. The problem was that `recode_avatar` used `name` to check for the extension, but some functions passed an empty string. There even were two tests from before the decision to keep the extensions that tested for the faulty behavior.
This commit is contained in:
34
src/blob.rs
34
src/blob.rs
@@ -439,7 +439,7 @@ impl<'a> BlobObject<'a> {
|
|||||||
// 32 / 4 * 3 = 24k if you account for base64 encoding. To be safe, we reduced this to 20k.
|
// 32 / 4 * 3 = 24k if you account for base64 encoding. To be safe, we reduced this to 20k.
|
||||||
self.recode_to_size(
|
self.recode_to_size(
|
||||||
context,
|
context,
|
||||||
"".to_string(), // The name of an avatar doesn't matter
|
None, // The name of an avatar doesn't matter
|
||||||
maybe_sticker,
|
maybe_sticker,
|
||||||
img_wh,
|
img_wh,
|
||||||
20_000,
|
20_000,
|
||||||
@@ -459,7 +459,7 @@ impl<'a> BlobObject<'a> {
|
|||||||
pub async fn recode_to_image_size(
|
pub async fn recode_to_image_size(
|
||||||
&mut self,
|
&mut self,
|
||||||
context: &Context,
|
context: &Context,
|
||||||
name: String,
|
name: Option<String>,
|
||||||
maybe_sticker: &mut bool,
|
maybe_sticker: &mut bool,
|
||||||
) -> Result<String> {
|
) -> Result<String> {
|
||||||
let (img_wh, max_bytes) =
|
let (img_wh, max_bytes) =
|
||||||
@@ -494,11 +494,10 @@ impl<'a> BlobObject<'a> {
|
|||||||
/// then the updated user-visible filename will be returned;
|
/// then the updated user-visible filename will be returned;
|
||||||
/// this may be necessary because the format may be changed to JPG,
|
/// this may be necessary because the format may be changed to JPG,
|
||||||
/// i.e. "image.png" -> "image.jpg".
|
/// i.e. "image.png" -> "image.jpg".
|
||||||
/// Pass an empty string if you don't care.
|
|
||||||
fn recode_to_size(
|
fn recode_to_size(
|
||||||
&mut self,
|
&mut self,
|
||||||
context: &Context,
|
context: &Context,
|
||||||
mut name: String,
|
name: Option<String>,
|
||||||
maybe_sticker: &mut bool,
|
maybe_sticker: &mut bool,
|
||||||
mut img_wh: u32,
|
mut img_wh: u32,
|
||||||
max_bytes: usize,
|
max_bytes: usize,
|
||||||
@@ -508,6 +507,7 @@ impl<'a> BlobObject<'a> {
|
|||||||
let mut add_white_bg = img_wh <= constants::BALANCED_AVATAR_SIZE;
|
let mut add_white_bg = img_wh <= constants::BALANCED_AVATAR_SIZE;
|
||||||
let mut no_exif = false;
|
let mut no_exif = false;
|
||||||
let no_exif_ref = &mut no_exif;
|
let no_exif_ref = &mut no_exif;
|
||||||
|
let mut name = name.unwrap_or_else(|| self.name.clone());
|
||||||
let original_name = name.clone();
|
let original_name = name.clone();
|
||||||
let res: Result<String> = tokio::task::block_in_place(move || {
|
let res: Result<String> = tokio::task::block_in_place(move || {
|
||||||
let mut file = std::fs::File::open(self.to_abs_path())?;
|
let mut file = std::fs::File::open(self.to_abs_path())?;
|
||||||
@@ -1103,15 +1103,8 @@ mod tests {
|
|||||||
let img_wh = 128;
|
let img_wh = 128;
|
||||||
let maybe_sticker = &mut false;
|
let maybe_sticker = &mut false;
|
||||||
let strict_limits = true;
|
let strict_limits = true;
|
||||||
blob.recode_to_size(
|
blob.recode_to_size(&t, None, maybe_sticker, img_wh, 20_000, strict_limits)
|
||||||
&t,
|
.unwrap();
|
||||||
"avatar.png".to_string(),
|
|
||||||
maybe_sticker,
|
|
||||||
img_wh,
|
|
||||||
20_000,
|
|
||||||
strict_limits,
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
tokio::task::block_in_place(move || {
|
tokio::task::block_in_place(move || {
|
||||||
let img = ImageReader::open(blob.to_abs_path())
|
let img = ImageReader::open(blob.to_abs_path())
|
||||||
.unwrap()
|
.unwrap()
|
||||||
@@ -1142,7 +1135,7 @@ mod tests {
|
|||||||
let avatar_blob = t.get_config(Config::Selfavatar).await.unwrap().unwrap();
|
let avatar_blob = t.get_config(Config::Selfavatar).await.unwrap().unwrap();
|
||||||
let avatar_path = Path::new(&avatar_blob);
|
let avatar_path = Path::new(&avatar_blob);
|
||||||
assert!(
|
assert!(
|
||||||
avatar_blob.ends_with("d98cd30ed8f2129bf3968420208849d"),
|
avatar_blob.ends_with("d98cd30ed8f2129bf3968420208849d.jpg"),
|
||||||
"The avatar filename should be its hash, put instead it's {avatar_blob}"
|
"The avatar filename should be its hash, put instead it's {avatar_blob}"
|
||||||
);
|
);
|
||||||
let scaled_avatar_size = file_size(avatar_path).await;
|
let scaled_avatar_size = file_size(avatar_path).await;
|
||||||
@@ -1158,15 +1151,8 @@ mod tests {
|
|||||||
let mut blob = BlobObject::new_from_path(&t, avatar_path).await.unwrap();
|
let mut blob = BlobObject::new_from_path(&t, avatar_path).await.unwrap();
|
||||||
let maybe_sticker = &mut false;
|
let maybe_sticker = &mut false;
|
||||||
let strict_limits = true;
|
let strict_limits = true;
|
||||||
blob.recode_to_size(
|
blob.recode_to_size(&t, None, maybe_sticker, 1000, 3000, strict_limits)
|
||||||
&t,
|
.unwrap();
|
||||||
"avatar.jpg".to_string(),
|
|
||||||
maybe_sticker,
|
|
||||||
1000,
|
|
||||||
3000,
|
|
||||||
strict_limits,
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
let new_file_size = file_size(&blob.to_abs_path()).await;
|
let new_file_size = file_size(&blob.to_abs_path()).await;
|
||||||
assert!(new_file_size <= 3000);
|
assert!(new_file_size <= 3000);
|
||||||
assert!(new_file_size > 2000);
|
assert!(new_file_size > 2000);
|
||||||
@@ -1201,7 +1187,7 @@ mod tests {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
let avatar_cfg = t.get_config(Config::Selfavatar).await.unwrap().unwrap();
|
let avatar_cfg = t.get_config(Config::Selfavatar).await.unwrap().unwrap();
|
||||||
assert!(
|
assert!(
|
||||||
avatar_cfg.ends_with("9e7f409ac5c92b942cc4f31cee2770a"),
|
avatar_cfg.ends_with("9e7f409ac5c92b942cc4f31cee2770a.png"),
|
||||||
"Avatar file name {avatar_cfg} should end with its hash"
|
"Avatar file name {avatar_cfg} should end with its hash"
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -2782,11 +2782,7 @@ async fn prepare_msg_blob(context: &Context, msg: &mut Message) -> Result<()> {
|
|||||||
|| maybe_sticker && !msg.param.exists(Param::ForceSticker))
|
|| maybe_sticker && !msg.param.exists(Param::ForceSticker))
|
||||||
{
|
{
|
||||||
let new_name = blob
|
let new_name = blob
|
||||||
.recode_to_image_size(
|
.recode_to_image_size(context, msg.get_filename(), &mut maybe_sticker)
|
||||||
context,
|
|
||||||
msg.get_filename().unwrap_or_else(|| "file".to_string()),
|
|
||||||
&mut maybe_sticker,
|
|
||||||
)
|
|
||||||
.await?;
|
.await?;
|
||||||
msg.param.set(Param::Filename, new_name);
|
msg.param.set(Param::Filename, new_name);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user