]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/cache/videos-preview-cache.ts
Add comments federation tests
[github/Chocobozzz/PeerTube.git] / server / lib / cache / videos-preview-cache.ts
index c5bda8dd894e699471bfc50765db882f0c388555..ea959076d88c7614b0e4e7dac4f981029bf1eea0 100644 (file)
@@ -33,7 +33,12 @@ class VideosPreviewCache {
     })
   }
 
-  getPreviewPath (key: string) {
+  async getPreviewPath (key: string) {
+    const video = await VideoModel.loadByUUID(key)
+    if (!video) return undefined
+
+    if (video.isOwned()) return join(CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName())
+
     return new Promise<string>((res, rej) => {
       this.lru.get(key, (err, value) => {
         err ? rej(err) : res(value)
@@ -45,23 +50,21 @@ class VideosPreviewCache {
     const video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(key)
     if (!video) return undefined
 
-    if (video.isOwned()) return join(CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName())
-
-    const res = await this.saveRemotePreviewAndReturnPath(video)
+    if (video.isOwned()) throw new Error('Cannot load preview of owned video.')
 
-    return res
+    return this.saveRemotePreviewAndReturnPath(video)
   }
 
   private saveRemotePreviewAndReturnPath (video: VideoModel) {
-    const req = fetchRemoteVideoPreview(video)
 
     return new Promise<string>((res, rej) => {
+      const req = fetchRemoteVideoPreview(video, rej)
       const path = join(CACHE.DIRECTORIES.PREVIEWS, video.getPreviewName())
       const stream = createWriteStream(path)
 
       req.pipe(stream)
-         .on('finish', () => res(path))
-         .on('error', (err) => rej(err))
+        .on('error', (err) => rej(err))
+        .on('finish', () => res(path))
     })
   }
 }