1 import * as asyncLRU from 'async-lru'
2 import { createWriteStream } from 'fs'
3 import { join } from 'path'
4 import { logger, unlinkPromise } from '../../helpers'
5 import { CACHE, CONFIG } from '../../initializers'
6 import { VideoModel } from '../../models/video/video'
7 import { fetchRemoteVideoPreview } from '../activitypub'
9 class VideosPreviewCache {
11 private static instance: VideosPreviewCache
15 private constructor () { }
17 static get Instance () {
18 return this.instance || (this.instance = new this())
22 this.lru = new asyncLRU({
25 this.loadPreviews(key)
26 .then(res => cb(null, res))
27 .catch(err => cb(err))
31 this.lru.on('evict', (obj: { key: string, value: string }) => {
32 unlinkPromise(obj.value).then(() => logger.debug('%s evicted from VideosPreviewCache', obj.value))
36 async getPreviewPath (key: string) {
37 const video = await VideoModel.loadByUUID(key)
38 if (!video) return undefined
40 if (video.isOwned()) return join(CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName())
42 return new Promise<string>((res, rej) => {
43 this.lru.get(key, (err, value) => {
44 err ? rej(err) : res(value)
49 private async loadPreviews (key: string) {
50 const video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(key)
51 if (!video) return undefined
53 if (video.isOwned()) throw new Error('Cannot load preview of owned video.')
55 return this.saveRemotePreviewAndReturnPath(video)
58 private saveRemotePreviewAndReturnPath (video: VideoModel) {
59 return new Promise<string>((res, rej) => {
60 const req = fetchRemoteVideoPreview(video, rej)
61 const path = join(CACHE.DIRECTORIES.PREVIEWS, video.getPreviewName())
62 const stream = createWriteStream(path)
65 .on('error', (err) => rej(err))
66 .on('finish', () => res(path))