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