import { remove } from 'fs-extra' ; import { logger } from '../../helpers/logger' ; import memoizee from 'memoizee' ; type GetFilePathResult = { isOwned : boolean ; path : string ; downloadName ? : string ; } | undefined ; export abstract class AbstractVideoStaticFileCache < T > { getFilePath : ( params : T ) => Promise < GetFilePathResult > ; abstract getFilePathImpl ( params ) ; protected abstract loadRemoteFile ( key ) ; init ( max , maxAge ) { this . getFilePath = memoizee ( this . getFilePathImpl , { maxAge , max , promise : true , dispose : ( result ? ) => { 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 } ) ) ; } } } ) ; } }