mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2025-10-04 10:19:35 +02:00
Limit user tokens cache
This commit is contained in:
parent
9f79ade627
commit
d74d29ad9e
13 changed files with 36 additions and 25 deletions
|
@ -1,52 +0,0 @@
|
|||
import * as AsyncLRU from 'async-lru'
|
||||
import { createWriteStream, remove } from 'fs-extra'
|
||||
import { logger } from '../../helpers/logger'
|
||||
import { VideoModel } from '../../models/video/video'
|
||||
import { fetchRemoteVideoStaticFile } from '../activitypub'
|
||||
|
||||
export abstract class AbstractVideoStaticFileCache <T> {
|
||||
|
||||
protected lru
|
||||
|
||||
abstract getFilePath (params: T): Promise<string>
|
||||
|
||||
// Load and save the remote file, then return the local path from filesystem
|
||||
protected abstract loadRemoteFile (key: string): Promise<string>
|
||||
|
||||
init (max: number, maxAge: number) {
|
||||
this.lru = new AsyncLRU({
|
||||
max,
|
||||
maxAge,
|
||||
load: (key, cb) => {
|
||||
this.loadRemoteFile(key)
|
||||
.then(res => cb(null, res))
|
||||
.catch(err => cb(err))
|
||||
}
|
||||
})
|
||||
|
||||
this.lru.on('evict', (obj: { key: string, value: string }) => {
|
||||
remove(obj.value)
|
||||
.then(() => logger.debug('%s evicted from %s', obj.value, this.constructor.name))
|
||||
})
|
||||
}
|
||||
|
||||
protected loadFromLRU (key: string) {
|
||||
return new Promise<string>((res, rej) => {
|
||||
this.lru.get(key, (err, value) => {
|
||||
err ? rej(err) : res(value)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
protected saveRemoteVideoFileAndReturnPath (video: VideoModel, remoteStaticPath: string, destPath: string) {
|
||||
return new Promise<string>((res, rej) => {
|
||||
const req = fetchRemoteVideoStaticFile(video, remoteStaticPath, rej)
|
||||
|
||||
const stream = createWriteStream(destPath)
|
||||
|
||||
req.pipe(stream)
|
||||
.on('error', (err) => rej(err))
|
||||
.on('finish', () => res(destPath))
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue