From 6302d599cdf98b5a5363a2a1dcdc266447950191 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 15 Feb 2021 14:08:16 +0100 Subject: Generate a name for caption files --- server/lib/files-cache/videos-caption-cache.ts | 30 ++++++++++---------------- server/lib/files-cache/videos-preview-cache.ts | 1 + 2 files changed, 12 insertions(+), 19 deletions(-) (limited to 'server/lib/files-cache') 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 @@ import { join } from 'path' +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' -import { CONFIG } from '../../initializers/config' -import { logger } from '../../helpers/logger' -import { doRequestAndSaveToFile } from '@server/helpers/requests' -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 () { @@ -22,32 +18,28 @@ class VideosCaptionCache extends AbstractVideoStaticFileCache { return this.instance || (this.instance = new this()) } - async getFilePathImpl (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 { isOwned: true, path: 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.loadRemoteFile(key) + return this.loadRemoteFile(filename) } + // Key is the caption filename 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) + 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.loadAndPopulateAccountAndServerAndTags(videoCaption.videoId) if (!video) return undefined const remoteUrl = videoCaption.getFileUrl(video) - const destPath = join(FILES_CACHE.VIDEO_CAPTIONS.DIRECTORY, videoCaption.getCaptionName()) + const destPath = join(FILES_CACHE.VIDEO_CAPTIONS.DIRECTORY, videoCaption.filename) await doRequestAndSaveToFile({ uri: remoteUrl }, destPath) diff --git a/server/lib/files-cache/videos-preview-cache.ts b/server/lib/files-cache/videos-preview-cache.ts index 51146d718..47488da74 100644 --- a/server/lib/files-cache/videos-preview-cache.ts +++ b/server/lib/files-cache/videos-preview-cache.ts @@ -28,6 +28,7 @@ class VideosPreviewCache extends AbstractVideoStaticFileCache { return this.loadRemoteFile(thumbnail.Video.uuid) } + // Key is the video UUID protected async loadRemoteFile (key: string) { const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(key) if (!video) return undefined -- cgit v1.2.3