]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/files-cache/videos-caption-cache.ts
Update sequelize
[github/Chocobozzz/PeerTube.git] / server / lib / files-cache / videos-caption-cache.ts
CommitLineData
40e87e9e 1import { join } from 'path'
74dc3bca 2import { FILES_CACHE } from '../../initializers/constants'
40e87e9e
C
3import { VideoModel } from '../../models/video/video'
4import { VideoCaptionModel } from '../../models/video/video-caption'
5import { AbstractVideoStaticFileCache } from './abstract-video-static-file-cache'
6dd9de95 6import { CONFIG } from '../../initializers/config'
40e87e9e
C
7
8type GetPathParam = { videoId: string, language: string }
9
10class VideosCaptionCache extends AbstractVideoStaticFileCache <GetPathParam> {
11
12 private static readonly KEY_DELIMITER = '%'
13 private static instance: VideosCaptionCache
14
15 private constructor () {
16 super()
17 }
18
19 static get Instance () {
20 return this.instance || (this.instance = new this())
21 }
22
e8bafea3 23 async getFilePathImpl (params: GetPathParam) {
40e87e9e
C
24 const videoCaption = await VideoCaptionModel.loadByVideoIdAndLanguage(params.videoId, params.language)
25 if (!videoCaption) return undefined
26
27 if (videoCaption.isOwned()) return join(CONFIG.STORAGE.CAPTIONS_DIR, videoCaption.getCaptionName())
28
29 const key = params.videoId + VideosCaptionCache.KEY_DELIMITER + params.language
e8bafea3 30 return this.loadRemoteFile(key)
40e87e9e
C
31 }
32
33 protected async loadRemoteFile (key: string) {
34 const [ videoId, language ] = key.split(VideosCaptionCache.KEY_DELIMITER)
35
36 const videoCaption = await VideoCaptionModel.loadByVideoIdAndLanguage(videoId, language)
37 if (!videoCaption) return undefined
38
39 if (videoCaption.isOwned()) throw new Error('Cannot load remote caption of owned video.')
40
41 // Used to fetch the path
627621c1 42 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoId)
40e87e9e
C
43 if (!video) return undefined
44
e8bafea3 45 // FIXME: use URL
40e87e9e 46 const remoteStaticPath = videoCaption.getCaptionStaticPath()
d74d29ad 47 const destPath = join(FILES_CACHE.VIDEO_CAPTIONS.DIRECTORY, videoCaption.getCaptionName())
40e87e9e
C
48
49 return this.saveRemoteVideoFileAndReturnPath(video, remoteStaticPath, destPath)
50 }
51}
52
53export {
54 VideosCaptionCache
55}