]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/avatar/avatar.ts
Cleaner warning of IP address leaking on embedded videos (#2034)
[github/Chocobozzz/PeerTube.git] / server / models / avatar / avatar.ts
CommitLineData
c5911fd3 1import { join } from 'path'
557b13ae 2import { AfterDestroy, AllowNull, Column, CreatedAt, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
c5911fd3 3import { Avatar } from '../../../shared/models/avatars/avatar.model'
557b13ae 4import { LAZY_STATIC_PATHS } from '../../initializers/constants'
8e0fd45e 5import { logger } from '../../helpers/logger'
62689b94 6import { remove } from 'fs-extra'
6dd9de95 7import { CONFIG } from '../../initializers/config'
557b13ae
C
8import { throwIfNotValid } from '../utils'
9import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
2295ce6c 10
3fd3ab2d 11@Table({
557b13ae
C
12 tableName: 'avatar',
13 indexes: [
14 {
15 fields: [ 'filename' ],
16 unique: true
17 }
18 ]
3fd3ab2d
C
19})
20export class AvatarModel extends Model<AvatarModel> {
2295ce6c 21
3fd3ab2d
C
22 @AllowNull(false)
23 @Column
24 filename: string
2295ce6c 25
557b13ae 26 @AllowNull(true)
5bb2eb56 27 @Is('AvatarFileUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'fileUrl', true))
557b13ae
C
28 @Column
29 fileUrl: string
30
31 @AllowNull(false)
32 @Column
33 onDisk: boolean
34
3fd3ab2d
C
35 @CreatedAt
36 createdAt: Date
2295ce6c 37
3fd3ab2d
C
38 @UpdatedAt
39 updatedAt: Date
c5911fd3
C
40
41 @AfterDestroy
42 static removeFilesAndSendDelete (instance: AvatarModel) {
8e0fd45e 43 logger.info('Removing avatar file %s.', instance.filename)
ecf3f060
C
44
45 // Don't block the transaction
46 instance.removeAvatar()
47 .catch(err => logger.error('Cannot remove avatar file %s.', instance.filename, err))
c5911fd3
C
48 }
49
557b13ae
C
50 static loadByName (filename: string) {
51 const query = {
52 where: {
53 filename
54 }
55 }
56
57 return AvatarModel.findOne(query)
58 }
59
c5911fd3
C
60 toFormattedJSON (): Avatar {
61 return {
557b13ae 62 path: this.getStaticPath(),
c5911fd3
C
63 createdAt: this.createdAt,
64 updatedAt: this.updatedAt
65 }
66 }
67
557b13ae
C
68 getStaticPath () {
69 return join(LAZY_STATIC_PATHS.AVATARS, this.filename)
70 }
71
72 getPath () {
73 return join(CONFIG.STORAGE.AVATARS_DIR, this.filename)
c5911fd3
C
74 }
75
76 removeAvatar () {
77 const avatarPath = join(CONFIG.STORAGE.AVATARS_DIR, this.filename)
62689b94 78 return remove(avatarPath)
c5911fd3 79 }
2295ce6c 80}