From e8bafea35bc930cb8ac5b2d521a188642a1adffe Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 17 Apr 2019 10:07:00 +0200 Subject: Create a dedicated table to track video thumbnails --- .../abstract-video-static-file-cache.ts | 32 +++++++--------------- server/lib/files-cache/videos-caption-cache.ts | 5 ++-- server/lib/files-cache/videos-preview-cache.ts | 11 ++++---- 3 files changed, 19 insertions(+), 29 deletions(-) (limited to 'server/lib/files-cache') diff --git a/server/lib/files-cache/abstract-video-static-file-cache.ts b/server/lib/files-cache/abstract-video-static-file-cache.ts index 7512f2b9d..61837e0f8 100644 --- a/server/lib/files-cache/abstract-video-static-file-cache.ts +++ b/server/lib/files-cache/abstract-video-static-file-cache.ts @@ -1,41 +1,29 @@ -import * as AsyncLRU from 'async-lru' import { createWriteStream, remove } from 'fs-extra' import { logger } from '../../helpers/logger' import { VideoModel } from '../../models/video/video' import { fetchRemoteVideoStaticFile } from '../activitypub' +import * as memoizee from 'memoizee' export abstract class AbstractVideoStaticFileCache { - protected lru + getFilePath: (params: T) => Promise - abstract getFilePath (params: T): Promise + abstract getFilePathImpl (params: T): Promise // Load and save the remote file, then return the local path from filesystem protected abstract loadRemoteFile (key: string): Promise init (max: number, maxAge: number) { - this.lru = new AsyncLRU({ - max, + this.getFilePath = memoizee(this.getFilePathImpl, { maxAge, - load: (key, cb) => { - this.loadRemoteFile(key) - .then(res => cb(null, res)) - .catch(err => cb(err)) + max, + promise: true, + dispose: (value: string) => { + remove(value) + .then(() => logger.debug('%s evicted from %s', value, this.constructor.name)) + .catch(err => logger.error('Cannot remove %s from cache %s.', value, this.constructor.name, { err })) } }) - - this.lru.on('evict', (obj: { key: string, value: string }) => { - remove(obj.value) - .then(() => logger.debug('%s evicted from %s', obj.value, this.constructor.name)) - }) - } - - protected loadFromLRU (key: string) { - return new Promise((res, rej) => { - this.lru.get(key, (err, value) => { - err ? rej(err) : res(value) - }) - }) } protected saveRemoteVideoFileAndReturnPath (video: VideoModel, remoteStaticPath: string, destPath: string) { diff --git a/server/lib/files-cache/videos-caption-cache.ts b/server/lib/files-cache/videos-caption-cache.ts index 0926f4009..d4a0a3345 100644 --- a/server/lib/files-cache/videos-caption-cache.ts +++ b/server/lib/files-cache/videos-caption-cache.ts @@ -20,14 +20,14 @@ class VideosCaptionCache extends AbstractVideoStaticFileCache { return this.instance || (this.instance = new this()) } - async getFilePath (params: GetPathParam) { + async getFilePathImpl (params: GetPathParam) { const videoCaption = await VideoCaptionModel.loadByVideoIdAndLanguage(params.videoId, params.language) if (!videoCaption) return undefined if (videoCaption.isOwned()) return join(CONFIG.STORAGE.CAPTIONS_DIR, videoCaption.getCaptionName()) const key = params.videoId + VideosCaptionCache.KEY_DELIMITER + params.language - return this.loadFromLRU(key) + return this.loadRemoteFile(key) } protected async loadRemoteFile (key: string) { @@ -42,6 +42,7 @@ class VideosCaptionCache extends AbstractVideoStaticFileCache { const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoId) if (!video) return undefined + // FIXME: use URL const remoteStaticPath = videoCaption.getCaptionStaticPath() const destPath = join(FILES_CACHE.VIDEO_CAPTIONS.DIRECTORY, videoCaption.getCaptionName()) diff --git a/server/lib/files-cache/videos-preview-cache.ts b/server/lib/files-cache/videos-preview-cache.ts index 6575e1c83..fc0d92c78 100644 --- a/server/lib/files-cache/videos-preview-cache.ts +++ b/server/lib/files-cache/videos-preview-cache.ts @@ -16,13 +16,13 @@ class VideosPreviewCache extends AbstractVideoStaticFileCache { return this.instance || (this.instance = new this()) } - async getFilePath (videoUUID: string) { + async getFilePathImpl (videoUUID: string) { const video = await VideoModel.loadByUUIDWithFile(videoUUID) if (!video) return undefined - if (video.isOwned()) return join(CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName()) + if (video.isOwned()) return join(CONFIG.STORAGE.PREVIEWS_DIR, video.getPreview().filename) - return this.loadFromLRU(videoUUID) + return this.loadRemoteFile(videoUUID) } protected async loadRemoteFile (key: string) { @@ -31,8 +31,9 @@ class VideosPreviewCache extends AbstractVideoStaticFileCache { if (video.isOwned()) throw new Error('Cannot load remote preview of owned video.') - const remoteStaticPath = join(STATIC_PATHS.PREVIEWS, video.getPreviewName()) - const destPath = join(FILES_CACHE.PREVIEWS.DIRECTORY, video.getPreviewName()) + // FIXME: use URL + const remoteStaticPath = join(STATIC_PATHS.PREVIEWS, video.getPreview().filename) + const destPath = join(FILES_CACHE.PREVIEWS.DIRECTORY, video.getPreview().filename) return this.saveRemoteVideoFileAndReturnPath(video, remoteStaticPath, destPath) } -- cgit v1.2.3