X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Fcache%2Fvideos-preview-cache.ts;h=22b6d9cb0db82ecafd74747062ca0db8879fbe36;hb=f4001cf408a99049d01a356bfb20a62342de06ea;hp=0eb43efcc03dc10c7aab65a929b1e64bfa2f1734;hpb=5f0805d39b94eb2de1b73e0f43ac8685ae900994;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/cache/videos-preview-cache.ts b/server/lib/cache/videos-preview-cache.ts index 0eb43efcc..22b6d9cb0 100644 --- a/server/lib/cache/videos-preview-cache.ts +++ b/server/lib/cache/videos-preview-cache.ts @@ -1,73 +1,39 @@ -import * as asyncLRU from 'async-lru' -import { createWriteStream } from 'fs' import { join } from 'path' -import { logger, unlinkPromise } from '../../helpers' -import { CACHE, CONFIG } from '../../initializers' +import { CACHE, CONFIG, STATIC_PATHS } from '../../initializers' import { VideoModel } from '../../models/video/video' -import { fetchRemoteVideoPreview } from '../activitypub' +import { AbstractVideoStaticFileCache } from './abstract-video-static-file-cache' -class VideosPreviewCache { +class VideosPreviewCache extends AbstractVideoStaticFileCache { private static instance: VideosPreviewCache - private lru - - private constructor () { } + private constructor () { + super() + } static get Instance () { return this.instance || (this.instance = new this()) } - init (max: number) { - this.lru = new asyncLRU({ - max, - load: (key, cb) => { - this.loadPreviews(key) - .then(res => cb(null, res)) - .catch(err => cb(err)) - } - }) - - this.lru.on('evict', (obj: { key: string, value: string }) => { - unlinkPromise(obj.value).then(() => logger.debug('%s evicted from VideosPreviewCache', obj.value)) - }) - } - - async getPreviewPath (key: string) { - const video = await VideoModel.loadByUUID(key) + async getFilePath (videoUUID: string) { + const video = await VideoModel.loadByUUID(videoUUID) if (!video) return undefined if (video.isOwned()) return join(CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName()) - return new Promise((res, rej) => { - this.lru.get(key, (err, value) => { - err ? rej(err) : res(value) - }) - }) + return this.loadFromLRU(videoUUID) } - private async loadPreviews (key: string) { + protected async loadRemoteFile (key: string) { const video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(key) if (!video) return undefined - if (video.isOwned()) throw new Error('Cannot load preview of owned video.') - - const res = await this.saveRemotePreviewAndReturnPath(video) - - return res - } - - private saveRemotePreviewAndReturnPath (video: VideoModel) { - const req = fetchRemoteVideoPreview(video) + if (video.isOwned()) throw new Error('Cannot load remote preview of owned video.') - return new Promise((res, rej) => { - const path = join(CACHE.DIRECTORIES.PREVIEWS, video.getPreviewName()) - const stream = createWriteStream(path) + const remoteStaticPath = join(STATIC_PATHS.PREVIEWS, video.getPreviewName()) + const destPath = join(CACHE.PREVIEWS.DIRECTORY, video.getPreviewName()) - req.pipe(stream) - .on('finish', () => res(path)) - .on('error', (err) => rej(err)) - }) + return this.saveRemoteVideoFileAndReturnPath(video, remoteStaticPath, destPath) } }