]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/files-cache/videos-preview-cache.ts
Create a dedicated table to track video thumbnails
[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
7 class VideosPreviewCache extends AbstractVideoStaticFileCache <string> {
8
9 private static instance: VideosPreviewCache
10
11 private constructor () {
12 super()
13 }
14
15 static get Instance () {
16 return this.instance || (this.instance = new this())
17 }
18
19 async getFilePathImpl (videoUUID: string) {
20 const video = await VideoModel.loadByUUIDWithFile(videoUUID)
21 if (!video) return undefined
22
23 if (video.isOwned()) return join(CONFIG.STORAGE.PREVIEWS_DIR, video.getPreview().filename)
24
25 return this.loadRemoteFile(videoUUID)
26 }
27
28 protected async loadRemoteFile (key: string) {
29 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(key)
30 if (!video) return undefined
31
32 if (video.isOwned()) throw new Error('Cannot load remote preview of owned video.')
33
34 // FIXME: use URL
35 const remoteStaticPath = join(STATIC_PATHS.PREVIEWS, video.getPreview().filename)
36 const destPath = join(FILES_CACHE.PREVIEWS.DIRECTORY, video.getPreview().filename)
37
38 return this.saveRemoteVideoFileAndReturnPath(video, remoteStaticPath, destPath)
39 }
40 }
41
42 export {
43 VideosPreviewCache
44 }