]>
Commit | Line | Data |
---|---|---|
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 | ||
102 | isOwned () { | |
103 | return !this.fileUrl | |
104 | } | |
105 | } |