1
0
Fork 0
peertube/server/models/avatar/avatar.ts

50 lines
1.2 KiB
TypeScript
Raw Normal View History

2017-12-29 18:10:13 +00:00
import { join } from 'path'
import { AfterDestroy, AllowNull, Column, CreatedAt, Model, Table, UpdatedAt } from 'sequelize-typescript'
import { Avatar } from '../../../shared/models/avatars/avatar.model'
import { STATIC_PATHS } from '../../initializers/constants'
2018-07-30 15:02:40 +00:00
import { logger } from '../../helpers/logger'
2018-08-27 14:23:34 +00:00
import { remove } from 'fs-extra'
2019-04-11 09:33:44 +00:00
import { CONFIG } from '../../initializers/config'
2017-12-04 09:34:40 +00:00
2017-12-12 16:53:50 +00:00
@Table({
tableName: 'avatar'
})
export class AvatarModel extends Model<AvatarModel> {
2017-12-04 09:34:40 +00:00
2017-12-12 16:53:50 +00:00
@AllowNull(false)
@Column
filename: string
2017-12-04 09:34:40 +00:00
2017-12-12 16:53:50 +00:00
@CreatedAt
createdAt: Date
2017-12-04 09:34:40 +00:00
2017-12-12 16:53:50 +00:00
@UpdatedAt
updatedAt: Date
2017-12-29 18:10:13 +00:00
@AfterDestroy
static removeFilesAndSendDelete (instance: AvatarModel) {
2018-07-30 15:02:40 +00:00
logger.info('Removing avatar file %s.', instance.filename)
2018-10-08 08:37:08 +00:00
// Don't block the transaction
instance.removeAvatar()
.catch(err => logger.error('Cannot remove avatar file %s.', instance.filename, err))
2017-12-29 18:10:13 +00:00
}
toFormattedJSON (): Avatar {
return {
path: this.getWebserverPath(),
createdAt: this.createdAt,
updatedAt: this.updatedAt
}
}
getWebserverPath () {
return join(STATIC_PATHS.AVATARS, this.filename)
}
removeAvatar () {
const avatarPath = join(CONFIG.STORAGE.AVATARS_DIR, this.filename)
2018-08-27 14:23:34 +00:00
return remove(avatarPath)
2017-12-29 18:10:13 +00:00
}
2017-12-04 09:34:40 +00:00
}