]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/files-cache/videos-preview-cache.ts
Update sequelize
[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
e8bafea3 19 async getFilePathImpl (videoUUID: string) {
627621c1 20 const video = await VideoModel.loadByUUIDWithFile(videoUUID)
8fa5653a
C
21 if (!video) return undefined
22
e8bafea3 23 if (video.isOwned()) return join(CONFIG.STORAGE.PREVIEWS_DIR, video.getPreview().filename)
8fa5653a 24
e8bafea3 25 return this.loadRemoteFile(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
e8bafea3
C
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)
f981dae8 37
40e87e9e 38 return this.saveRemoteVideoFileAndReturnPath(video, remoteStaticPath, destPath)
f981dae8
C
39 }
40}
41
42export {
43 VideosPreviewCache
44}