diff options
author | Chocobozzz <me@florianbigard.com> | 2021-04-06 11:35:56 +0200 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2021-04-08 10:07:53 +0200 |
commit | f479685678406a5df864d89615b33d29085ebfc6 (patch) | |
tree | 8de15e90cd8d97d8810715df8585c61f48d5282a /server/models/account | |
parent | 968aaed2066873fc1c39f95168284122d9d15e21 (diff) | |
download | PeerTube-f479685678406a5df864d89615b33d29085ebfc6.tar.gz PeerTube-f479685678406a5df864d89615b33d29085ebfc6.tar.zst PeerTube-f479685678406a5df864d89615b33d29085ebfc6.zip |
Agnostic actor image storage
Diffstat (limited to 'server/models/account')
-rw-r--r-- | server/models/account/account.ts | 5 | ||||
-rw-r--r-- | server/models/account/actor-image.ts | 86 | ||||
-rw-r--r-- | server/models/account/user-notification.ts | 8 |
3 files changed, 94 insertions, 5 deletions
diff --git a/server/models/account/account.ts b/server/models/account/account.ts index c72f9c63d..312451abe 100644 --- a/server/models/account/account.ts +++ b/server/models/account/account.ts | |||
@@ -33,7 +33,7 @@ import { | |||
33 | import { ActorModel } from '../activitypub/actor' | 33 | import { ActorModel } from '../activitypub/actor' |
34 | import { ActorFollowModel } from '../activitypub/actor-follow' | 34 | import { ActorFollowModel } from '../activitypub/actor-follow' |
35 | import { ApplicationModel } from '../application/application' | 35 | import { ApplicationModel } from '../application/application' |
36 | import { AvatarModel } from '../avatar/avatar' | 36 | import { ActorImageModel } from './actor-image' |
37 | import { ServerModel } from '../server/server' | 37 | import { ServerModel } from '../server/server' |
38 | import { ServerBlocklistModel } from '../server/server-blocklist' | 38 | import { ServerBlocklistModel } from '../server/server-blocklist' |
39 | import { getSort, throwIfNotValid } from '../utils' | 39 | import { getSort, throwIfNotValid } from '../utils' |
@@ -82,7 +82,8 @@ export type SummaryOptions = { | |||
82 | serverInclude, | 82 | serverInclude, |
83 | 83 | ||
84 | { | 84 | { |
85 | model: AvatarModel.unscoped(), | 85 | model: ActorImageModel.unscoped(), |
86 | as: 'Avatar', | ||
86 | required: false | 87 | required: false |
87 | } | 88 | } |
88 | ] | 89 | ] |
diff --git a/server/models/account/actor-image.ts b/server/models/account/actor-image.ts new file mode 100644 index 000000000..c532bd08d --- /dev/null +++ b/server/models/account/actor-image.ts | |||
@@ -0,0 +1,86 @@ | |||
1 | import { remove } from 'fs-extra' | ||
2 | import { join } from 'path' | ||
3 | import { AfterDestroy, AllowNull, Column, CreatedAt, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' | ||
4 | import { MActorImageFormattable } from '@server/types/models' | ||
5 | import { ActorImageType } from '@shared/models' | ||
6 | import { ActorImage } from '../../../shared/models/actors/actor-image.model' | ||
7 | import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' | ||
8 | import { logger } from '../../helpers/logger' | ||
9 | import { CONFIG } from '../../initializers/config' | ||
10 | import { LAZY_STATIC_PATHS } from '../../initializers/constants' | ||
11 | import { throwIfNotValid } from '../utils' | ||
12 | |||
13 | @Table({ | ||
14 | tableName: 'actorImage', | ||
15 | indexes: [ | ||
16 | { | ||
17 | fields: [ 'filename' ], | ||
18 | unique: true | ||
19 | } | ||
20 | ] | ||
21 | }) | ||
22 | export class ActorImageModel extends Model { | ||
23 | |||
24 | @AllowNull(false) | ||
25 | @Column | ||
26 | filename: string | ||
27 | |||
28 | @AllowNull(true) | ||
29 | @Is('ActorImageFileUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'fileUrl', true)) | ||
30 | @Column | ||
31 | fileUrl: string | ||
32 | |||
33 | @AllowNull(false) | ||
34 | @Column | ||
35 | onDisk: boolean | ||
36 | |||
37 | @AllowNull(false) | ||
38 | @Column | ||
39 | type: ActorImageType | ||
40 | |||
41 | @CreatedAt | ||
42 | createdAt: Date | ||
43 | |||
44 | @UpdatedAt | ||
45 | updatedAt: Date | ||
46 | |||
47 | @AfterDestroy | ||
48 | static removeFilesAndSendDelete (instance: ActorImageModel) { | ||
49 | logger.info('Removing actor image file %s.', instance.filename) | ||
50 | |||
51 | // Don't block the transaction | ||
52 | instance.removeImage() | ||
53 | .catch(err => logger.error('Cannot remove actor image file %s.', instance.filename, err)) | ||
54 | } | ||
55 | |||
56 | static loadByName (filename: string) { | ||
57 | const query = { | ||
58 | where: { | ||
59 | filename | ||
60 | } | ||
61 | } | ||
62 | |||
63 | return ActorImageModel.findOne(query) | ||
64 | } | ||
65 | |||
66 | toFormattedJSON (this: MActorImageFormattable): ActorImage { | ||
67 | return { | ||
68 | path: this.getStaticPath(), | ||
69 | createdAt: this.createdAt, | ||
70 | updatedAt: this.updatedAt | ||
71 | } | ||
72 | } | ||
73 | |||
74 | getStaticPath () { | ||
75 | return join(LAZY_STATIC_PATHS.AVATARS, this.filename) | ||
76 | } | ||
77 | |||
78 | getPath () { | ||
79 | return join(CONFIG.STORAGE.ACTOR_IMAGES, this.filename) | ||
80 | } | ||
81 | |||
82 | removeImage () { | ||
83 | const imagePath = join(CONFIG.STORAGE.ACTOR_IMAGES, this.filename) | ||
84 | return remove(imagePath) | ||
85 | } | ||
86 | } | ||
diff --git a/server/models/account/user-notification.ts b/server/models/account/user-notification.ts index 25c523203..805095002 100644 --- a/server/models/account/user-notification.ts +++ b/server/models/account/user-notification.ts | |||
@@ -10,7 +10,6 @@ import { VideoCommentAbuseModel } from '../abuse/video-comment-abuse' | |||
10 | import { ActorModel } from '../activitypub/actor' | 10 | import { ActorModel } from '../activitypub/actor' |
11 | import { ActorFollowModel } from '../activitypub/actor-follow' | 11 | import { ActorFollowModel } from '../activitypub/actor-follow' |
12 | import { ApplicationModel } from '../application/application' | 12 | import { ApplicationModel } from '../application/application' |
13 | import { AvatarModel } from '../avatar/avatar' | ||
14 | import { PluginModel } from '../server/plugin' | 13 | import { PluginModel } from '../server/plugin' |
15 | import { ServerModel } from '../server/server' | 14 | import { ServerModel } from '../server/server' |
16 | import { getSort, throwIfNotValid } from '../utils' | 15 | import { getSort, throwIfNotValid } from '../utils' |
@@ -20,6 +19,7 @@ import { VideoChannelModel } from '../video/video-channel' | |||
20 | import { VideoCommentModel } from '../video/video-comment' | 19 | import { VideoCommentModel } from '../video/video-comment' |
21 | import { VideoImportModel } from '../video/video-import' | 20 | import { VideoImportModel } from '../video/video-import' |
22 | import { AccountModel } from './account' | 21 | import { AccountModel } from './account' |
22 | import { ActorImageModel } from './actor-image' | ||
23 | import { UserModel } from './user' | 23 | import { UserModel } from './user' |
24 | 24 | ||
25 | enum ScopeNames { | 25 | enum ScopeNames { |
@@ -34,7 +34,8 @@ function buildActorWithAvatarInclude () { | |||
34 | include: [ | 34 | include: [ |
35 | { | 35 | { |
36 | attributes: [ 'filename' ], | 36 | attributes: [ 'filename' ], |
37 | model: AvatarModel.unscoped(), | 37 | as: 'Avatar', |
38 | model: ActorImageModel.unscoped(), | ||
38 | required: false | 39 | required: false |
39 | }, | 40 | }, |
40 | { | 41 | { |
@@ -172,7 +173,8 @@ function buildAccountInclude (required: boolean, withActor = false) { | |||
172 | }, | 173 | }, |
173 | { | 174 | { |
174 | attributes: [ 'filename' ], | 175 | attributes: [ 'filename' ], |
175 | model: AvatarModel.unscoped(), | 176 | as: 'Avatar', |
177 | model: ActorImageModel.unscoped(), | ||
176 | required: false | 178 | required: false |
177 | }, | 179 | }, |
178 | { | 180 | { |