]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/files-cache/videos-preview-cache.ts
Fix tests
[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'
dc852737 6import { fetchRemoteVideoStaticFile } from '../activitypub'
f981dae8 7
40e87e9e 8class VideosPreviewCache extends AbstractVideoStaticFileCache <string> {
f981dae8
C
9
10 private static instance: VideosPreviewCache
11
40e87e9e
C
12 private constructor () {
13 super()
14 }
f981dae8
C
15
16 static get Instance () {
17 return this.instance || (this.instance = new this())
18 }
19
e8bafea3 20 async getFilePathImpl (videoUUID: string) {
e2600d8b 21 const video = await VideoModel.loadByUUID(videoUUID)
8fa5653a
C
22 if (!video) return undefined
23
536598cf 24 if (video.isOwned()) return { isOwned: true, path: video.getPreview().getPath() }
8fa5653a 25
e8bafea3 26 return this.loadRemoteFile(videoUUID)
f981dae8
C
27 }
28
40e87e9e 29 protected async loadRemoteFile (key: string) {
627621c1 30 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(key)
f5028693 31 if (!video) return undefined
f981dae8 32
40e87e9e 33 if (video.isOwned()) throw new Error('Cannot load remote preview of owned video.')
f981dae8 34
e8bafea3
C
35 // FIXME: use URL
36 const remoteStaticPath = join(STATIC_PATHS.PREVIEWS, video.getPreview().filename)
37 const destPath = join(FILES_CACHE.PREVIEWS.DIRECTORY, video.getPreview().filename)
f981dae8 38
dc852737 39 await fetchRemoteVideoStaticFile(video, remoteStaticPath, destPath)
3acc5084 40
dc852737 41 return { isOwned: false, path: destPath }
f981dae8
C
42 }
43}
44
45export {
46 VideosPreviewCache
47}