]>
Commit | Line | Data |
---|---|---|
1 | import { remove } from 'fs-extra' | |
2 | import { logger } from '../../helpers/logger' | |
3 | import * as memoizee from 'memoizee' | |
4 | ||
5 | type GetFilePathResult = { isOwned: boolean, path: string } | undefined | |
6 | ||
7 | export abstract class AbstractVideoStaticFileCache <T> { | |
8 | ||
9 | getFilePath: (params: T) => Promise<GetFilePathResult> | |
10 | ||
11 | abstract getFilePathImpl (params: T): Promise<GetFilePathResult> | |
12 | ||
13 | // Load and save the remote file, then return the local path from filesystem | |
14 | protected abstract loadRemoteFile (key: string): Promise<GetFilePathResult> | |
15 | ||
16 | init (max: number, maxAge: number) { | |
17 | this.getFilePath = memoizee(this.getFilePathImpl, { | |
18 | maxAge, | |
19 | max, | |
20 | promise: true, | |
21 | dispose: (result?: GetFilePathResult) => { | |
22 | if (result && result.isOwned !== true) { | |
23 | remove(result.path) | |
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 })) | |
26 | } | |
27 | } | |
28 | }) | |
29 | } | |
30 | } |