import { join } from 'path' ; import { doRequestAndSaveToFile } from '@server/helpers/requests' ; import { CONFIG } from '../../initializers/config' ; import { FILES_CACHE } from '../../initializers/constants' ; import { VideoModel } from '../../models/video/video' ; import { VideoCaptionModel } from '../../models/video/video-caption' ; import { AbstractVideoStaticFileCache } from './abstract-video-static-file-cache' ; class VideosCaptionCache extends AbstractVideoStaticFileCache < string > { private static instance : VideosCaptionCache ; private constructor ( ) { super ( ) ; } static get Instance ( ) { return this . instance || ( this . instance = new this ( ) ) ; } async getFilePathImpl ( filename ) { const videoCaption = await VideoCaptionModel . loadWithVideoByFilename ( filename ) ; if ( ! videoCaption ) return undefined ; if ( videoCaption . isOwned ( ) ) return { isOwned : true , path : join ( CONFIG . STORAGE . CAPTIONS_DIR , videoCaption . filename ) } ; return this . loadRemoteFile ( filename ) ; } protected async loadRemoteFile ( key ) { const videoCaption = await VideoCaptionModel . loadWithVideoByFilename ( key ) ; if ( ! videoCaption ) return undefined ; if ( videoCaption . isOwned ( ) ) throw new Error ( 'Cannot load remote caption of owned video.' ) ; const video = await VideoModel . loadAndPopulateAccountAndServerAndTags ( videoCaption . videoId ) ; if ( ! video ) return undefined ; const remoteUrl = videoCaption . getFileUrl ( video ) ; const destPath = join ( FILES_CACHE . VIDEO_CAPTIONS . DIRECTORY , videoCaption . filename ) ; await doRequestAndSaveToFile ( remoteUrl , destPath ) ; return { isOwned : false , path : destPath } ; } } export { VideosCaptionCache } ;