X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Ffiles-cache%2Fvideos-caption-cache.ts;h=d21acf4ef7fa666c955590675643bb47d3f5c6b9;hb=b7221c1d94e088231ef28b11d95d45d781fbe41c;hp=fe5b441afc9ac934655eb7f3c1a3a5ddefe23deb;hpb=d74d29ad9e35929491cf37223398d2535ab23de0;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/files-cache/videos-caption-cache.ts b/server/lib/files-cache/videos-caption-cache.ts index fe5b441af..d21acf4ef 100644 --- a/server/lib/files-cache/videos-caption-cache.ts +++ b/server/lib/files-cache/videos-caption-cache.ts @@ -1,14 +1,14 @@ import { join } from 'path' -import { FILES_CACHE, CONFIG } from '../../initializers' +import { logger } from '@server/helpers/logger' +import { doRequestAndSaveToFile } from '@server/helpers/requests' +import { CONFIG } from '../../initializers/config' +import { FILES_CACHE } from '../../initializers/constants' import { VideoModel } from '../../models/video/video' import { VideoCaptionModel } from '../../models/video/video-caption' import { AbstractVideoStaticFileCache } from './abstract-video-static-file-cache' -type GetPathParam = { videoId: string, language: string } +class VideosCaptionCache extends AbstractVideoStaticFileCache { -class VideosCaptionCache extends AbstractVideoStaticFileCache { - - private static readonly KEY_DELIMITER = '%' private static instance: VideosCaptionCache private constructor () { @@ -19,32 +19,38 @@ class VideosCaptionCache extends AbstractVideoStaticFileCache { return this.instance || (this.instance = new this()) } - async getFilePath (params: GetPathParam) { - const videoCaption = await VideoCaptionModel.loadByVideoIdAndLanguage(params.videoId, params.language) + async getFilePathImpl (filename: string) { + const videoCaption = await VideoCaptionModel.loadWithVideoByFilename(filename) 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.filename) } - const key = params.videoId + VideosCaptionCache.KEY_DELIMITER + params.language - return this.loadFromLRU(key) + return this.loadRemoteFile(filename) } + // Key is the caption filename protected async loadRemoteFile (key: string) { - const [ videoId, language ] = key.split(VideosCaptionCache.KEY_DELIMITER) - - const videoCaption = await VideoCaptionModel.loadByVideoIdAndLanguage(videoId, language) + const videoCaption = await VideoCaptionModel.loadWithVideoByFilename(key) if (!videoCaption) return undefined if (videoCaption.isOwned()) throw new Error('Cannot load remote caption of owned video.') // Used to fetch the path - const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoId) + const video = await VideoModel.loadFull(videoCaption.videoId) if (!video) return undefined - const remoteStaticPath = videoCaption.getCaptionStaticPath() - const destPath = join(FILES_CACHE.VIDEO_CAPTIONS.DIRECTORY, videoCaption.getCaptionName()) + const remoteUrl = videoCaption.getFileUrl(video) + const destPath = join(FILES_CACHE.VIDEO_CAPTIONS.DIRECTORY, videoCaption.filename) + + try { + await doRequestAndSaveToFile(remoteUrl, destPath) + + return { isOwned: false, path: destPath } + } catch (err) { + logger.info('Cannot fetch remote caption file %s.', remoteUrl, { err }) - return this.saveRemoteVideoFileAndReturnPath(video, remoteStaticPath, destPath) + return undefined + } } }