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