]>
Commit | Line | Data |
---|---|---|
1 | import { join } from 'path' | |
2 | import { logger } from '@server/helpers/logger' | |
3 | import { doRequestAndSaveToFile } from '@server/helpers/requests' | |
4 | import { CONFIG } from '../../initializers/config' | |
5 | import { FILES_CACHE } from '../../initializers/constants' | |
6 | import { VideoModel } from '../../models/video/video' | |
7 | import { VideoCaptionModel } from '../../models/video/video-caption' | |
8 | import { AbstractVideoStaticFileCache } from './abstract-video-static-file-cache' | |
9 | ||
10 | class VideosCaptionCache extends AbstractVideoStaticFileCache <string> { | |
11 | ||
12 | private static instance: VideosCaptionCache | |
13 | ||
14 | private constructor () { | |
15 | super() | |
16 | } | |
17 | ||
18 | static get Instance () { | |
19 | return this.instance || (this.instance = new this()) | |
20 | } | |
21 | ||
22 | async getFilePathImpl (filename: string) { | |
23 | const videoCaption = await VideoCaptionModel.loadWithVideoByFilename(filename) | |
24 | if (!videoCaption) return undefined | |
25 | ||
26 | if (videoCaption.isOwned()) return { isOwned: true, path: join(CONFIG.STORAGE.CAPTIONS_DIR, videoCaption.filename) } | |
27 | ||
28 | return this.loadRemoteFile(filename) | |
29 | } | |
30 | ||
31 | // Key is the caption filename | |
32 | protected async loadRemoteFile (key: string) { | |
33 | const videoCaption = await VideoCaptionModel.loadWithVideoByFilename(key) | |
34 | if (!videoCaption) return undefined | |
35 | ||
36 | if (videoCaption.isOwned()) throw new Error('Cannot load remote caption of owned video.') | |
37 | ||
38 | // Used to fetch the path | |
39 | const video = await VideoModel.loadFull(videoCaption.videoId) | |
40 | if (!video) return undefined | |
41 | ||
42 | const remoteUrl = videoCaption.getFileUrl(video) | |
43 | const destPath = join(FILES_CACHE.VIDEO_CAPTIONS.DIRECTORY, videoCaption.filename) | |
44 | ||
45 | try { | |
46 | await doRequestAndSaveToFile(remoteUrl, destPath) | |
47 | ||
48 | return { isOwned: false, path: destPath } | |
49 | } catch (err) { | |
50 | logger.info('Cannot fetch remote caption file %s.', remoteUrl, { err }) | |
51 | ||
52 | return undefined | |
53 | } | |
54 | } | |
55 | } | |
56 | ||
57 | export { | |
58 | VideosCaptionCache | |
59 | } |