Skip to content

Commit a5f949c

Browse files
r10shpk42
authored andcommitted
recode group- and user-avatar to 192x192 pixel
1 parent 9fc5568 commit a5f949c

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

src/blob.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ use std::fs;
66
use std::io::Write;
77
use std::path::{Path, PathBuf};
88

9+
use self::image::GenericImageView;
10+
use crate::constants::AVATAR_SIZE;
911
use crate::context::Context;
1012
use crate::events::Event;
1113

14+
extern crate image;
15+
1216
/// Represents a file in the blob directory.
1317
///
1418
/// The object has a name, which will always be valid UTF-8. Having a
@@ -349,6 +353,31 @@ impl<'a> BlobObject<'a> {
349353
}
350354
true
351355
}
356+
357+
pub fn recode_to_avatar_size(&self, context: &Context) -> Result<(), BlobError> {
358+
let blob_abs = self.to_abs_path();
359+
let img = image::open(&blob_abs).map_err(|err| BlobError::RecodeFailure {
360+
blobdir: context.get_blobdir().to_path_buf(),
361+
blobname: blob_abs.to_str().unwrap_or_default().to_string(),
362+
cause: err,
363+
backtrace: failure::Backtrace::new(),
364+
})?;
365+
366+
if img.width() <= AVATAR_SIZE && img.height() <= AVATAR_SIZE {
367+
return Ok(());
368+
}
369+
370+
let img = img.thumbnail(AVATAR_SIZE, AVATAR_SIZE);
371+
372+
img.save(&blob_abs).map_err(|err| BlobError::WriteFailure {
373+
blobdir: context.get_blobdir().to_path_buf(),
374+
blobname: blob_abs.to_str().unwrap_or_default().to_string(),
375+
cause: err,
376+
backtrace: failure::Backtrace::new(),
377+
})?;
378+
379+
Ok(())
380+
}
352381
}
353382

354383
impl<'a> fmt::Display for BlobObject<'a> {
@@ -382,6 +411,13 @@ pub enum BlobError {
382411
cause: std::io::Error,
383412
backtrace: failure::Backtrace,
384413
},
414+
RecodeFailure {
415+
blobdir: PathBuf,
416+
blobname: String,
417+
#[cause]
418+
cause: image::ImageError,
419+
backtrace: failure::Backtrace,
420+
},
385421
WrongBlobdir {
386422
blobdir: PathBuf,
387423
src: PathBuf,
@@ -429,6 +465,9 @@ impl fmt::Display for BlobError {
429465
blobname,
430466
blobdir.display(),
431467
),
468+
BlobError::RecodeFailure {
469+
blobdir, blobname, ..
470+
} => write!(f, "Failed to recode {} in {}", blobname, blobdir.display(),),
432471
BlobError::WrongBlobdir { blobdir, src, .. } => write!(
433472
f,
434473
"File path {} is not in blobdir {}",

src/chat.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1883,6 +1883,7 @@ pub fn set_chat_profile_image(
18831883
_ => Err(err),
18841884
},
18851885
)?;
1886+
image_blob.recode_to_avatar_size(context)?;
18861887
chat.param.set(Param::ProfileImage, image_blob.as_name());
18871888
msg.param.set(Param::Arg, image_blob.as_name());
18881889
msg.text = Some(context.stock_system_msg(

src/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ impl Context {
136136
match value {
137137
Some(value) => {
138138
let blob = BlobObject::new_from_path(&self, value)?;
139+
blob.recode_to_avatar_size(self)?;
139140
self.sql.set_raw_config(self, key, Some(blob.as_name()))
140141
}
141142
None => self.sql.set_raw_config(self, key, None),

src/constants.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ pub const DC_VC_CONTACT_CONFIRM: i32 = 6;
184184
pub const DC_BOB_ERROR: i32 = 0;
185185
pub const DC_BOB_SUCCESS: i32 = 1;
186186

187+
// max. width/height of an avatar
188+
pub const AVATAR_SIZE: u32 = 192;
189+
187190
#[derive(Debug, Display, Clone, Copy, PartialEq, Eq, FromPrimitive, ToPrimitive, FromSql, ToSql)]
188191
#[repr(i32)]
189192
pub enum Viewtype {

0 commit comments

Comments
 (0)