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