2018-07-12 19:02:00 +02:00
|
|
|
import { join } from 'path'
|
2021-02-15 14:08:16 +01:00
|
|
|
import { doRequestAndSaveToFile } from '@server/helpers/requests'
|
|
|
|
import { CONFIG } from '../../initializers/config'
|
2019-04-11 14:26:41 +02:00
|
|
|
import { FILES_CACHE } from '../../initializers/constants'
|
2018-07-12 19:02:00 +02:00
|
|
|
import { VideoModel } from '../../models/video/video'
|
|
|
|
import { VideoCaptionModel } from '../../models/video/video-caption'
|
|
|
|
import { AbstractVideoStaticFileCache } from './abstract-video-static-file-cache'
|
|
|
|
|
2021-02-15 14:08:16 +01:00
|
|
|
class VideosCaptionCache extends AbstractVideoStaticFileCache <string> {
|
2018-07-12 19:02:00 +02:00
|
|
|
|
|
|
|
private static instance: VideosCaptionCache
|
|
|
|
|
|
|
|
private constructor () {
|
|
|
|
super()
|
|
|
|
}
|
|
|
|
|
|
|
|
static get Instance () {
|
|
|
|
return this.instance || (this.instance = new this())
|
|
|
|
}
|
|
|
|
|
2021-02-15 14:08:16 +01:00
|
|
|
async getFilePathImpl (filename: string) {
|
|
|
|
const videoCaption = await VideoCaptionModel.loadWithVideoByFilename(filename)
|
2018-07-12 19:02:00 +02:00
|
|
|
if (!videoCaption) return undefined
|
|
|
|
|
2021-02-15 14:08:16 +01:00
|
|
|
if (videoCaption.isOwned()) return { isOwned: true, path: join(CONFIG.STORAGE.CAPTIONS_DIR, videoCaption.filename) }
|
2018-07-12 19:02:00 +02:00
|
|
|
|
2021-02-15 14:08:16 +01:00
|
|
|
return this.loadRemoteFile(filename)
|
2018-07-12 19:02:00 +02:00
|
|
|
}
|
|
|
|
|
2021-02-15 14:08:16 +01:00
|
|
|
// Key is the caption filename
|
2018-07-12 19:02:00 +02:00
|
|
|
protected async loadRemoteFile (key: string) {
|
2021-02-15 14:08:16 +01:00
|
|
|
const videoCaption = await VideoCaptionModel.loadWithVideoByFilename(key)
|
2018-07-12 19:02:00 +02:00
|
|
|
if (!videoCaption) return undefined
|
|
|
|
|
|
|
|
if (videoCaption.isOwned()) throw new Error('Cannot load remote caption of owned video.')
|
|
|
|
|
|
|
|
// Used to fetch the path
|
2021-02-15 14:08:16 +01:00
|
|
|
const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoCaption.videoId)
|
2018-07-12 19:02:00 +02:00
|
|
|
if (!video) return undefined
|
|
|
|
|
2020-01-30 11:53:38 +01:00
|
|
|
const remoteUrl = videoCaption.getFileUrl(video)
|
2021-02-15 14:08:16 +01:00
|
|
|
const destPath = join(FILES_CACHE.VIDEO_CAPTIONS.DIRECTORY, videoCaption.filename)
|
2018-07-12 19:02:00 +02:00
|
|
|
|
2020-01-30 11:53:38 +01:00
|
|
|
await doRequestAndSaveToFile({ uri: remoteUrl }, destPath)
|
2019-04-23 09:50:57 +02:00
|
|
|
|
2019-04-24 09:28:06 +02:00
|
|
|
return { isOwned: false, path: destPath }
|
2018-07-12 19:02:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
VideosCaptionCache
|
|
|
|
}
|