]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/files-cache/videos-preview-cache.ts
Merge branch 'release/v1.3.0' into develop
[github/Chocobozzz/PeerTube.git] / server / lib / files-cache / videos-preview-cache.ts
1 import { join } from 'path'
2 import { FILES_CACHE, STATIC_PATHS } from '../../initializers/constants'
3 import { VideoModel } from '../../models/video/video'
4 import { AbstractVideoStaticFileCache } from './abstract-video-static-file-cache'
5 import { CONFIG } from '../../initializers/config'
6 import { fetchRemoteVideoStaticFile } from '../activitypub'
7
8 class VideosPreviewCache extends AbstractVideoStaticFileCache <string> {
9
10 private static instance: VideosPreviewCache
11
12 private constructor () {
13 super()
14 }
15
16 static get Instance () {
17 return this.instance || (this.instance = new this())
18 }
19
20 async getFilePathImpl (videoUUID: string) {
21 const video = await VideoModel.loadByUUIDWithFile(videoUUID)
22 if (!video) return undefined
23
24 if (video.isOwned()) return { isOwned: true, path: video.getPreview().getPath() }
25
26 return this.loadRemoteFile(videoUUID)
27 }
28
29 protected async loadRemoteFile (key: string) {
30 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(key)
31 if (!video) return undefined
32
33 if (video.isOwned()) throw new Error('Cannot load remote preview of owned video.')
34
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)
38
39 await fetchRemoteVideoStaticFile(video, remoteStaticPath, destPath)
40
41 return { isOwned: false, path: destPath }
42 }
43 }
44
45 export {
46 VideosPreviewCache
47 }