]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/lib/cache/videos-preview-cache.ts
Begin advanced search
[github/Chocobozzz/PeerTube.git] / server / lib / cache / videos-preview-cache.ts
... / ...
CommitLineData
1import { join } from 'path'
2import { CACHE, CONFIG, STATIC_PATHS } from '../../initializers'
3import { VideoModel } from '../../models/video/video'
4import { AbstractVideoStaticFileCache } from './abstract-video-static-file-cache'
5
6class VideosPreviewCache extends AbstractVideoStaticFileCache <string> {
7
8 private static instance: VideosPreviewCache
9
10 private constructor () {
11 super()
12 }
13
14 static get Instance () {
15 return this.instance || (this.instance = new this())
16 }
17
18 async getFilePath (videoUUID: string) {
19 const video = await VideoModel.loadByUUID(videoUUID)
20 if (!video) return undefined
21
22 if (video.isOwned()) return join(CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName())
23
24 return this.loadFromLRU(videoUUID)
25 }
26
27 protected async loadRemoteFile (key: string) {
28 const video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(key)
29 if (!video) return undefined
30
31 if (video.isOwned()) throw new Error('Cannot load remote preview of owned video.')
32
33 const remoteStaticPath = join(STATIC_PATHS.PREVIEWS, video.getPreviewName())
34 const destPath = join(CACHE.PREVIEWS.DIRECTORY, video.getPreviewName())
35
36 return this.saveRemoteVideoFileAndReturnPath(video, remoteStaticPath, destPath)
37 }
38}
39
40export {
41 VideosPreviewCache
42}