PeerTube/server/lib/files-cache/videos-storyboard-cache.ts

54 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-06-01 07:51:16 -05:00
import { join } from 'path'
import { logger } from '@server/helpers/logger'
import { doRequestAndSaveToFile } from '@server/helpers/requests'
import { StoryboardModel } from '@server/models/video/storyboard'
import { FILES_CACHE } from '../../initializers/constants'
import { AbstractVideoStaticFileCache } from './abstract-video-static-file-cache'
class VideosStoryboardCache extends AbstractVideoStaticFileCache <string> {
private static instance: VideosStoryboardCache
private constructor () {
super()
}
static get Instance () {
return this.instance || (this.instance = new this())
}
async getFilePathImpl (filename: string) {
const storyboard = await StoryboardModel.loadWithVideoByFilename(filename)
if (!storyboard) return undefined
if (storyboard.Video.isOwned()) return { isOwned: true, path: storyboard.getPath() }
return this.loadRemoteFile(storyboard.filename)
}
// Key is the storyboard filename
protected async loadRemoteFile (key: string) {
const storyboard = await StoryboardModel.loadWithVideoByFilename(key)
if (!storyboard) return undefined
const destPath = join(FILES_CACHE.STORYBOARDS.DIRECTORY, storyboard.filename)
const remoteUrl = storyboard.getOriginFileUrl(storyboard.Video)
try {
await doRequestAndSaveToFile(remoteUrl, destPath)
logger.debug('Fetched remote storyboard %s to %s.', remoteUrl, destPath)
return { isOwned: false, path: destPath }
} catch (err) {
logger.info('Cannot fetch remote storyboard file %s.', remoteUrl, { err })
return undefined
}
}
}
export {
VideosStoryboardCache
}