]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/files-cache/videos-caption-cache.ts
fe5b441afc9ac934655eb7f3c1a3a5ddefe23deb
[github/Chocobozzz/PeerTube.git] / server / lib / files-cache / videos-caption-cache.ts
1 import { join } from 'path'
2 import { FILES_CACHE, CONFIG } from '../../initializers'
3 import { VideoModel } from '../../models/video/video'
4 import { VideoCaptionModel } from '../../models/video/video-caption'
5 import { AbstractVideoStaticFileCache } from './abstract-video-static-file-cache'
6
7 type GetPathParam = { videoId: string, language: string }
8
9 class VideosCaptionCache extends AbstractVideoStaticFileCache <GetPathParam> {
10
11 private static readonly KEY_DELIMITER = '%'
12 private static instance: VideosCaptionCache
13
14 private constructor () {
15 super()
16 }
17
18 static get Instance () {
19 return this.instance || (this.instance = new this())
20 }
21
22 async getFilePath (params: GetPathParam) {
23 const videoCaption = await VideoCaptionModel.loadByVideoIdAndLanguage(params.videoId, params.language)
24 if (!videoCaption) return undefined
25
26 if (videoCaption.isOwned()) return join(CONFIG.STORAGE.CAPTIONS_DIR, videoCaption.getCaptionName())
27
28 const key = params.videoId + VideosCaptionCache.KEY_DELIMITER + params.language
29 return this.loadFromLRU(key)
30 }
31
32 protected async loadRemoteFile (key: string) {
33 const [ videoId, language ] = key.split(VideosCaptionCache.KEY_DELIMITER)
34
35 const videoCaption = await VideoCaptionModel.loadByVideoIdAndLanguage(videoId, language)
36 if (!videoCaption) return undefined
37
38 if (videoCaption.isOwned()) throw new Error('Cannot load remote caption of owned video.')
39
40 // Used to fetch the path
41 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoId)
42 if (!video) return undefined
43
44 const remoteStaticPath = videoCaption.getCaptionStaticPath()
45 const destPath = join(FILES_CACHE.VIDEO_CAPTIONS.DIRECTORY, videoCaption.getCaptionName())
46
47 return this.saveRemoteVideoFileAndReturnPath(video, remoteStaticPath, destPath)
48 }
49 }
50
51 export {
52 VideosCaptionCache
53 }