diff options
author | Chocobozzz <me@florianbigard.com> | 2019-08-09 11:32:40 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2019-08-09 11:32:40 +0200 |
commit | 557b13ae24019d9ab214bbea7eaa0f892c8f4b05 (patch) | |
tree | aa32396531acf93e3dfdb29880177813039ed77f /server/models | |
parent | c5407d7046168abb4098df1408e7aa84519cb61a (diff) | |
download | PeerTube-557b13ae24019d9ab214bbea7eaa0f892c8f4b05.tar.gz PeerTube-557b13ae24019d9ab214bbea7eaa0f892c8f4b05.tar.zst PeerTube-557b13ae24019d9ab214bbea7eaa0f892c8f4b05.zip |
Lazy load avatars
Diffstat (limited to 'server/models')
-rw-r--r-- | server/models/account/user-notification.ts | 4 | ||||
-rw-r--r-- | server/models/activitypub/actor.ts | 2 | ||||
-rw-r--r-- | server/models/avatar/avatar.ts | 43 | ||||
-rw-r--r-- | server/models/video/thumbnail.ts | 4 | ||||
-rw-r--r-- | server/models/video/video-caption.ts | 4 | ||||
-rw-r--r-- | server/models/video/video.ts | 3 |
6 files changed, 46 insertions, 14 deletions
diff --git a/server/models/account/user-notification.ts b/server/models/account/user-notification.ts index a4f97037b..f38cd7e78 100644 --- a/server/models/account/user-notification.ts +++ b/server/models/account/user-notification.ts | |||
@@ -410,7 +410,7 @@ export class UserNotificationModel extends Model<UserNotificationModel> { | |||
410 | id: this.ActorFollow.ActorFollower.Account.id, | 410 | id: this.ActorFollow.ActorFollower.Account.id, |
411 | displayName: this.ActorFollow.ActorFollower.Account.getDisplayName(), | 411 | displayName: this.ActorFollow.ActorFollower.Account.getDisplayName(), |
412 | name: this.ActorFollow.ActorFollower.preferredUsername, | 412 | name: this.ActorFollow.ActorFollower.preferredUsername, |
413 | avatar: this.ActorFollow.ActorFollower.Avatar ? { path: this.ActorFollow.ActorFollower.Avatar.getWebserverPath() } : undefined, | 413 | avatar: this.ActorFollow.ActorFollower.Avatar ? { path: this.ActorFollow.ActorFollower.Avatar.getStaticPath() } : undefined, |
414 | host: this.ActorFollow.ActorFollower.getHost() | 414 | host: this.ActorFollow.ActorFollower.getHost() |
415 | }, | 415 | }, |
416 | following: { | 416 | following: { |
@@ -446,7 +446,7 @@ export class UserNotificationModel extends Model<UserNotificationModel> { | |||
446 | 446 | ||
447 | private formatActor (accountOrChannel: AccountModel | VideoChannelModel) { | 447 | private formatActor (accountOrChannel: AccountModel | VideoChannelModel) { |
448 | const avatar = accountOrChannel.Actor.Avatar | 448 | const avatar = accountOrChannel.Actor.Avatar |
449 | ? { path: accountOrChannel.Actor.Avatar.getWebserverPath() } | 449 | ? { path: accountOrChannel.Actor.Avatar.getStaticPath() } |
450 | : undefined | 450 | : undefined |
451 | 451 | ||
452 | return { | 452 | return { |
diff --git a/server/models/activitypub/actor.ts b/server/models/activitypub/actor.ts index bd6a2c8fd..9cc53f78a 100644 --- a/server/models/activitypub/actor.ts +++ b/server/models/activitypub/actor.ts | |||
@@ -513,7 +513,7 @@ export class ActorModel extends Model<ActorModel> { | |||
513 | getAvatarUrl () { | 513 | getAvatarUrl () { |
514 | if (!this.avatarId) return undefined | 514 | if (!this.avatarId) return undefined |
515 | 515 | ||
516 | return WEBSERVER.URL + this.Avatar.getWebserverPath() | 516 | return WEBSERVER.URL + this.Avatar.getStaticPath() |
517 | } | 517 | } |
518 | 518 | ||
519 | isOutdated () { | 519 | isOutdated () { |
diff --git a/server/models/avatar/avatar.ts b/server/models/avatar/avatar.ts index aaf1b8bd9..7a370bcd3 100644 --- a/server/models/avatar/avatar.ts +++ b/server/models/avatar/avatar.ts | |||
@@ -1,13 +1,21 @@ | |||
1 | import { join } from 'path' | 1 | import { join } from 'path' |
2 | import { AfterDestroy, AllowNull, Column, CreatedAt, Model, Table, UpdatedAt } from 'sequelize-typescript' | 2 | import { AfterDestroy, AllowNull, Column, CreatedAt, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' |
3 | import { Avatar } from '../../../shared/models/avatars/avatar.model' | 3 | import { Avatar } from '../../../shared/models/avatars/avatar.model' |
4 | import { STATIC_PATHS } from '../../initializers/constants' | 4 | import { LAZY_STATIC_PATHS } from '../../initializers/constants' |
5 | import { logger } from '../../helpers/logger' | 5 | import { logger } from '../../helpers/logger' |
6 | import { remove } from 'fs-extra' | 6 | import { remove } from 'fs-extra' |
7 | import { CONFIG } from '../../initializers/config' | 7 | import { CONFIG } from '../../initializers/config' |
8 | import { throwIfNotValid } from '../utils' | ||
9 | import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' | ||
8 | 10 | ||
9 | @Table({ | 11 | @Table({ |
10 | tableName: 'avatar' | 12 | tableName: 'avatar', |
13 | indexes: [ | ||
14 | { | ||
15 | fields: [ 'filename' ], | ||
16 | unique: true | ||
17 | } | ||
18 | ] | ||
11 | }) | 19 | }) |
12 | export class AvatarModel extends Model<AvatarModel> { | 20 | export class AvatarModel extends Model<AvatarModel> { |
13 | 21 | ||
@@ -15,6 +23,15 @@ export class AvatarModel extends Model<AvatarModel> { | |||
15 | @Column | 23 | @Column |
16 | filename: string | 24 | filename: string |
17 | 25 | ||
26 | @AllowNull(true) | ||
27 | @Is('AvatarFileUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'fileUrl')) | ||
28 | @Column | ||
29 | fileUrl: string | ||
30 | |||
31 | @AllowNull(false) | ||
32 | @Column | ||
33 | onDisk: boolean | ||
34 | |||
18 | @CreatedAt | 35 | @CreatedAt |
19 | createdAt: Date | 36 | createdAt: Date |
20 | 37 | ||
@@ -30,16 +47,30 @@ export class AvatarModel extends Model<AvatarModel> { | |||
30 | .catch(err => logger.error('Cannot remove avatar file %s.', instance.filename, err)) | 47 | .catch(err => logger.error('Cannot remove avatar file %s.', instance.filename, err)) |
31 | } | 48 | } |
32 | 49 | ||
50 | static loadByName (filename: string) { | ||
51 | const query = { | ||
52 | where: { | ||
53 | filename | ||
54 | } | ||
55 | } | ||
56 | |||
57 | return AvatarModel.findOne(query) | ||
58 | } | ||
59 | |||
33 | toFormattedJSON (): Avatar { | 60 | toFormattedJSON (): Avatar { |
34 | return { | 61 | return { |
35 | path: this.getWebserverPath(), | 62 | path: this.getStaticPath(), |
36 | createdAt: this.createdAt, | 63 | createdAt: this.createdAt, |
37 | updatedAt: this.updatedAt | 64 | updatedAt: this.updatedAt |
38 | } | 65 | } |
39 | } | 66 | } |
40 | 67 | ||
41 | getWebserverPath () { | 68 | getStaticPath () { |
42 | return join(STATIC_PATHS.AVATARS, this.filename) | 69 | return join(LAZY_STATIC_PATHS.AVATARS, this.filename) |
70 | } | ||
71 | |||
72 | getPath () { | ||
73 | return join(CONFIG.STORAGE.AVATARS_DIR, this.filename) | ||
43 | } | 74 | } |
44 | 75 | ||
45 | removeAvatar () { | 76 | removeAvatar () { |
diff --git a/server/models/video/thumbnail.ts b/server/models/video/thumbnail.ts index b767a6874..cf2040cbf 100644 --- a/server/models/video/thumbnail.ts +++ b/server/models/video/thumbnail.ts | |||
@@ -1,6 +1,6 @@ | |||
1 | import { join } from 'path' | 1 | import { join } from 'path' |
2 | import { AfterDestroy, AllowNull, BelongsTo, Column, CreatedAt, Default, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript' | 2 | import { AfterDestroy, AllowNull, BelongsTo, Column, CreatedAt, Default, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript' |
3 | import { STATIC_PATHS, WEBSERVER } from '../../initializers/constants' | 3 | import { LAZY_STATIC_PATHS, STATIC_PATHS, WEBSERVER } from '../../initializers/constants' |
4 | import { logger } from '../../helpers/logger' | 4 | import { logger } from '../../helpers/logger' |
5 | import { remove } from 'fs-extra' | 5 | import { remove } from 'fs-extra' |
6 | import { CONFIG } from '../../initializers/config' | 6 | import { CONFIG } from '../../initializers/config' |
@@ -87,7 +87,7 @@ export class ThumbnailModel extends Model<ThumbnailModel> { | |||
87 | [ThumbnailType.PREVIEW]: { | 87 | [ThumbnailType.PREVIEW]: { |
88 | label: 'preview', | 88 | label: 'preview', |
89 | directory: CONFIG.STORAGE.PREVIEWS_DIR, | 89 | directory: CONFIG.STORAGE.PREVIEWS_DIR, |
90 | staticPath: STATIC_PATHS.PREVIEWS | 90 | staticPath: LAZY_STATIC_PATHS.PREVIEWS |
91 | } | 91 | } |
92 | } | 92 | } |
93 | 93 | ||
diff --git a/server/models/video/video-caption.ts b/server/models/video/video-caption.ts index 76243bf48..a01565851 100644 --- a/server/models/video/video-caption.ts +++ b/server/models/video/video-caption.ts | |||
@@ -16,7 +16,7 @@ import { buildWhereIdOrUUID, throwIfNotValid } from '../utils' | |||
16 | import { VideoModel } from './video' | 16 | import { VideoModel } from './video' |
17 | import { isVideoCaptionLanguageValid } from '../../helpers/custom-validators/video-captions' | 17 | import { isVideoCaptionLanguageValid } from '../../helpers/custom-validators/video-captions' |
18 | import { VideoCaption } from '../../../shared/models/videos/caption/video-caption.model' | 18 | import { VideoCaption } from '../../../shared/models/videos/caption/video-caption.model' |
19 | import { STATIC_PATHS, VIDEO_LANGUAGES } from '../../initializers/constants' | 19 | import { LAZY_STATIC_PATHS, VIDEO_LANGUAGES } from '../../initializers/constants' |
20 | import { join } from 'path' | 20 | import { join } from 'path' |
21 | import { logger } from '../../helpers/logger' | 21 | import { logger } from '../../helpers/logger' |
22 | import { remove } from 'fs-extra' | 22 | import { remove } from 'fs-extra' |
@@ -163,7 +163,7 @@ export class VideoCaptionModel extends Model<VideoCaptionModel> { | |||
163 | } | 163 | } |
164 | 164 | ||
165 | getCaptionStaticPath () { | 165 | getCaptionStaticPath () { |
166 | return join(STATIC_PATHS.VIDEO_CAPTIONS, this.getCaptionName()) | 166 | return join(LAZY_STATIC_PATHS.VIDEO_CAPTIONS, this.getCaptionName()) |
167 | } | 167 | } |
168 | 168 | ||
169 | getCaptionName () { | 169 | getCaptionName () { |
diff --git a/server/models/video/video.ts b/server/models/video/video.ts index ae29cf286..1321337ff 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts | |||
@@ -63,6 +63,7 @@ import { | |||
63 | CONSTRAINTS_FIELDS, | 63 | CONSTRAINTS_FIELDS, |
64 | HLS_REDUNDANCY_DIRECTORY, | 64 | HLS_REDUNDANCY_DIRECTORY, |
65 | HLS_STREAMING_PLAYLIST_DIRECTORY, | 65 | HLS_STREAMING_PLAYLIST_DIRECTORY, |
66 | LAZY_STATIC_PATHS, | ||
66 | REMOTE_SCHEME, | 67 | REMOTE_SCHEME, |
67 | STATIC_DOWNLOAD_PATHS, | 68 | STATIC_DOWNLOAD_PATHS, |
68 | STATIC_PATHS, | 69 | STATIC_PATHS, |
@@ -1856,7 +1857,7 @@ export class VideoModel extends Model<VideoModel> { | |||
1856 | if (!preview) return null | 1857 | if (!preview) return null |
1857 | 1858 | ||
1858 | // We use a local cache, so specify our cache endpoint instead of potential remote URL | 1859 | // We use a local cache, so specify our cache endpoint instead of potential remote URL |
1859 | return join(STATIC_PATHS.PREVIEWS, preview.filename) | 1860 | return join(LAZY_STATIC_PATHS.PREVIEWS, preview.filename) |
1860 | } | 1861 | } |
1861 | 1862 | ||
1862 | toFormattedJSON (options?: VideoFormattingJSONOptions): Video { | 1863 | toFormattedJSON (options?: VideoFormattingJSONOptions): Video { |