Fix issue with avatar / icon deletion when saving settings.

- Fixes #4763
This commit is contained in:
Dessalines 2024-06-03 17:07:55 -04:00
parent 69b4c6647b
commit 5f340d979d

View file

@ -338,16 +338,18 @@ async fn is_image_content_type(client: &ClientWithMiddleware, url: &Url) -> Lemm
}
}
/// When adding a new avatar or similar image, delete the old one.
/// When adding a new avatar, banner or similar image, delete the old one.
pub async fn replace_image(
new_image: &Option<String>,
old_image: &Option<DbUrl>,
context: &Data<LemmyContext>,
) -> LemmyResult<()> {
if new_image.is_some() {
// Ignore errors because image may be stored externally.
if let Some(avatar) = &old_image {
let image = LocalImage::delete_by_url(&mut context.pool(), avatar)
if let (Some(new_image), Some(old_image)) = (new_image, old_image) {
// Note: Oftentimes front ends will include the current image in the form,
// so only delete if the urls are different.
if new_image != old_image.as_str() {
// Ignore errors because image may be stored externally.
let image = LocalImage::delete_by_url(&mut context.pool(), old_image)
.await
.ok();
if let Some(image) = image {