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