]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/cache/videos-preview-cache.ts
Design modals
[github/Chocobozzz/PeerTube.git] / server / lib / cache / videos-preview-cache.ts
index 7f352f361df8e84905555eb22636212d216551b4..0eb43efcc03dc10c7aab65a929b1e64bfa2f1734 100644 (file)
@@ -1,11 +1,10 @@
 import * as asyncLRU from 'async-lru'
-import { join } from 'path'
 import { createWriteStream } from 'fs'
-
-import { database as db, CONFIG, CACHE } from '../../initializers'
+import { join } from 'path'
 import { logger, unlinkPromise } from '../../helpers'
-import { VideoInstance } from '../../models'
-import { fetchRemoteVideoPreview } from '../activitypub/videos'
+import { CACHE, CONFIG } from '../../initializers'
+import { VideoModel } from '../../models/video/video'
+import { fetchRemoteVideoPreview } from '../activitypub'
 
 class VideosPreviewCache {
 
@@ -34,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)
@@ -43,17 +47,17 @@ class VideosPreviewCache {
   }
 
   private async loadPreviews (key: string) {
-    const video = await db.Video.loadByUUIDAndPopulateAccountAndServerAndTags(key)
+    const video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(key)
     if (!video) return undefined
 
-    if (video.isOwned()) return join(CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName())
+    if (video.isOwned()) throw new Error('Cannot load preview of owned video.')
 
     const res = await this.saveRemotePreviewAndReturnPath(video)
 
     return res
   }
 
-  private saveRemotePreviewAndReturnPath (video: VideoInstance) {
+  private saveRemotePreviewAndReturnPath (video: VideoModel) {
     const req = fetchRemoteVideoPreview(video)
 
     return new Promise<string>((res, rej) => {