]>
Commit | Line | Data |
---|---|---|
40e87e9e | 1 | import { join } from 'path' |
6302d599 C |
2 | import { doRequestAndSaveToFile } from '@server/helpers/requests' |
3 | import { CONFIG } from '../../initializers/config' | |
74dc3bca | 4 | import { FILES_CACHE } from '../../initializers/constants' |
40e87e9e C |
5 | import { VideoModel } from '../../models/video/video' |
6 | import { VideoCaptionModel } from '../../models/video/video-caption' | |
7 | import { AbstractVideoStaticFileCache } from './abstract-video-static-file-cache' | |
8 | ||
6302d599 | 9 | class VideosCaptionCache extends AbstractVideoStaticFileCache <string> { |
40e87e9e | 10 | |
40e87e9e C |
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 | ||
6302d599 C |
21 | async getFilePathImpl (filename: string) { |
22 | const videoCaption = await VideoCaptionModel.loadWithVideoByFilename(filename) | |
40e87e9e C |
23 | if (!videoCaption) return undefined |
24 | ||
6302d599 | 25 | if (videoCaption.isOwned()) return { isOwned: true, path: join(CONFIG.STORAGE.CAPTIONS_DIR, videoCaption.filename) } |
40e87e9e | 26 | |
6302d599 | 27 | return this.loadRemoteFile(filename) |
40e87e9e C |
28 | } |
29 | ||
6302d599 | 30 | // Key is the caption filename |
40e87e9e | 31 | protected async loadRemoteFile (key: string) { |
6302d599 | 32 | const videoCaption = await VideoCaptionModel.loadWithVideoByFilename(key) |
40e87e9e C |
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 | |
6302d599 | 38 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoCaption.videoId) |
40e87e9e C |
39 | if (!video) return undefined |
40 | ||
ca6d3622 | 41 | const remoteUrl = videoCaption.getFileUrl(video) |
6302d599 | 42 | const destPath = join(FILES_CACHE.VIDEO_CAPTIONS.DIRECTORY, videoCaption.filename) |
40e87e9e | 43 | |
db4b15f2 | 44 | await doRequestAndSaveToFile(remoteUrl, destPath) |
3acc5084 | 45 | |
dc852737 | 46 | return { isOwned: false, path: destPath } |
40e87e9e C |
47 | } |
48 | } | |
49 | ||
50 | export { | |
51 | VideosCaptionCache | |
52 | } |