1 import * as request from 'request'
2 import * as asyncLRU from 'async-lru'
3 import { join } from 'path'
4 import { createWriteStream } from 'fs'
5 import * as Promise from 'bluebird'
7 import { database as db, CONFIG, CACHE } from '../../initializers'
8 import { logger, writeFilePromise, unlinkPromise } from '../../helpers'
9 import { VideoInstance } from '../../models'
10 import { fetchRemotePreview } from '../../lib'
12 class VideosPreviewCache {
14 private static instance: VideosPreviewCache
18 private constructor () { }
20 static get Instance () {
21 return this.instance || (this.instance = new this())
25 this.lru = new asyncLRU({
28 this.loadPreviews(key)
29 .then(res => cb(null, res))
30 .catch(err => cb(err))
34 this.lru.on('evict', (obj: { key: string, value: string }) => {
35 unlinkPromise(obj.value).then(() => logger.debug('%s evicted from VideosPreviewCache', obj.value))
39 getPreviewPath (key: string) {
40 return new Promise<string>((res, rej) => {
41 this.lru.get(key, (err, value) => {
42 err ? rej(err) : res(value)
47 private loadPreviews (key: string) {
48 return db.Video.loadByUUIDAndPopulateAuthorAndPodAndTags(key)
50 if (!video) return undefined
52 if (video.isOwned()) return join(CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName())
54 return this.saveRemotePreviewAndReturnPath(video)
58 private saveRemotePreviewAndReturnPath (video: VideoInstance) {
59 const req = fetchRemotePreview(video.Author.Pod, video)
61 return new Promise<string>((res, rej) => {
62 const path = join(CACHE.DIRECTORIES.PREVIEWS, video.getPreviewName())
63 const stream = createWriteStream(path)
66 .on('finish', () => res(path))
67 .on('error', (err) => rej(err))