X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Ffiles-cache%2Fabstract-video-static-file-cache.ts;h=a7ac88525c80be00d6d7616e7607f8f195b914f6;hb=46f7cd6837311019acef6e2086c7b006e6dbe1b9;hp=7512f2b9da0b260feff00afad908601003fcf25a;hpb=d74d29ad9e35929491cf37223398d2535ab23de0;p=github%2FChocobozzz%2FPeerTube.git 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..a7ac88525 100644 --- a/server/lib/files-cache/abstract-video-static-file-cache.ts +++ b/server/lib/files-cache/abstract-video-static-file-cache.ts @@ -1,52 +1,30 @@ -import * as AsyncLRU from 'async-lru' -import { createWriteStream, remove } from 'fs-extra' +import { remove } from 'fs-extra' import { logger } from '../../helpers/logger' -import { VideoModel } from '../../models/video/video' -import { fetchRemoteVideoStaticFile } from '../activitypub' +import memoizee from 'memoizee' + +type GetFilePathResult = { isOwned: boolean, path: string, downloadName?: string } | undefined 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 + 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: (result?: GetFilePathResult) => { + if (result && result.isOwned !== true) { + remove(result.path) + .then(() => logger.debug('%s removed from %s', result.path, this.constructor.name)) + .catch(err => logger.error('Cannot remove %s from cache %s.', result.path, 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) { - return new Promise((res, rej) => { - const req = fetchRemoteVideoStaticFile(video, remoteStaticPath, rej) - - const stream = createWriteStream(destPath) - - req.pipe(stream) - .on('error', (err) => rej(err)) - .on('finish', () => res(destPath)) - }) } }