]>
Commit | Line | Data |
---|---|---|
40e87e9e | 1 | import { join } from 'path' |
cd25344f | 2 | import { logger } from '@server/helpers/logger' |
6302d599 C |
3 | import { doRequestAndSaveToFile } from '@server/helpers/requests' |
4 | import { CONFIG } from '../../initializers/config' | |
74dc3bca | 5 | import { FILES_CACHE } from '../../initializers/constants' |
40e87e9e C |
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 | ||
6302d599 | 10 | class VideosCaptionCache extends AbstractVideoStaticFileCache <string> { |
40e87e9e | 11 | |
40e87e9e C |
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 | ||
6302d599 C |
22 | async getFilePathImpl (filename: string) { |
23 | const videoCaption = await VideoCaptionModel.loadWithVideoByFilename(filename) | |
40e87e9e C |
24 | if (!videoCaption) return undefined |
25 | ||
6302d599 | 26 | if (videoCaption.isOwned()) return { isOwned: true, path: join(CONFIG.STORAGE.CAPTIONS_DIR, videoCaption.filename) } |
40e87e9e | 27 | |
6302d599 | 28 | return this.loadRemoteFile(filename) |
40e87e9e C |
29 | } |
30 | ||
6302d599 | 31 | // Key is the caption filename |
40e87e9e | 32 | protected async loadRemoteFile (key: string) { |
6302d599 | 33 | const videoCaption = await VideoCaptionModel.loadWithVideoByFilename(key) |
40e87e9e C |
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 | |
4fae2b1f | 39 | const video = await VideoModel.loadFull(videoCaption.videoId) |
40e87e9e C |
40 | if (!video) return undefined |
41 | ||
ca6d3622 | 42 | const remoteUrl = videoCaption.getFileUrl(video) |
6302d599 | 43 | const destPath = join(FILES_CACHE.VIDEO_CAPTIONS.DIRECTORY, videoCaption.filename) |
40e87e9e | 44 | |
cd25344f C |
45 | try { |
46 | await doRequestAndSaveToFile(remoteUrl, destPath) | |
3acc5084 | 47 | |
cd25344f C |
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 | } | |
40e87e9e C |
54 | } |
55 | } | |
56 | ||
57 | export { | |
58 | VideosCaptionCache | |
59 | } |