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