1 import { remove } from 'fs-extra'
2 import { join } from 'path'
3 import { AfterDestroy, AllowNull, Column, CreatedAt, Default, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
4 import { MActorImageFormattable } from '@server/types/models'
5 import { ActorImageType } from '@shared/models'
6 import { ActorImage } from '../../../shared/models/actors/actor-image.model'
7 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
8 import { logger } from '../../helpers/logger'
9 import { CONFIG } from '../../initializers/config'
10 import { LAZY_STATIC_PATHS } from '../../initializers/constants'
11 import { throwIfNotValid } from '../utils'
14 tableName: 'actorImage',
17 fields: [ 'filename' ],
22 export class ActorImageModel extends Model {
39 @Is('ActorImageFileUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'fileUrl', true))
58 static removeFilesAndSendDelete (instance: ActorImageModel) {
59 logger.info('Removing actor image file %s.', instance.filename)
61 // Don't block the transaction
62 instance.removeImage()
63 .catch(err => logger.error('Cannot remove actor image file %s.', instance.filename, err))
66 static loadByName (filename: string) {
73 return ActorImageModel.findOne(query)
76 toFormattedJSON (this: MActorImageFormattable): ActorImage {
78 path: this.getStaticPath(),
79 createdAt: this.createdAt,
80 updatedAt: this.updatedAt
85 if (this.type === ActorImageType.AVATAR) {
86 return join(LAZY_STATIC_PATHS.AVATARS, this.filename)
89 return join(LAZY_STATIC_PATHS.BANNERS, this.filename)
93 return join(CONFIG.STORAGE.ACTOR_IMAGES, this.filename)
97 const imagePath = join(CONFIG.STORAGE.ACTOR_IMAGES, this.filename)
98 return remove(imagePath)