1 import { join } from 'path'
2 import { AfterDestroy, AllowNull, Column, CreatedAt, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
3 import { Avatar } from '../../../shared/models/avatars/avatar.model'
4 import { LAZY_STATIC_PATHS } from '../../initializers/constants'
5 import { logger } from '../../helpers/logger'
6 import { remove } from 'fs-extra'
7 import { CONFIG } from '../../initializers/config'
8 import { throwIfNotValid } from '../utils'
9 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
10 import { MAvatarFormattable } from '@server/types/models'
16 fields: [ 'filename' ],
21 export class AvatarModel extends Model<AvatarModel> {
28 @Is('AvatarFileUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'fileUrl', true))
43 static removeFilesAndSendDelete (instance: AvatarModel) {
44 logger.info('Removing avatar file %s.', instance.filename)
46 // Don't block the transaction
47 instance.removeAvatar()
48 .catch(err => logger.error('Cannot remove avatar file %s.', instance.filename, err))
51 static loadByName (filename: string) {
58 return AvatarModel.findOne(query)
61 toFormattedJSON (this: MAvatarFormattable): Avatar {
63 path: this.getStaticPath(),
64 createdAt: this.createdAt,
65 updatedAt: this.updatedAt
70 return join(LAZY_STATIC_PATHS.AVATARS, this.filename)
74 return join(CONFIG.STORAGE.AVATARS_DIR, this.filename)
78 const avatarPath = join(CONFIG.STORAGE.AVATARS_DIR, this.filename)
79 return remove(avatarPath)