]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/files-cache/videos-caption-cache.ts
Add last login date to users
[github/Chocobozzz/PeerTube.git] / server / lib / files-cache / videos-caption-cache.ts
index 0926f400952a37e6738191621de7d9224372de2c..26ab3bd0d0c621b35b84f005fb69ccd059f76361 100644 (file)
@@ -4,6 +4,8 @@ import { VideoModel } from '../../models/video/video'
 import { VideoCaptionModel } from '../../models/video/video-caption'
 import { AbstractVideoStaticFileCache } from './abstract-video-static-file-cache'
 import { CONFIG } from '../../initializers/config'
+import { logger } from '../../helpers/logger'
+import { doRequestAndSaveToFile } from '@server/helpers/requests'
 
 type GetPathParam = { videoId: string, language: string }
 
@@ -20,17 +22,19 @@ class VideosCaptionCache extends AbstractVideoStaticFileCache <GetPathParam> {
     return this.instance || (this.instance = new this())
   }
 
-  async getFilePath (params: GetPathParam) {
+  async getFilePathImpl (params: GetPathParam) {
     const videoCaption = await VideoCaptionModel.loadByVideoIdAndLanguage(params.videoId, params.language)
     if (!videoCaption) return undefined
 
-    if (videoCaption.isOwned()) return join(CONFIG.STORAGE.CAPTIONS_DIR, videoCaption.getCaptionName())
+    if (videoCaption.isOwned()) return { isOwned: true, path: join(CONFIG.STORAGE.CAPTIONS_DIR, videoCaption.getCaptionName()) }
 
     const key = params.videoId + VideosCaptionCache.KEY_DELIMITER + params.language
-    return this.loadFromLRU(key)
+    return this.loadRemoteFile(key)
   }
 
   protected async loadRemoteFile (key: string) {
+    logger.debug('Loading remote caption file %s.', key)
+
     const [ videoId, language ] = key.split(VideosCaptionCache.KEY_DELIMITER)
 
     const videoCaption = await VideoCaptionModel.loadByVideoIdAndLanguage(videoId, language)
@@ -42,10 +46,12 @@ class VideosCaptionCache extends AbstractVideoStaticFileCache <GetPathParam> {
     const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoId)
     if (!video) return undefined
 
-    const remoteStaticPath = videoCaption.getCaptionStaticPath()
+    const remoteUrl = videoCaption.getFileUrl(video)
     const destPath = join(FILES_CACHE.VIDEO_CAPTIONS.DIRECTORY, videoCaption.getCaptionName())
 
-    return this.saveRemoteVideoFileAndReturnPath(video, remoteStaticPath, destPath)
+    await doRequestAndSaveToFile({ uri: remoteUrl }, destPath)
+
+    return { isOwned: false, path: destPath }
   }
 }