1
0
Fork 0
peertube/server/lib/files-cache/abstract-video-static-file-...

31 lines
1.0 KiB
TypeScript
Raw Normal View History

2019-04-24 07:28:06 +00:00
import { remove } from 'fs-extra'
2018-07-12 17:02:00 +00:00
import { logger } from '../../helpers/logger'
import * as memoizee from 'memoizee'
2018-07-12 17:02:00 +00:00
2019-04-23 07:50:57 +00:00
type GetFilePathResult = { isOwned: boolean, path: string } | undefined
2018-07-12 17:02:00 +00:00
export abstract class AbstractVideoStaticFileCache <T> {
2019-04-23 07:50:57 +00:00
getFilePath: (params: T) => Promise<GetFilePathResult>
2018-07-12 17:02:00 +00:00
2019-04-23 07:50:57 +00:00
abstract getFilePathImpl (params: T): Promise<GetFilePathResult>
2018-07-12 17:02:00 +00:00
// Load and save the remote file, then return the local path from filesystem
2019-04-23 07:50:57 +00:00
protected abstract loadRemoteFile (key: string): Promise<GetFilePathResult>
2018-07-12 17:02:00 +00:00
2018-07-16 12:22:16 +00:00
init (max: number, maxAge: number) {
this.getFilePath = memoizee(this.getFilePathImpl, {
2018-07-16 12:22:16 +00:00
maxAge,
max,
promise: true,
2019-05-28 07:36:46 +00:00
dispose: (result?: GetFilePathResult) => {
if (result && result.isOwned !== true) {
2019-04-23 07:50:57 +00:00
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 }))
}
2018-07-12 17:02:00 +00:00
}
})
}
}