]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/lib/files-cache/videos-caption-cache.ts
Fix email action button label for reports
[github/Chocobozzz/PeerTube.git] / server / lib / files-cache / videos-caption-cache.ts
... / ...
CommitLineData
1import { join } from 'path'
2import { doRequestAndSaveToFile } from '@server/helpers/requests'
3import { CONFIG } from '../../initializers/config'
4import { FILES_CACHE } from '../../initializers/constants'
5import { VideoModel } from '../../models/video/video'
6import { VideoCaptionModel } from '../../models/video/video-caption'
7import { AbstractVideoStaticFileCache } from './abstract-video-static-file-cache'
8
9class VideosCaptionCache extends AbstractVideoStaticFileCache <string> {
10
11 private static instance: VideosCaptionCache
12
13 private constructor () {
14 super()
15 }
16
17 static get Instance () {
18 return this.instance || (this.instance = new this())
19 }
20
21 async getFilePathImpl (filename: string) {
22 const videoCaption = await VideoCaptionModel.loadWithVideoByFilename(filename)
23 if (!videoCaption) return undefined
24
25 if (videoCaption.isOwned()) return { isOwned: true, path: join(CONFIG.STORAGE.CAPTIONS_DIR, videoCaption.filename) }
26
27 return this.loadRemoteFile(filename)
28 }
29
30 // Key is the caption filename
31 protected async loadRemoteFile (key: string) {
32 const videoCaption = await VideoCaptionModel.loadWithVideoByFilename(key)
33 if (!videoCaption) return undefined
34
35 if (videoCaption.isOwned()) throw new Error('Cannot load remote caption of owned video.')
36
37 // Used to fetch the path
38 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoCaption.videoId)
39 if (!video) return undefined
40
41 const remoteUrl = videoCaption.getFileUrl(video)
42 const destPath = join(FILES_CACHE.VIDEO_CAPTIONS.DIRECTORY, videoCaption.filename)
43
44 await doRequestAndSaveToFile(remoteUrl, destPath)
45
46 return { isOwned: false, path: destPath }
47 }
48}
49
50export {
51 VideosCaptionCache
52}