diff options
Diffstat (limited to 'server/lib/files-cache/videos-caption-cache.ts')
-rw-r--r-- | server/lib/files-cache/videos-caption-cache.ts | 30 |
1 files changed, 11 insertions, 19 deletions
diff --git a/server/lib/files-cache/videos-caption-cache.ts b/server/lib/files-cache/videos-caption-cache.ts index 26ab3bd0d..ee0447010 100644 --- a/server/lib/files-cache/videos-caption-cache.ts +++ b/server/lib/files-cache/videos-caption-cache.ts | |||
@@ -1,17 +1,13 @@ | |||
1 | import { join } from 'path' | 1 | import { join } from 'path' |
2 | import { doRequestAndSaveToFile } from '@server/helpers/requests' | ||
3 | import { CONFIG } from '../../initializers/config' | ||
2 | import { FILES_CACHE } from '../../initializers/constants' | 4 | import { FILES_CACHE } from '../../initializers/constants' |
3 | import { VideoModel } from '../../models/video/video' | 5 | import { VideoModel } from '../../models/video/video' |
4 | import { VideoCaptionModel } from '../../models/video/video-caption' | 6 | import { VideoCaptionModel } from '../../models/video/video-caption' |
5 | import { AbstractVideoStaticFileCache } from './abstract-video-static-file-cache' | 7 | import { AbstractVideoStaticFileCache } from './abstract-video-static-file-cache' |
6 | import { CONFIG } from '../../initializers/config' | ||
7 | import { logger } from '../../helpers/logger' | ||
8 | import { doRequestAndSaveToFile } from '@server/helpers/requests' | ||
9 | 8 | ||
10 | type GetPathParam = { videoId: string, language: string } | 9 | class VideosCaptionCache extends AbstractVideoStaticFileCache <string> { |
11 | 10 | ||
12 | class VideosCaptionCache extends AbstractVideoStaticFileCache <GetPathParam> { | ||
13 | |||
14 | private static readonly KEY_DELIMITER = '%' | ||
15 | private static instance: VideosCaptionCache | 11 | private static instance: VideosCaptionCache |
16 | 12 | ||
17 | private constructor () { | 13 | private constructor () { |
@@ -22,32 +18,28 @@ class VideosCaptionCache extends AbstractVideoStaticFileCache <GetPathParam> { | |||
22 | return this.instance || (this.instance = new this()) | 18 | return this.instance || (this.instance = new this()) |
23 | } | 19 | } |
24 | 20 | ||
25 | async getFilePathImpl (params: GetPathParam) { | 21 | async getFilePathImpl (filename: string) { |
26 | const videoCaption = await VideoCaptionModel.loadByVideoIdAndLanguage(params.videoId, params.language) | 22 | const videoCaption = await VideoCaptionModel.loadWithVideoByFilename(filename) |
27 | if (!videoCaption) return undefined | 23 | if (!videoCaption) return undefined |
28 | 24 | ||
29 | if (videoCaption.isOwned()) return { isOwned: true, path: join(CONFIG.STORAGE.CAPTIONS_DIR, videoCaption.getCaptionName()) } | 25 | if (videoCaption.isOwned()) return { isOwned: true, path: join(CONFIG.STORAGE.CAPTIONS_DIR, videoCaption.filename) } |
30 | 26 | ||
31 | const key = params.videoId + VideosCaptionCache.KEY_DELIMITER + params.language | 27 | return this.loadRemoteFile(filename) |
32 | return this.loadRemoteFile(key) | ||
33 | } | 28 | } |
34 | 29 | ||
30 | // Key is the caption filename | ||
35 | protected async loadRemoteFile (key: string) { | 31 | protected async loadRemoteFile (key: string) { |
36 | logger.debug('Loading remote caption file %s.', key) | 32 | const videoCaption = await VideoCaptionModel.loadWithVideoByFilename(key) |
37 | |||
38 | const [ videoId, language ] = key.split(VideosCaptionCache.KEY_DELIMITER) | ||
39 | |||
40 | const videoCaption = await VideoCaptionModel.loadByVideoIdAndLanguage(videoId, language) | ||
41 | if (!videoCaption) return undefined | 33 | if (!videoCaption) return undefined |
42 | 34 | ||
43 | if (videoCaption.isOwned()) throw new Error('Cannot load remote caption of owned video.') | 35 | if (videoCaption.isOwned()) throw new Error('Cannot load remote caption of owned video.') |
44 | 36 | ||
45 | // Used to fetch the path | 37 | // Used to fetch the path |
46 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoId) | 38 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoCaption.videoId) |
47 | if (!video) return undefined | 39 | if (!video) return undefined |
48 | 40 | ||
49 | const remoteUrl = videoCaption.getFileUrl(video) | 41 | const remoteUrl = videoCaption.getFileUrl(video) |
50 | const destPath = join(FILES_CACHE.VIDEO_CAPTIONS.DIRECTORY, videoCaption.getCaptionName()) | 42 | const destPath = join(FILES_CACHE.VIDEO_CAPTIONS.DIRECTORY, videoCaption.filename) |
51 | 43 | ||
52 | await doRequestAndSaveToFile({ uri: remoteUrl }, destPath) | 44 | await doRequestAndSaveToFile({ uri: remoteUrl }, destPath) |
53 | 45 | ||