diff options
Diffstat (limited to 'server/models/actor/actor-image.ts')
-rw-r--r-- | server/models/actor/actor-image.ts | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/server/models/actor/actor-image.ts b/server/models/actor/actor-image.ts new file mode 100644 index 000000000..a35f9edb0 --- /dev/null +++ b/server/models/actor/actor-image.ts | |||
@@ -0,0 +1,101 @@ | |||
1 | import { remove } from 'fs-extra' | ||
2 | import { join } from 'path' | ||
3 | import { AfterDestroy, AllowNull, Column, CreatedAt, Default, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' | ||
4 | import { MActorImageFormattable } from '@server/types/models' | ||
5 | import { AttributesOnly } from '@shared/core-utils' | ||
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' | ||
9 | import { logger } from '../../helpers/logger' | ||
10 | import { CONFIG } from '../../initializers/config' | ||
11 | import { LAZY_STATIC_PATHS } from '../../initializers/constants' | ||
12 | import { throwIfNotValid } from '../utils' | ||
13 | |||
14 | @Table({ | ||
15 | tableName: 'actorImage', | ||
16 | indexes: [ | ||
17 | { | ||
18 | fields: [ 'filename' ], | ||
19 | unique: true | ||
20 | } | ||
21 | ] | ||
22 | }) | ||
23 | export class ActorImageModel extends Model<Partial<AttributesOnly<ActorImageModel>>> { | ||
24 | |||
25 | @AllowNull(false) | ||
26 | @Column | ||
27 | filename: string | ||
28 | |||
29 | @AllowNull(true) | ||
30 | @Default(null) | ||
31 | @Column | ||
32 | height: number | ||
33 | |||
34 | @AllowNull(true) | ||
35 | @Default(null) | ||
36 | @Column | ||
37 | width: number | ||
38 | |||
39 | @AllowNull(true) | ||
40 | @Is('ActorImageFileUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'fileUrl', true)) | ||
41 | @Column | ||
42 | fileUrl: string | ||
43 | |||
44 | @AllowNull(false) | ||
45 | @Column | ||
46 | onDisk: boolean | ||
47 | |||
48 | @AllowNull(false) | ||
49 | @Column | ||
50 | type: ActorImageType | ||
51 | |||
52 | @CreatedAt | ||
53 | createdAt: Date | ||
54 | |||
55 | @UpdatedAt | ||
56 | updatedAt: Date | ||
57 | |||
58 | @AfterDestroy | ||
59 | static removeFilesAndSendDelete (instance: ActorImageModel) { | ||
60 | logger.info('Removing actor image file %s.', instance.filename) | ||
61 | |||
62 | // Don't block the transaction | ||
63 | instance.removeImage() | ||
64 | .catch(err => logger.error('Cannot remove actor image file %s.', instance.filename, err)) | ||
65 | } | ||
66 | |||
67 | static loadByName (filename: string) { | ||
68 | const query = { | ||
69 | where: { | ||
70 | filename | ||
71 | } | ||
72 | } | ||
73 | |||
74 | return ActorImageModel.findOne(query) | ||
75 | } | ||
76 | |||
77 | toFormattedJSON (this: MActorImageFormattable): ActorImage { | ||
78 | return { | ||
79 | path: this.getStaticPath(), | ||
80 | createdAt: this.createdAt, | ||
81 | updatedAt: this.updatedAt | ||
82 | } | ||
83 | } | ||
84 | |||
85 | getStaticPath () { | ||
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) | ||
91 | } | ||
92 | |||
93 | getPath () { | ||
94 | return join(CONFIG.STORAGE.ACTOR_IMAGES, this.filename) | ||
95 | } | ||
96 | |||
97 | removeImage () { | ||
98 | const imagePath = join(CONFIG.STORAGE.ACTOR_IMAGES, this.filename) | ||
99 | return remove(imagePath) | ||
100 | } | ||
101 | } | ||