]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/avatar/avatar.ts
303aebcc2cc0919f7296e824ad958cc081b0780d
[github/Chocobozzz/PeerTube.git] / server / models / avatar / avatar.ts
1 import { join } from 'path'
2 import { AfterDestroy, AllowNull, Column, CreatedAt, Model, Table, UpdatedAt } from 'sequelize-typescript'
3 import { Avatar } from '../../../shared/models/avatars/avatar.model'
4 import { CONFIG, STATIC_PATHS } from '../../initializers'
5 import { logger } from '../../helpers/logger'
6 import { remove } from 'fs-extra'
7
8 @Table({
9 tableName: 'avatar'
10 })
11 export class AvatarModel extends Model<AvatarModel> {
12
13 @AllowNull(false)
14 @Column
15 filename: string
16
17 @CreatedAt
18 createdAt: Date
19
20 @UpdatedAt
21 updatedAt: Date
22
23 @AfterDestroy
24 static removeFilesAndSendDelete (instance: AvatarModel) {
25 logger.info('Removing avatar file %s.', instance.filename)
26
27 // Don't block the transaction
28 instance.removeAvatar()
29 .catch(err => logger.error('Cannot remove avatar file %s.', instance.filename, err))
30 }
31
32 toFormattedJSON (): Avatar {
33 return {
34 path: this.getWebserverPath(),
35 createdAt: this.createdAt,
36 updatedAt: this.updatedAt
37 }
38 }
39
40 getWebserverPath () {
41 return join(STATIC_PATHS.AVATARS, this.filename)
42 }
43
44 removeAvatar () {
45 const avatarPath = join(CONFIG.STORAGE.AVATARS_DIR, this.filename)
46 return remove(avatarPath)
47 }
48 }