]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Refactor video caption/preview caches
authorChocobozzz <me@florianbigard.com>
Wed, 24 Apr 2019 07:28:06 +0000 (09:28 +0200)
committerChocobozzz <me@florianbigard.com>
Wed, 24 Apr 2019 14:26:21 +0000 (16:26 +0200)
server/lib/activitypub/videos.ts
server/lib/files-cache/abstract-video-static-file-cache.ts
server/lib/files-cache/videos-caption-cache.ts
server/lib/files-cache/videos-preview-cache.ts

index 63bb07ec116dc7b14194559aeb59471a3b4618e1..4f26cb6bec986fbf6b793afb108be0bd30b351e7 100644 (file)
@@ -15,7 +15,7 @@ import { sanitizeAndCheckVideoTorrentObject } from '../../helpers/custom-validat
 import { isVideoFileInfoHashValid } from '../../helpers/custom-validators/videos'
 import { resetSequelizeInstance, retryTransactionWrapper } from '../../helpers/database-utils'
 import { logger } from '../../helpers/logger'
-import { doRequest } from '../../helpers/requests'
+import { doRequest, doRequestAndSaveToFile } from '../../helpers/requests'
 import {
   ACTIVITY_PUB,
   MIMETYPES,
@@ -108,13 +108,11 @@ async function fetchRemoteVideoDescription (video: VideoModel) {
   return body.description ? body.description : ''
 }
 
-function fetchRemoteVideoStaticFile (video: VideoModel, path: string, reject: Function) {
+function fetchRemoteVideoStaticFile (video: VideoModel, path: string, destPath: string) {
   const url = buildRemoteBaseUrl(video, path)
 
   // We need to provide a callback, if no we could have an uncaught exception
-  return request.get(url, err => {
-    if (err) reject(err)
-  })
+  return doRequestAndSaveToFile({ uri: url }, destPath)
 }
 
 function buildRemoteBaseUrl (video: VideoModel, path: string) {
index 84ed74c988d33fd41927e154ca05674bf35b5eb0..1908cfb06f12959c117ab84327aeb53a125bee34 100644 (file)
@@ -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 <T> {
       }
     })
   }
-
-  protected saveRemoteVideoFileAndReturnPath (video: VideoModel, remoteStaticPath: string, destPath: string) {
-    return new Promise<string>((res, rej) => {
-      const req = fetchRemoteVideoStaticFile(video, remoteStaticPath, rej)
-
-      const stream = createWriteStream(destPath)
-
-      req.pipe(stream)
-         .on('error', (err) => rej(err))
-         .on('finish', () => res(destPath))
-    })
-  }
 }
index 305e39c35053d33d4dc0479f5c95992d61a55362..440c3fde81c2de19c4c763ad0d217e659e4ca4f0 100644 (file)
@@ -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 <GetPathParam> {
     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 }
   }
 }
 
index c117ae426a9df633a58c2bcc1f048811ec43f47e..14be7f24a57f75e1cfa63deb6548519e31806b4e 100644 (file)
@@ -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 <string> {
 
@@ -35,9 +36,9 @@ class VideosPreviewCache extends AbstractVideoStaticFileCache <string> {
     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 }
   }
 }