From 557b13ae24019d9ab214bbea7eaa0f892c8f4b05 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 9 Aug 2019 11:32:40 +0200 Subject: Lazy load avatars --- server/models/account/user-notification.ts | 4 +-- server/models/activitypub/actor.ts | 2 +- server/models/avatar/avatar.ts | 43 +++++++++++++++++++++++++----- server/models/video/thumbnail.ts | 4 +-- server/models/video/video-caption.ts | 4 +-- server/models/video/video.ts | 3 ++- 6 files changed, 46 insertions(+), 14 deletions(-) (limited to 'server/models') 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 { id: this.ActorFollow.ActorFollower.Account.id, displayName: this.ActorFollow.ActorFollower.Account.getDisplayName(), name: this.ActorFollow.ActorFollower.preferredUsername, - avatar: this.ActorFollow.ActorFollower.Avatar ? { path: this.ActorFollow.ActorFollower.Avatar.getWebserverPath() } : undefined, + avatar: this.ActorFollow.ActorFollower.Avatar ? { path: this.ActorFollow.ActorFollower.Avatar.getStaticPath() } : undefined, host: this.ActorFollow.ActorFollower.getHost() }, following: { @@ -446,7 +446,7 @@ export class UserNotificationModel extends Model { private formatActor (accountOrChannel: AccountModel | VideoChannelModel) { const avatar = accountOrChannel.Actor.Avatar - ? { path: accountOrChannel.Actor.Avatar.getWebserverPath() } + ? { path: accountOrChannel.Actor.Avatar.getStaticPath() } : undefined 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 { getAvatarUrl () { if (!this.avatarId) return undefined - return WEBSERVER.URL + this.Avatar.getWebserverPath() + return WEBSERVER.URL + this.Avatar.getStaticPath() } 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 @@ import { join } from 'path' -import { AfterDestroy, AllowNull, Column, CreatedAt, Model, Table, UpdatedAt } from 'sequelize-typescript' +import { AfterDestroy, AllowNull, Column, CreatedAt, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' import { Avatar } from '../../../shared/models/avatars/avatar.model' -import { STATIC_PATHS } from '../../initializers/constants' +import { LAZY_STATIC_PATHS } from '../../initializers/constants' import { logger } from '../../helpers/logger' import { remove } from 'fs-extra' import { CONFIG } from '../../initializers/config' +import { throwIfNotValid } from '../utils' +import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' @Table({ - tableName: 'avatar' + tableName: 'avatar', + indexes: [ + { + fields: [ 'filename' ], + unique: true + } + ] }) export class AvatarModel extends Model { @@ -15,6 +23,15 @@ export class AvatarModel extends Model { @Column filename: string + @AllowNull(true) + @Is('AvatarFileUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'fileUrl')) + @Column + fileUrl: string + + @AllowNull(false) + @Column + onDisk: boolean + @CreatedAt createdAt: Date @@ -30,16 +47,30 @@ export class AvatarModel extends Model { .catch(err => logger.error('Cannot remove avatar file %s.', instance.filename, err)) } + static loadByName (filename: string) { + const query = { + where: { + filename + } + } + + return AvatarModel.findOne(query) + } + toFormattedJSON (): Avatar { return { - path: this.getWebserverPath(), + path: this.getStaticPath(), createdAt: this.createdAt, updatedAt: this.updatedAt } } - getWebserverPath () { - return join(STATIC_PATHS.AVATARS, this.filename) + getStaticPath () { + return join(LAZY_STATIC_PATHS.AVATARS, this.filename) + } + + getPath () { + return join(CONFIG.STORAGE.AVATARS_DIR, this.filename) } 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 @@ import { join } from 'path' import { AfterDestroy, AllowNull, BelongsTo, Column, CreatedAt, Default, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript' -import { STATIC_PATHS, WEBSERVER } from '../../initializers/constants' +import { LAZY_STATIC_PATHS, STATIC_PATHS, WEBSERVER } from '../../initializers/constants' import { logger } from '../../helpers/logger' import { remove } from 'fs-extra' import { CONFIG } from '../../initializers/config' @@ -87,7 +87,7 @@ export class ThumbnailModel extends Model { [ThumbnailType.PREVIEW]: { label: 'preview', directory: CONFIG.STORAGE.PREVIEWS_DIR, - staticPath: STATIC_PATHS.PREVIEWS + staticPath: LAZY_STATIC_PATHS.PREVIEWS } } 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' import { VideoModel } from './video' import { isVideoCaptionLanguageValid } from '../../helpers/custom-validators/video-captions' import { VideoCaption } from '../../../shared/models/videos/caption/video-caption.model' -import { STATIC_PATHS, VIDEO_LANGUAGES } from '../../initializers/constants' +import { LAZY_STATIC_PATHS, VIDEO_LANGUAGES } from '../../initializers/constants' import { join } from 'path' import { logger } from '../../helpers/logger' import { remove } from 'fs-extra' @@ -163,7 +163,7 @@ export class VideoCaptionModel extends Model { } getCaptionStaticPath () { - return join(STATIC_PATHS.VIDEO_CAPTIONS, this.getCaptionName()) + return join(LAZY_STATIC_PATHS.VIDEO_CAPTIONS, this.getCaptionName()) } 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 { CONSTRAINTS_FIELDS, HLS_REDUNDANCY_DIRECTORY, HLS_STREAMING_PLAYLIST_DIRECTORY, + LAZY_STATIC_PATHS, REMOTE_SCHEME, STATIC_DOWNLOAD_PATHS, STATIC_PATHS, @@ -1856,7 +1857,7 @@ export class VideoModel extends Model { if (!preview) return null // We use a local cache, so specify our cache endpoint instead of potential remote URL - return join(STATIC_PATHS.PREVIEWS, preview.filename) + return join(LAZY_STATIC_PATHS.PREVIEWS, preview.filename) } toFormattedJSON (options?: VideoFormattingJSONOptions): Video { -- cgit v1.2.3