1 import { join } from 'path'
2 import { FILES_CACHE } from '../../initializers/constants'
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 import { CONFIG } from '../../initializers/config'
8 type GetPathParam = { videoId: string, language: string }
10 class VideosCaptionCache extends AbstractVideoStaticFileCache <GetPathParam> {
12 private static readonly KEY_DELIMITER = '%'
13 private static instance: VideosCaptionCache
15 private constructor () {
19 static get Instance () {
20 return this.instance || (this.instance = new this())
23 async getFilePath (params: GetPathParam) {
24 const videoCaption = await VideoCaptionModel.loadByVideoIdAndLanguage(params.videoId, params.language)
25 if (!videoCaption) return undefined
27 if (videoCaption.isOwned()) return join(CONFIG.STORAGE.CAPTIONS_DIR, videoCaption.getCaptionName())
29 const key = params.videoId + VideosCaptionCache.KEY_DELIMITER + params.language
30 return this.loadFromLRU(key)
33 protected async loadRemoteFile (key: string) {
34 const [ videoId, language ] = key.split(VideosCaptionCache.KEY_DELIMITER)
36 const videoCaption = await VideoCaptionModel.loadByVideoIdAndLanguage(videoId, language)
37 if (!videoCaption) return undefined
39 if (videoCaption.isOwned()) throw new Error('Cannot load remote caption of owned video.')
41 // Used to fetch the path
42 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoId)
43 if (!video) return undefined
45 const remoteStaticPath = videoCaption.getCaptionStaticPath()
46 const destPath = join(FILES_CACHE.VIDEO_CAPTIONS.DIRECTORY, videoCaption.getCaptionName())
48 return this.saveRemoteVideoFileAndReturnPath(video, remoteStaticPath, destPath)