2017-12-29 21:10:13 +03: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 { CONFIG, STATIC_PATHS } from '../../initializers'
|
2018-07-30 18:02:40 +03:00
|
|
|
import { logger } from '../../helpers/logger'
|
2018-08-27 17:23:34 +03:00
|
|
|
import { remove } from 'fs-extra'
|
2017-12-04 12:34:40 +03:00
|
|
|
|
2017-12-12 19:53:50 +03:00
|
|
|
@Table({
|
|
|
|
tableName: 'avatar'
|
|
|
|
})
|
|
|
|
export class AvatarModel extends Model<AvatarModel> {
|
2017-12-04 12:34:40 +03:00
|
|
|
|
2017-12-12 19:53:50 +03:00
|
|
|
@AllowNull(false)
|
|
|
|
@Column
|
|
|
|
filename: string
|
2017-12-04 12:34:40 +03:00
|
|
|
|
2017-12-12 19:53:50 +03:00
|
|
|
@CreatedAt
|
|
|
|
createdAt: Date
|
2017-12-04 12:34:40 +03:00
|
|
|
|
2017-12-12 19:53:50 +03:00
|
|
|
@UpdatedAt
|
|
|
|
updatedAt: Date
|
2017-12-29 21:10:13 +03:00
|
|
|
|
|
|
|
@AfterDestroy
|
|
|
|
static removeFilesAndSendDelete (instance: AvatarModel) {
|
2018-07-30 18:02:40 +03:00
|
|
|
logger.info('Removing avatar file %s.', instance.filename)
|
2017-12-29 21:10:13 +03:00
|
|
|
return instance.removeAvatar()
|
|
|
|
}
|
|
|
|
|
|
|
|
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 17:23:34 +03:00
|
|
|
return remove(avatarPath)
|
2017-12-29 21:10:13 +03:00
|
|
|
}
|
2017-12-04 12:34:40 +03:00
|
|
|
}
|