]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/files-cache/videos-preview-cache.ts
Merge branch 'release/v1.3.0' into develop
[github/Chocobozzz/PeerTube.git] / server / lib / files-cache / videos-preview-cache.ts
index 192f99aec6997c2c66b213cea147f06c6d466cb8..a68619d076da9372bd6c6b00ad056d6da6e1b626 100644 (file)
@@ -1,8 +1,9 @@
 import { join } from 'path'
-import { FILES_CACHE, STATIC_PATHS } from '../../initializers'
+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> {
 
@@ -16,13 +17,13 @@ class VideosPreviewCache extends AbstractVideoStaticFileCache <string> {
     return this.instance || (this.instance = new this())
   }
 
-  async getFilePath (videoUUID: string) {
+  async getFilePathImpl (videoUUID: string) {
     const video = await VideoModel.loadByUUIDWithFile(videoUUID)
     if (!video) return undefined
 
-    if (video.isOwned()) return join(CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName())
+    if (video.isOwned()) return { isOwned: true, path: video.getPreview().getPath() }
 
-    return this.loadFromLRU(videoUUID)
+    return this.loadRemoteFile(videoUUID)
   }
 
   protected async loadRemoteFile (key: string) {
@@ -31,10 +32,13 @@ class VideosPreviewCache extends AbstractVideoStaticFileCache <string> {
 
     if (video.isOwned()) throw new Error('Cannot load remote preview of owned video.')
 
-    const remoteStaticPath = join(STATIC_PATHS.PREVIEWS, video.getPreviewName())
-    const destPath = join(FILES_CACHE.PREVIEWS.DIRECTORY, video.getPreviewName())
+    // FIXME: use URL
+    const remoteStaticPath = join(STATIC_PATHS.PREVIEWS, video.getPreview().filename)
+    const destPath = join(FILES_CACHE.PREVIEWS.DIRECTORY, video.getPreview().filename)
 
-    return this.saveRemoteVideoFileAndReturnPath(video, remoteStaticPath, destPath)
+    await fetchRemoteVideoStaticFile(video, remoteStaticPath, destPath)
+
+    return { isOwned: false, path: destPath }
   }
 }