-type AvatarInfo = { name: string, onDisk: boolean, fileUrl: string }
-async function updateActorAvatarInstance (actor: MActorDefault, info: AvatarInfo, t: Transaction) {
- if (info.name !== undefined) {
- if (actor.avatarId) {
- try {
- await actor.Avatar.destroy({ transaction: t })
- } catch (err) {
- logger.error('Cannot remove old avatar of actor %s.', actor.url, { err })
- }
+type ImageInfo = {
+ name: string
+ fileUrl: string
+ height: number
+ width: number
+ onDisk?: boolean
+}
+async function updateActorImageInstance (actor: MActorImages, type: ActorImageType, imageInfo: ImageInfo | null, t: Transaction) {
+ const oldImageModel = type === ActorImageType.AVATAR
+ ? actor.Avatar
+ : actor.Banner
+
+ if (oldImageModel) {
+ // Don't update the avatar if the file URL did not change
+ if (imageInfo?.fileUrl && oldImageModel.fileUrl === imageInfo.fileUrl) return actor
+
+ try {
+ await oldImageModel.destroy({ transaction: t })
+
+ setActorImage(actor, type, null)
+ } catch (err) {
+ logger.error('Cannot remove old actor image of actor %s.', actor.url, { err })