2018-07-12 13:02:00 -04:00
|
|
|
import { join } from 'path'
|
2019-04-11 08:26:41 -04:00
|
|
|
import { FILES_CACHE } from '../../initializers/constants'
|
2018-07-12 13:02:00 -04: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 05:33:44 -04:00
|
|
|
import { CONFIG } from '../../initializers/config'
|
2019-04-23 03:50:57 -04:00
|
|
|
import { logger } from '../../helpers/logger'
|
2020-01-30 05:53:38 -05:00
|
|
|
import { doRequestAndSaveToFile } from '@server/helpers/requests'
|
2018-07-12 13:02:00 -04: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())
|
|
|
|
}
|
|
|
|
|
2019-04-17 04:07:00 -04:00
|
|
|
async getFilePathImpl (params: GetPathParam) {
|
2018-07-12 13:02:00 -04:00
|
|
|
const videoCaption = await VideoCaptionModel.loadByVideoIdAndLanguage(params.videoId, params.language)
|
|
|
|
if (!videoCaption) return undefined
|
|
|
|
|
2019-04-23 03:50:57 -04:00
|
|
|
if (videoCaption.isOwned()) return { isOwned: true, path: join(CONFIG.STORAGE.CAPTIONS_DIR, videoCaption.getCaptionName()) }
|
2018-07-12 13:02:00 -04:00
|
|
|
|
|
|
|
const key = params.videoId + VideosCaptionCache.KEY_DELIMITER + params.language
|
2019-04-17 04:07:00 -04:00
|
|
|
return this.loadRemoteFile(key)
|
2018-07-12 13:02:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
protected async loadRemoteFile (key: string) {
|
2019-04-23 03:50:57 -04:00
|
|
|
logger.debug('Loading remote caption file %s.', key)
|
|
|
|
|
2018-07-12 13:02:00 -04:00
|
|
|
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 06:00:49 -04:00
|
|
|
const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoId)
|
2018-07-12 13:02:00 -04:00
|
|
|
if (!video) return undefined
|
|
|
|
|
2020-01-30 05:53:38 -05:00
|
|
|
const remoteUrl = videoCaption.getFileUrl(video)
|
2019-03-19 09:23:17 -04:00
|
|
|
const destPath = join(FILES_CACHE.VIDEO_CAPTIONS.DIRECTORY, videoCaption.getCaptionName())
|
2018-07-12 13:02:00 -04:00
|
|
|
|
2020-01-30 05:53:38 -05:00
|
|
|
await doRequestAndSaveToFile({ uri: remoteUrl }, destPath)
|
2019-04-23 03:50:57 -04:00
|
|
|
|
2019-04-24 03:28:06 -04:00
|
|
|
return { isOwned: false, path: destPath }
|
2018-07-12 13:02:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
VideosCaptionCache
|
|
|
|
}
|