1 import * as asyncLRU from 'async-lru'
2 import { join } from 'path'
3 import { createWriteStream } from 'fs'
4 import * as Promise from 'bluebird'
6 import { database as db, CONFIG, CACHE } from '../../initializers'
7 import { logger, unlinkPromise } from '../../helpers'
8 import { VideoInstance } from '../../models'
9 import { fetchRemotePreview } from '../../lib'
11 class VideosPreviewCache {
13 private static instance: VideosPreviewCache
17 private constructor () { }
19 static get Instance () {
20 return this.instance || (this.instance = new this())
24 this.lru = new asyncLRU({
27 this.loadPreviews(key)
28 .then(res => cb(null, res))
29 .catch(err => cb(err))
33 this.lru.on('evict', (obj: { key: string, value: string }) => {
34 unlinkPromise(obj.value).then(() => logger.debug('%s evicted from VideosPreviewCache', obj.value))
38 getPreviewPath (key: string) {
39 return new Promise<string>((res, rej) => {
40 this.lru.get(key, (err, value) => {
41 err ? rej(err) : res(value)
46 private loadPreviews (key: string) {
47 return db.Video.loadByUUIDAndPopulateAuthorAndPodAndTags(key)
49 if (!video) return undefined
51 if (video.isOwned()) return join(CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName())
53 return this.saveRemotePreviewAndReturnPath(video)
57 private saveRemotePreviewAndReturnPath (video: VideoInstance) {
58 const req = fetchRemotePreview(video)
60 return new Promise<string>((res, rej) => {
61 const path = join(CACHE.DIRECTORIES.PREVIEWS, video.getPreviewName())
62 const stream = createWriteStream(path)
65 .on('finish', () => res(path))
66 .on('error', (err) => rej(err))