From dc8527376482293c87fc6f30027d626f58a1197b Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 24 Apr 2019 09:28:06 +0200 Subject: Refactor video caption/preview caches --- .../lib/files-cache/abstract-video-static-file-cache.ts | 16 +--------------- server/lib/files-cache/videos-caption-cache.ts | 5 +++-- server/lib/files-cache/videos-preview-cache.ts | 5 +++-- 3 files changed, 7 insertions(+), 19 deletions(-) (limited to 'server/lib/files-cache') diff --git a/server/lib/files-cache/abstract-video-static-file-cache.ts b/server/lib/files-cache/abstract-video-static-file-cache.ts index 84ed74c98..1908cfb06 100644 --- a/server/lib/files-cache/abstract-video-static-file-cache.ts +++ b/server/lib/files-cache/abstract-video-static-file-cache.ts @@ -1,7 +1,5 @@ -import { createWriteStream, remove } from 'fs-extra' +import { remove } from 'fs-extra' import { logger } from '../../helpers/logger' -import { VideoModel } from '../../models/video/video' -import { fetchRemoteVideoStaticFile } from '../activitypub' import * as memoizee from 'memoizee' type GetFilePathResult = { isOwned: boolean, path: string } | undefined @@ -29,16 +27,4 @@ export abstract class AbstractVideoStaticFileCache { } }) } - - protected saveRemoteVideoFileAndReturnPath (video: VideoModel, remoteStaticPath: string, destPath: string) { - return new Promise((res, rej) => { - const req = fetchRemoteVideoStaticFile(video, remoteStaticPath, rej) - - const stream = createWriteStream(destPath) - - req.pipe(stream) - .on('error', (err) => rej(err)) - .on('finish', () => res(destPath)) - }) - } } diff --git a/server/lib/files-cache/videos-caption-cache.ts b/server/lib/files-cache/videos-caption-cache.ts index 305e39c35..440c3fde8 100644 --- a/server/lib/files-cache/videos-caption-cache.ts +++ b/server/lib/files-cache/videos-caption-cache.ts @@ -5,6 +5,7 @@ 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 { fetchRemoteVideoStaticFile } from '../activitypub' type GetPathParam = { videoId: string, language: string } @@ -49,9 +50,9 @@ class VideosCaptionCache extends AbstractVideoStaticFileCache { const remoteStaticPath = videoCaption.getCaptionStaticPath() const destPath = join(FILES_CACHE.VIDEO_CAPTIONS.DIRECTORY, videoCaption.getCaptionName()) - const path = await this.saveRemoteVideoFileAndReturnPath(video, remoteStaticPath, destPath) + await fetchRemoteVideoStaticFile(video, remoteStaticPath, destPath) - return { isOwned: false, path } + return { isOwned: false, path: destPath } } } diff --git a/server/lib/files-cache/videos-preview-cache.ts b/server/lib/files-cache/videos-preview-cache.ts index c117ae426..14be7f24a 100644 --- a/server/lib/files-cache/videos-preview-cache.ts +++ b/server/lib/files-cache/videos-preview-cache.ts @@ -3,6 +3,7 @@ import { FILES_CACHE, STATIC_PATHS } from '../../initializers/constants' import { VideoModel } from '../../models/video/video' import { AbstractVideoStaticFileCache } from './abstract-video-static-file-cache' import { CONFIG } from '../../initializers/config' +import { fetchRemoteVideoStaticFile } from '../activitypub' class VideosPreviewCache extends AbstractVideoStaticFileCache { @@ -35,9 +36,9 @@ class VideosPreviewCache extends AbstractVideoStaticFileCache { const remoteStaticPath = join(STATIC_PATHS.PREVIEWS, video.getPreview().filename) const destPath = join(FILES_CACHE.PREVIEWS.DIRECTORY, video.getPreview().filename) - const path = await this.saveRemoteVideoFileAndReturnPath(video, remoteStaticPath, destPath) + await fetchRemoteVideoStaticFile(video, remoteStaticPath, destPath) - return { isOwned: false, path } + return { isOwned: false, path: destPath } } } -- cgit v1.2.3