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