1 import { join } from 'path'
2 import { CACHE, CONFIG, STATIC_PATHS } from '../../initializers'
3 import { VideoModel } from '../../models/video/video'
4 import { AbstractVideoStaticFileCache } from './abstract-video-static-file-cache'
6 class VideosPreviewCache extends AbstractVideoStaticFileCache <string> {
8 private static instance: VideosPreviewCache
10 private constructor () {
14 static get Instance () {
15 return this.instance || (this.instance = new this())
18 async getFilePath (videoUUID: string) {
19 const video = await VideoModel.loadByUUID(videoUUID)
20 if (!video) return undefined
22 if (video.isOwned()) return join(CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName())
24 return this.loadFromLRU(videoUUID)
27 protected async loadRemoteFile (key: string) {
28 const video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(key)
29 if (!video) return undefined
31 if (video.isOwned()) throw new Error('Cannot load remote preview of owned video.')
33 const remoteStaticPath = join(STATIC_PATHS.PREVIEWS, video.getPreviewName())
34 const destPath = join(CACHE.PREVIEWS.DIRECTORY, video.getPreviewName())
36 return this.saveRemoteVideoFileAndReturnPath(video, remoteStaticPath, destPath)