]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/account/actor-image.ts
Merge branch 'release/3.1.0' into develop
[github/Chocobozzz/PeerTube.git] / server / models / account / actor-image.ts
CommitLineData
f4796856 1import { remove } from 'fs-extra'
c5911fd3 2import { join } from 'path'
84531547 3import { AfterDestroy, AllowNull, Column, CreatedAt, Default, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
f4796856
C
4import { MActorImageFormattable } from '@server/types/models'
5import { ActorImageType } from '@shared/models'
6import { ActorImage } from '../../../shared/models/actors/actor-image.model'
7import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
8e0fd45e 8import { logger } from '../../helpers/logger'
6dd9de95 9import { CONFIG } from '../../initializers/config'
f4796856 10import { LAZY_STATIC_PATHS } from '../../initializers/constants'
557b13ae 11import { throwIfNotValid } from '../utils'
2295ce6c 12
3fd3ab2d 13@Table({
f4796856 14 tableName: 'actorImage',
557b13ae
C
15 indexes: [
16 {
17 fields: [ 'filename' ],
18 unique: true
19 }
20 ]
3fd3ab2d 21})
f4796856 22export class ActorImageModel extends Model {
2295ce6c 23
3fd3ab2d
C
24 @AllowNull(false)
25 @Column
26 filename: string
2295ce6c 27
84531547
C
28 @AllowNull(true)
29 @Default(null)
30 @Column
31 height: number
32
33 @AllowNull(true)
34 @Default(null)
35 @Column
36 width: number
37
557b13ae 38 @AllowNull(true)
f4796856 39 @Is('ActorImageFileUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'fileUrl', true))
557b13ae
C
40 @Column
41 fileUrl: string
42
43 @AllowNull(false)
44 @Column
45 onDisk: boolean
46
f4796856
C
47 @AllowNull(false)
48 @Column
49 type: ActorImageType
50
3fd3ab2d
C
51 @CreatedAt
52 createdAt: Date
2295ce6c 53
3fd3ab2d
C
54 @UpdatedAt
55 updatedAt: Date
c5911fd3
C
56
57 @AfterDestroy
f4796856
C
58 static removeFilesAndSendDelete (instance: ActorImageModel) {
59 logger.info('Removing actor image file %s.', instance.filename)
ecf3f060
C
60
61 // Don't block the transaction
f4796856
C
62 instance.removeImage()
63 .catch(err => logger.error('Cannot remove actor image file %s.', instance.filename, err))
c5911fd3
C
64 }
65
557b13ae
C
66 static loadByName (filename: string) {
67 const query = {
68 where: {
69 filename
70 }
71 }
72
f4796856 73 return ActorImageModel.findOne(query)
557b13ae
C
74 }
75
f4796856 76 toFormattedJSON (this: MActorImageFormattable): ActorImage {
c5911fd3 77 return {
557b13ae 78 path: this.getStaticPath(),
c5911fd3
C
79 createdAt: this.createdAt,
80 updatedAt: this.updatedAt
81 }
82 }
83
557b13ae 84 getStaticPath () {
cdeddff1
C
85 if (this.type === ActorImageType.AVATAR) {
86 return join(LAZY_STATIC_PATHS.AVATARS, this.filename)
87 }
88
89 return join(LAZY_STATIC_PATHS.BANNERS, this.filename)
557b13ae
C
90 }
91
92 getPath () {
f4796856 93 return join(CONFIG.STORAGE.ACTOR_IMAGES, this.filename)
c5911fd3
C
94 }
95
f4796856
C
96 removeImage () {
97 const imagePath = join(CONFIG.STORAGE.ACTOR_IMAGES, this.filename)
98 return remove(imagePath)
c5911fd3 99 }
2295ce6c 100}