1 import { remove } from 'fs-extra'
2 import { logger } from '../../helpers/logger'
3 import memoizee from 'memoizee'
5 type GetFilePathResult = { isOwned: boolean, path: string, downloadName?: string } | undefined
7 export abstract class AbstractVideoStaticFileCache <T> {
9 getFilePath: (params: T) => Promise<GetFilePathResult>
11 abstract getFilePathImpl (params: T): Promise<GetFilePathResult>
13 // Load and save the remote file, then return the local path from filesystem
14 protected abstract loadRemoteFile (key: string): Promise<GetFilePathResult>
16 init (max: number, maxAge: number) {
17 this.getFilePath = memoizee(this.getFilePathImpl, {
21 dispose: (result?: GetFilePathResult) => {
22 if (result && result.isOwned !== true) {
24 .then(() => logger.debug('%s removed from %s', result.path, this.constructor.name))
25 .catch(err => logger.error('Cannot remove %s from cache %s.', result.path, this.constructor.name, { err }))