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