From e0bfb72ce07c7b3c4dc0cce4188adc45a07464ac Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 16 Sep 2022 14:49:08 +0200 Subject: [PATCH] Cleanup actor image without width --- server/lib/activitypub/actors/image.ts | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/server/lib/activitypub/actors/image.ts b/server/lib/activitypub/actors/image.ts index d17c2ef1a..e1d29af5b 100644 --- a/server/lib/activitypub/actors/image.ts +++ b/server/lib/activitypub/actors/image.ts @@ -13,20 +13,31 @@ type ImageInfo = { } async function updateActorImages (actor: MActorImages, type: ActorImageType, imagesInfo: ImageInfo[], t: Transaction) { - const avatarsOrBanners = type === ActorImageType.AVATAR - ? actor.Avatars - : actor.Banners + const getAvatarsOrBanners = () => { + const result = type === ActorImageType.AVATAR + ? actor.Avatars + : actor.Banners + + return result || [] + } if (imagesInfo.length === 0) { await deleteActorImages(actor, type, t) } + // Cleanup old images that did not have a width + for (const oldImageModel of getAvatarsOrBanners()) { + if (oldImageModel.width) continue + + await safeDeleteActorImage(actor, oldImageModel, type, t) + } + for (const imageInfo of imagesInfo) { - const oldImageModel = (avatarsOrBanners || []).find(i => i.width === imageInfo.width) + const oldImageModel = getAvatarsOrBanners().find(i => imageInfo.width && i.width === imageInfo.width) if (oldImageModel) { // Don't update the avatar if the file URL did not change - if (imageInfo?.fileUrl && oldImageModel.fileUrl === imageInfo.fileUrl) { + if (imageInfo.fileUrl && oldImageModel.fileUrl === imageInfo.fileUrl) { continue }