PeerTube/server/models/avatar/avatar.ts

44 lines
1012 B
TypeScript
Raw Normal View History

2017-12-29 12:10:13 -06: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 { unlinkPromise } from '../../helpers/core-utils'
import { CONFIG, STATIC_PATHS } from '../../initializers'
2017-12-04 03:34:40 -06:00
2017-12-12 10:53:50 -06:00
@Table({
tableName: 'avatar'
})
export class AvatarModel extends Model<AvatarModel> {
2017-12-04 03:34:40 -06:00
2017-12-12 10:53:50 -06:00
@AllowNull(false)
@Column
filename: string
2017-12-04 03:34:40 -06:00
2017-12-12 10:53:50 -06:00
@CreatedAt
createdAt: Date
2017-12-04 03:34:40 -06:00
2017-12-12 10:53:50 -06:00
@UpdatedAt
updatedAt: Date
2017-12-29 12:10:13 -06:00
@AfterDestroy
static removeFilesAndSendDelete (instance: AvatarModel) {
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)
return unlinkPromise(avatarPath)
}
2017-12-04 03:34:40 -06:00
}