]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/cache/videos-preview-cache.ts
Handle .srt subtitles
[github/Chocobozzz/PeerTube.git] / server / lib / cache / videos-preview-cache.ts
index 791ad1cbf77f0735af2fa0e3ef35f7ae26900e07..22b6d9cb0db82ecafd74747062ca0db8879fbe36 100644 (file)
@@ -1,69 +1,39 @@
-import * as asyncLRU from 'async-lru'
 import { join } from 'path'
-import { createWriteStream } from 'fs'
+import { CACHE, CONFIG, STATIC_PATHS } from '../../initializers'
+import { VideoModel } from '../../models/video/video'
+import { AbstractVideoStaticFileCache } from './abstract-video-static-file-cache'
 
-import { database as db, CONFIG, CACHE } from '../../initializers'
-import { logger, unlinkPromise } from '../../helpers'
-import { VideoInstance } from '../../models'
-import { fetchRemotePreview } from '../../lib'
-
-class VideosPreviewCache {
+class VideosPreviewCache extends AbstractVideoStaticFileCache <string> {
 
   private static instance: VideosPreviewCache
 
-  private lru
-
-  private constructor () { }
+  private constructor () {
+    super()
+  }
 
   static get Instance () {
     return this.instance || (this.instance = new this())
   }
 
-  init (max: number) {
-    this.lru = new asyncLRU({
-      max,
-      load: (key, cb) => {
-        this.loadPreviews(key)
-          .then(res => cb(null, res))
-          .catch(err => cb(err))
-      }
-    })
-
-    this.lru.on('evict', (obj: { key: string, value: string }) => {
-      unlinkPromise(obj.value).then(() => logger.debug('%s evicted from VideosPreviewCache', obj.value))
-    })
-  }
-
-  getPreviewPath (key: string) {
-    return new Promise<string>((res, rej) => {
-      this.lru.get(key, (err, value) => {
-        err ? rej(err) : res(value)
-      })
-    })
-  }
-
-  private async loadPreviews (key: string) {
-    const video = await db.Video.loadByUUIDAndPopulateAccountAndPodAndTags(key)
+  async getFilePath (videoUUID: string) {
+    const video = await VideoModel.loadByUUID(videoUUID)
     if (!video) return undefined
 
     if (video.isOwned()) return join(CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName())
 
-    const res = await this.saveRemotePreviewAndReturnPath(video)
-
-    return res
+    return this.loadFromLRU(videoUUID)
   }
 
-  private saveRemotePreviewAndReturnPath (video: VideoInstance) {
-    const req = fetchRemotePreview(video)
+  protected async loadRemoteFile (key: string) {
+    const video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(key)
+    if (!video) return undefined
+
+    if (video.isOwned()) throw new Error('Cannot load remote preview of owned video.')
 
-    return new Promise<string>((res, rej) => {
-      const path = join(CACHE.DIRECTORIES.PREVIEWS, video.getPreviewName())
-      const stream = createWriteStream(path)
+    const remoteStaticPath = join(STATIC_PATHS.PREVIEWS, video.getPreviewName())
+    const destPath = join(CACHE.PREVIEWS.DIRECTORY, video.getPreviewName())
 
-      req.pipe(stream)
-         .on('finish', () => res(path))
-         .on('error', (err) => rej(err))
-    })
+    return this.saveRemoteVideoFileAndReturnPath(video, remoteStaticPath, destPath)
   }
 }