X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Ffiles-cache%2Fabstract-video-static-file-cache.ts;h=c06355446c73fc73cb0da0952f97d9494e2da84c;hb=b6a1dd4d1b3b0032f8b968e72cbd074f646e8827;hp=61837e0f8e6c101a34f93c97e637e23fadb692f7;hpb=e8bafea35bc930cb8ac5b2d521a188642a1adffe;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 61837e0f8..c06355446 100644 --- a/server/lib/files-cache/abstract-video-static-file-cache.ts +++ b/server/lib/files-cache/abstract-video-static-file-cache.ts @@ -1,40 +1,30 @@ -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 * as memoizee from 'memoizee' +type GetFilePathResult = { isOwned: boolean, path: string } | undefined + export abstract class AbstractVideoStaticFileCache { - getFilePath: (params: T) => Promise + getFilePath: (params: T) => Promise - abstract getFilePathImpl (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.getFilePath = memoizee(this.getFilePathImpl, { maxAge, 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 })) + 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 })) + } } }) } - - 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)) - }) - } }