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