2018-07-12 12:02:00 -05:00
|
|
|
import { join } from 'path'
|
2019-04-11 04:33:44 -05:00
|
|
|
import { FILES_CACHE } from '../../initializers'
|
2018-07-12 12:02:00 -05:00
|
|
|
import { VideoModel } from '../../models/video/video'
|
|
|
|
import { VideoCaptionModel } from '../../models/video/video-caption'
|
|
|
|
import { AbstractVideoStaticFileCache } from './abstract-video-static-file-cache'
|
2019-04-11 04:33:44 -05:00
|
|
|
import { CONFIG } from '../../initializers/config'
|
2018-07-12 12:02:00 -05:00
|
|
|
|
|
|
|
type GetPathParam = { videoId: string, language: string }
|
|
|
|
|
|
|
|
class VideosCaptionCache extends AbstractVideoStaticFileCache <GetPathParam> {
|
|
|
|
|
|
|
|
private static readonly KEY_DELIMITER = '%'
|
|
|
|
private static instance: VideosCaptionCache
|
|
|
|
|
|
|
|
private constructor () {
|
|
|
|
super()
|
|
|
|
}
|
|
|
|
|
|
|
|
static get Instance () {
|
|
|
|
return this.instance || (this.instance = new this())
|
|
|
|
}
|
|
|
|
|
|
|
|
async getFilePath (params: GetPathParam) {
|
|
|
|
const videoCaption = await VideoCaptionModel.loadByVideoIdAndLanguage(params.videoId, params.language)
|
|
|
|
if (!videoCaption) return undefined
|
|
|
|
|
|
|
|
if (videoCaption.isOwned()) return join(CONFIG.STORAGE.CAPTIONS_DIR, videoCaption.getCaptionName())
|
|
|
|
|
|
|
|
const key = params.videoId + VideosCaptionCache.KEY_DELIMITER + params.language
|
|
|
|
return this.loadFromLRU(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
protected async loadRemoteFile (key: string) {
|
|
|
|
const [ videoId, language ] = key.split(VideosCaptionCache.KEY_DELIMITER)
|
|
|
|
|
|
|
|
const videoCaption = await VideoCaptionModel.loadByVideoIdAndLanguage(videoId, language)
|
|
|
|
if (!videoCaption) return undefined
|
|
|
|
|
|
|
|
if (videoCaption.isOwned()) throw new Error('Cannot load remote caption of owned video.')
|
|
|
|
|
|
|
|
// Used to fetch the path
|
2018-09-18 05:00:49 -05:00
|
|
|
const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoId)
|
2018-07-12 12:02:00 -05:00
|
|
|
if (!video) return undefined
|
|
|
|
|
|
|
|
const remoteStaticPath = videoCaption.getCaptionStaticPath()
|
2019-03-19 08:23:17 -05:00
|
|
|
const destPath = join(FILES_CACHE.VIDEO_CAPTIONS.DIRECTORY, videoCaption.getCaptionName())
|
2018-07-12 12:02:00 -05:00
|
|
|
|
|
|
|
return this.saveRemoteVideoFileAndReturnPath(video, remoteStaticPath, destPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
VideosCaptionCache
|
|
|
|
}
|