1
0
Fork 0
mirror of https://github.com/Chocobozzz/PeerTube.git synced 2025-10-03 17:59:37 +02:00

Create a dedicated table to track video thumbnails

This commit is contained in:
Chocobozzz 2019-04-17 10:07:00 +02:00
parent 94565d52bb
commit e8bafea35b
No known key found for this signature in database
GPG key ID: 583A612D890159BE
21 changed files with 654 additions and 266 deletions

View file

@ -1,41 +1,29 @@
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'
import * as memoizee from 'memoizee'
export abstract class AbstractVideoStaticFileCache <T> {
protected lru
getFilePath: (params: T) => Promise<string>
abstract getFilePath (params: T): Promise<string>
abstract getFilePathImpl (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,
this.getFilePath = memoizee(this.getFilePathImpl, {
maxAge,
load: (key, cb) => {
this.loadRemoteFile(key)
.then(res => cb(null, res))
.catch(err => cb(err))
max,
promise: true,
dispose: (value: string) => {
remove(value)
.then(() => logger.debug('%s evicted from %s', value, this.constructor.name))
.catch(err => logger.error('Cannot remove %s from cache %s.', value, this.constructor.name, { 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) {