mirror of
https://github.com/chatmail/core.git
synced 2026-05-04 22:06:29 +03:00
recode group- and user-avatar to 192x192 pixel
This commit is contained in:
committed by
holger krekel
parent
9fc556864e
commit
a5f949c4e2
39
src/blob.rs
39
src/blob.rs
@@ -6,9 +6,13 @@ use std::fs;
|
|||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
|
use self::image::GenericImageView;
|
||||||
|
use crate::constants::AVATAR_SIZE;
|
||||||
use crate::context::Context;
|
use crate::context::Context;
|
||||||
use crate::events::Event;
|
use crate::events::Event;
|
||||||
|
|
||||||
|
extern crate image;
|
||||||
|
|
||||||
/// Represents a file in the blob directory.
|
/// Represents a file in the blob directory.
|
||||||
///
|
///
|
||||||
/// The object has a name, which will always be valid UTF-8. Having a
|
/// The object has a name, which will always be valid UTF-8. Having a
|
||||||
@@ -349,6 +353,31 @@ impl<'a> BlobObject<'a> {
|
|||||||
}
|
}
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn recode_to_avatar_size(&self, context: &Context) -> Result<(), BlobError> {
|
||||||
|
let blob_abs = self.to_abs_path();
|
||||||
|
let img = image::open(&blob_abs).map_err(|err| BlobError::RecodeFailure {
|
||||||
|
blobdir: context.get_blobdir().to_path_buf(),
|
||||||
|
blobname: blob_abs.to_str().unwrap_or_default().to_string(),
|
||||||
|
cause: err,
|
||||||
|
backtrace: failure::Backtrace::new(),
|
||||||
|
})?;
|
||||||
|
|
||||||
|
if img.width() <= AVATAR_SIZE && img.height() <= AVATAR_SIZE {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
let img = img.thumbnail(AVATAR_SIZE, AVATAR_SIZE);
|
||||||
|
|
||||||
|
img.save(&blob_abs).map_err(|err| BlobError::WriteFailure {
|
||||||
|
blobdir: context.get_blobdir().to_path_buf(),
|
||||||
|
blobname: blob_abs.to_str().unwrap_or_default().to_string(),
|
||||||
|
cause: err,
|
||||||
|
backtrace: failure::Backtrace::new(),
|
||||||
|
})?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> fmt::Display for BlobObject<'a> {
|
impl<'a> fmt::Display for BlobObject<'a> {
|
||||||
@@ -382,6 +411,13 @@ pub enum BlobError {
|
|||||||
cause: std::io::Error,
|
cause: std::io::Error,
|
||||||
backtrace: failure::Backtrace,
|
backtrace: failure::Backtrace,
|
||||||
},
|
},
|
||||||
|
RecodeFailure {
|
||||||
|
blobdir: PathBuf,
|
||||||
|
blobname: String,
|
||||||
|
#[cause]
|
||||||
|
cause: image::ImageError,
|
||||||
|
backtrace: failure::Backtrace,
|
||||||
|
},
|
||||||
WrongBlobdir {
|
WrongBlobdir {
|
||||||
blobdir: PathBuf,
|
blobdir: PathBuf,
|
||||||
src: PathBuf,
|
src: PathBuf,
|
||||||
@@ -429,6 +465,9 @@ impl fmt::Display for BlobError {
|
|||||||
blobname,
|
blobname,
|
||||||
blobdir.display(),
|
blobdir.display(),
|
||||||
),
|
),
|
||||||
|
BlobError::RecodeFailure {
|
||||||
|
blobdir, blobname, ..
|
||||||
|
} => write!(f, "Failed to recode {} in {}", blobname, blobdir.display(),),
|
||||||
BlobError::WrongBlobdir { blobdir, src, .. } => write!(
|
BlobError::WrongBlobdir { blobdir, src, .. } => write!(
|
||||||
f,
|
f,
|
||||||
"File path {} is not in blobdir {}",
|
"File path {} is not in blobdir {}",
|
||||||
|
|||||||
@@ -1883,6 +1883,7 @@ pub fn set_chat_profile_image(
|
|||||||
_ => Err(err),
|
_ => Err(err),
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
|
image_blob.recode_to_avatar_size(context)?;
|
||||||
chat.param.set(Param::ProfileImage, image_blob.as_name());
|
chat.param.set(Param::ProfileImage, image_blob.as_name());
|
||||||
msg.param.set(Param::Arg, image_blob.as_name());
|
msg.param.set(Param::Arg, image_blob.as_name());
|
||||||
msg.text = Some(context.stock_system_msg(
|
msg.text = Some(context.stock_system_msg(
|
||||||
|
|||||||
@@ -136,6 +136,7 @@ impl Context {
|
|||||||
match value {
|
match value {
|
||||||
Some(value) => {
|
Some(value) => {
|
||||||
let blob = BlobObject::new_from_path(&self, value)?;
|
let blob = BlobObject::new_from_path(&self, value)?;
|
||||||
|
blob.recode_to_avatar_size(self)?;
|
||||||
self.sql.set_raw_config(self, key, Some(blob.as_name()))
|
self.sql.set_raw_config(self, key, Some(blob.as_name()))
|
||||||
}
|
}
|
||||||
None => self.sql.set_raw_config(self, key, None),
|
None => self.sql.set_raw_config(self, key, None),
|
||||||
|
|||||||
@@ -184,6 +184,9 @@ pub const DC_VC_CONTACT_CONFIRM: i32 = 6;
|
|||||||
pub const DC_BOB_ERROR: i32 = 0;
|
pub const DC_BOB_ERROR: i32 = 0;
|
||||||
pub const DC_BOB_SUCCESS: i32 = 1;
|
pub const DC_BOB_SUCCESS: i32 = 1;
|
||||||
|
|
||||||
|
// max. width/height of an avatar
|
||||||
|
pub const AVATAR_SIZE: u32 = 192;
|
||||||
|
|
||||||
#[derive(Debug, Display, Clone, Copy, PartialEq, Eq, FromPrimitive, ToPrimitive, FromSql, ToSql)]
|
#[derive(Debug, Display, Clone, Copy, PartialEq, Eq, FromPrimitive, ToPrimitive, FromSql, ToSql)]
|
||||||
#[repr(i32)]
|
#[repr(i32)]
|
||||||
pub enum Viewtype {
|
pub enum Viewtype {
|
||||||
|
|||||||
Reference in New Issue
Block a user