Compress avatar to below 20k (#2384)

- Currently, group images are compressed as well because it was easier to implement that way.
- Currently, in the unlikely case that the avatar is compressed down to 20x20 pixels but still bigger than 20KB, the user doesn't get any indication of this, the avatar simply isn't changed (at least on Android).

  If we want to change this, the easiest way is probably to let `dc_set_config()` in the ffi call `error!()` if `Selfavatar` can't be set. The same might make sense for some or all other configs. BUUUUUT: At least Android doesn't show error!() toasts anymore, probably because they were used too often and too spammy.
- The factor by which we scale down if the file is too big is 1.5.
This commit is contained in:
Hocuri
2021-05-02 19:54:13 +02:00
committed by GitHub
parent 8e9d8ae1ec
commit b7864f232b
6 changed files with 258 additions and 106 deletions

View File

@@ -11,6 +11,7 @@ use anyhow::Context as _;
use async_std::prelude::*;
use rusqlite::OpenFlags;
use crate::blob::BlobObject;
use crate::chat::{add_device_msg, update_device_icon, update_saved_messages_icon};
use crate::config::Config;
use crate::constants::{Viewtype, DC_CHAT_ID_TRASH};
@@ -138,7 +139,7 @@ impl Sql {
// this should be done before updates that use high-level objects that
// rely themselves on the low-level structure.
let (recalc_fingerprints, update_icons, disable_server_delete) =
let (recalc_fingerprints, update_icons, disable_server_delete, recode_avatar) =
migrations::run(context, self).await?;
// (2) updates that require high-level objects
@@ -183,6 +184,23 @@ impl Sql {
.await?;
}
}
if recode_avatar {
if let Some(avatar) = context.get_config(Config::Selfavatar).await? {
let mut blob = BlobObject::new_from_path(context, &avatar).await?;
match blob.recode_to_avatar_size(context).await {
Ok(()) => {
context
.set_config(Config::Selfavatar, Some(&avatar))
.await?
}
Err(e) => {
warn!(context, "Migrations can't recode avatar, removing. {:#}", e);
context.set_config(Config::Selfavatar, None).await?
}
}
}
}
}
info!(context, "Opened {:?}.", dbfile.as_ref());