mirror of
https://github.com/chatmail/core.git
synced 2026-05-19 06:46:32 +03:00
use split off image recoding function for avatars
This commit is contained in:
23
src/blob.rs
23
src/blob.rs
@@ -377,27 +377,10 @@ impl<'a> BlobObject<'a> {
|
|||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn recode_to_avatar_size(&self, context: &Context) -> Result<(), BlobError> {
|
pub async fn recode_to_avatar_size(&self, context: &Context) -> Result<(), BlobError> {
|
||||||
let blob_abs = self.to_abs_path();
|
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,
|
|
||||||
})?;
|
|
||||||
|
|
||||||
if img.width() <= AVATAR_SIZE && img.height() <= AVATAR_SIZE {
|
self.recode_to_size(context, blob_abs, AVATAR_SIZE).await
|
||||||
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.into(),
|
|
||||||
})?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn recode_to_image_size(&self, context: &Context) -> Result<(), BlobError> {
|
pub async fn recode_to_image_size(&self, context: &Context) -> Result<(), BlobError> {
|
||||||
@@ -459,7 +442,7 @@ impl<'a> BlobObject<'a> {
|
|||||||
cause: err.into(),
|
cause: err.into(),
|
||||||
})?;
|
})?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2609,7 +2609,7 @@ pub async fn set_chat_profile_image(
|
|||||||
_ => Err(err),
|
_ => Err(err),
|
||||||
},
|
},
|
||||||
}?;
|
}?;
|
||||||
image_blob.recode_to_avatar_size(context)?;
|
image_blob.recode_to_avatar_size(context).await?;
|
||||||
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(
|
msg.text = Some(
|
||||||
|
|||||||
@@ -217,7 +217,7 @@ impl Context {
|
|||||||
match value {
|
match value {
|
||||||
Some(value) => {
|
Some(value) => {
|
||||||
let blob = BlobObject::new_from_path(&self, value).await?;
|
let blob = BlobObject::new_from_path(&self, value).await?;
|
||||||
blob.recode_to_avatar_size(self)?;
|
blob.recode_to_avatar_size(self).await?;
|
||||||
self.sql
|
self.sql
|
||||||
.set_raw_config(self, key, Some(blob.as_name()))
|
.set_raw_config(self, key, Some(blob.as_name()))
|
||||||
.await
|
.await
|
||||||
|
|||||||
Reference in New Issue
Block a user