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'
15 fields: [ 'filename' ],
20 export class AvatarModel extends Model<AvatarModel> {
27 @Is('AvatarFileUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'fileUrl', true))
42 static removeFilesAndSendDelete (instance: AvatarModel) {
43 logger.info('Removing avatar file %s.', instance.filename)
45 // Don't block the transaction
46 instance.removeAvatar()
47 .catch(err => logger.error('Cannot remove avatar file %s.', instance.filename, err))
50 static loadByName (filename: string) {
57 return AvatarModel.findOne(query)
60 toFormattedJSON (): Avatar {
62 path: this.getStaticPath(),
63 createdAt: this.createdAt,
64 updatedAt: this.updatedAt
69 return join(LAZY_STATIC_PATHS.AVATARS, this.filename)
73 return join(CONFIG.STORAGE.AVATARS_DIR, this.filename)
77 const avatarPath = join(CONFIG.STORAGE.AVATARS_DIR, this.filename)
78 return remove(avatarPath)