]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/account/actor-image.ts
Add banner migrations
[github/Chocobozzz/PeerTube.git] / server / models / account / actor-image.ts
CommitLineData
f4796856 1import { remove } from 'fs-extra'
c5911fd3 2import { join } from 'path'
557b13ae 3import { AfterDestroy, AllowNull, Column, CreatedAt, 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
557b13ae 28 @AllowNull(true)
f4796856 29 @Is('ActorImageFileUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'fileUrl', true))
557b13ae
C
30 @Column
31 fileUrl: string
32
33 @AllowNull(false)
34 @Column
35 onDisk: boolean
36
f4796856
C
37 @AllowNull(false)
38 @Column
39 type: ActorImageType
40
3fd3ab2d
C
41 @CreatedAt
42 createdAt: Date
2295ce6c 43
3fd3ab2d
C
44 @UpdatedAt
45 updatedAt: Date
c5911fd3
C
46
47 @AfterDestroy
f4796856
C
48 static removeFilesAndSendDelete (instance: ActorImageModel) {
49 logger.info('Removing actor image file %s.', instance.filename)
ecf3f060
C
50
51 // Don't block the transaction
f4796856
C
52 instance.removeImage()
53 .catch(err => logger.error('Cannot remove actor image file %s.', instance.filename, err))
c5911fd3
C
54 }
55
557b13ae
C
56 static loadByName (filename: string) {
57 const query = {
58 where: {
59 filename
60 }
61 }
62
f4796856 63 return ActorImageModel.findOne(query)
557b13ae
C
64 }
65
f4796856 66 toFormattedJSON (this: MActorImageFormattable): ActorImage {
c5911fd3 67 return {
557b13ae 68 path: this.getStaticPath(),
c5911fd3
C
69 createdAt: this.createdAt,
70 updatedAt: this.updatedAt
71 }
72 }
73
557b13ae
C
74 getStaticPath () {
75 return join(LAZY_STATIC_PATHS.AVATARS, this.filename)
76 }
77
78 getPath () {
f4796856 79 return join(CONFIG.STORAGE.ACTOR_IMAGES, this.filename)
c5911fd3
C
80 }
81
f4796856
C
82 removeImage () {
83 const imagePath = join(CONFIG.STORAGE.ACTOR_IMAGES, this.filename)
84 return remove(imagePath)
c5911fd3 85 }
2295ce6c 86}