From a8b1b40485145ac1eae513a661d7dd6e0986ce96 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 12 Feb 2021 16:23:19 +0100 Subject: Generate a name for thumbnails Allows aggressive caching --- server/models/video/thumbnail.ts | 31 +++++++++++++++++++------------ server/models/video/video.ts | 5 +++-- 2 files changed, 22 insertions(+), 14 deletions(-) (limited to 'server/models/video') diff --git a/server/models/video/thumbnail.ts b/server/models/video/thumbnail.ts index 6878a3155..3cad6c668 100644 --- a/server/models/video/thumbnail.ts +++ b/server/models/video/thumbnail.ts @@ -1,3 +1,4 @@ +import { remove } from 'fs-extra' import { join } from 'path' import { AfterDestroy, @@ -12,15 +13,14 @@ import { Table, UpdatedAt } from 'sequelize-typescript' -import { CONSTRAINTS_FIELDS, LAZY_STATIC_PATHS, STATIC_PATHS, WEBSERVER } from '../../initializers/constants' +import { buildRemoteVideoBaseUrl } from '@server/helpers/activitypub' +import { MThumbnailVideo, MVideoAccountLight } from '@server/types/models' +import { ThumbnailType } from '../../../shared/models/videos/thumbnail.type' import { logger } from '../../helpers/logger' -import { remove } from 'fs-extra' import { CONFIG } from '../../initializers/config' +import { CONSTRAINTS_FIELDS, LAZY_STATIC_PATHS, STATIC_PATHS, WEBSERVER } from '../../initializers/constants' import { VideoModel } from './video' import { VideoPlaylistModel } from './video-playlist' -import { ThumbnailType } from '../../../shared/models/videos/thumbnail.type' -import { MVideoAccountLight } from '@server/types/models' -import { buildRemoteVideoBaseUrl } from '@server/helpers/activitypub' @Table({ tableName: 'thumbnail', @@ -31,6 +31,10 @@ import { buildRemoteVideoBaseUrl } from '@server/helpers/activitypub' { fields: [ 'videoPlaylistId' ], unique: true + }, + { + fields: [ 'filename', 'type' ], + unique: true } ] }) @@ -114,20 +118,23 @@ export class ThumbnailModel extends Model { .catch(err => logger.error('Cannot remove thumbnail file %s.', instance.filename, err)) } - static loadByName (filename: string) { + static loadWithVideoByName (filename: string, thumbnailType: ThumbnailType): Promise { const query = { where: { - filename - } + filename, + type: thumbnailType + }, + include: [ + { + model: VideoModel.unscoped(), + required: true + } + ] } return ThumbnailModel.findOne(query) } - static generateDefaultPreviewName (videoUUID: string) { - return videoUUID + '.jpg' - } - getFileUrl (video: MVideoAccountLight) { const staticPath = ThumbnailModel.types[this.type].staticPath + this.filename diff --git a/server/models/video/video.ts b/server/models/video/video.ts index 14e80a3ba..3321deed3 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts @@ -130,6 +130,7 @@ import { VideoShareModel } from './video-share' import { VideoStreamingPlaylistModel } from './video-streaming-playlist' import { VideoTagModel } from './video-tag' import { VideoViewModel } from './video-view' +import { v4 as uuidv4 } from 'uuid' export enum ScopeNames { AVAILABLE_FOR_LIST_IDS = 'AVAILABLE_FOR_LIST_IDS', @@ -1827,7 +1828,7 @@ export class VideoModel extends Model { } generateThumbnailName () { - return this.uuid + '.jpg' + return uuidv4() + '.jpg' } getMiniature () { @@ -1837,7 +1838,7 @@ export class VideoModel extends Model { } generatePreviewName () { - return this.uuid + '.jpg' + return uuidv4() + '.jpg' } hasPreview () { -- cgit v1.2.3