2017-07-12 05:56:02 -04:00
|
|
|
import * as asyncLRU from 'async-lru'
|
|
|
|
import { createWriteStream } from 'fs'
|
2017-12-12 11:53:50 -05:00
|
|
|
import { join } from 'path'
|
2017-12-28 05:16:08 -05:00
|
|
|
import { unlinkPromise } from '../../helpers/core-utils'
|
|
|
|
import { logger } from '../../helpers/logger'
|
2017-12-12 11:53:50 -05:00
|
|
|
import { CACHE, CONFIG } from '../../initializers'
|
|
|
|
import { VideoModel } from '../../models/video/video'
|
|
|
|
import { fetchRemoteVideoPreview } from '../activitypub'
|
2017-07-12 05:56:02 -04:00
|
|
|
|
|
|
|
class VideosPreviewCache {
|
|
|
|
|
|
|
|
private static instance: VideosPreviewCache
|
|
|
|
|
|
|
|
private lru
|
|
|
|
|
|
|
|
private constructor () { }
|
|
|
|
|
|
|
|
static get Instance () {
|
|
|
|
return this.instance || (this.instance = new this())
|
|
|
|
}
|
|
|
|
|
|
|
|
init (max: number) {
|
|
|
|
this.lru = new asyncLRU({
|
|
|
|
max,
|
|
|
|
load: (key, cb) => {
|
|
|
|
this.loadPreviews(key)
|
|
|
|
.then(res => cb(null, res))
|
|
|
|
.catch(err => cb(err))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
this.lru.on('evict', (obj: { key: string, value: string }) => {
|
|
|
|
unlinkPromise(obj.value).then(() => logger.debug('%s evicted from VideosPreviewCache', obj.value))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-12-20 05:05:10 -05:00
|
|
|
async getPreviewPath (key: string) {
|
|
|
|
const video = await VideoModel.loadByUUID(key)
|
|
|
|
if (!video) return undefined
|
|
|
|
|
|
|
|
if (video.isOwned()) return join(CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName())
|
|
|
|
|
2017-07-12 05:56:02 -04:00
|
|
|
return new Promise<string>((res, rej) => {
|
|
|
|
this.lru.get(key, (err, value) => {
|
|
|
|
err ? rej(err) : res(value)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-10-25 10:03:33 -04:00
|
|
|
private async loadPreviews (key: string) {
|
2017-12-20 11:49:58 -05:00
|
|
|
const video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(key)
|
2017-10-25 10:03:33 -04:00
|
|
|
if (!video) return undefined
|
2017-07-12 05:56:02 -04:00
|
|
|
|
2017-12-20 05:05:10 -05:00
|
|
|
if (video.isOwned()) throw new Error('Cannot load preview of owned video.')
|
2017-07-12 05:56:02 -04:00
|
|
|
|
2017-12-27 14:03:37 -05:00
|
|
|
return this.saveRemotePreviewAndReturnPath(video)
|
2017-07-12 05:56:02 -04:00
|
|
|
}
|
|
|
|
|
2017-12-12 11:53:50 -05:00
|
|
|
private saveRemotePreviewAndReturnPath (video: VideoModel) {
|
2017-07-12 05:56:02 -04:00
|
|
|
return new Promise<string>((res, rej) => {
|
2017-12-27 14:03:37 -05:00
|
|
|
const req = fetchRemoteVideoPreview(video, rej)
|
2017-07-12 05:56:02 -04:00
|
|
|
const path = join(CACHE.DIRECTORIES.PREVIEWS, video.getPreviewName())
|
|
|
|
const stream = createWriteStream(path)
|
|
|
|
|
|
|
|
req.pipe(stream)
|
2017-12-27 14:03:37 -05:00
|
|
|
.on('error', (err) => rej(err))
|
|
|
|
.on('finish', () => res(path))
|
2017-07-12 05:56:02 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
VideosPreviewCache
|
|
|
|
}
|