]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/static.ts
Change video spinner
[github/Chocobozzz/PeerTube.git] / server / controllers / static.ts
index 51f75c57ed862d8afdebc350fd46ad4c0ea05f84..c7c952d6fcc86b8470debbf02ba02b6af4ff0c4d 100644 (file)
@@ -1,11 +1,13 @@
-import express = require('express')
-import cors = require('cors')
+import * as express from 'express'
+import * as cors from 'cors'
 
 import {
   CONFIG,
   STATIC_MAX_AGE,
   STATIC_PATHS
 } from '../initializers'
+import { VideosPreviewCache } from '../lib'
+import { asyncMiddleware } from '../middlewares'
 
 const staticRouter = express.Router()
 
@@ -17,7 +19,7 @@ const torrentsPhysicalPath = CONFIG.STORAGE.TORRENTS_DIR
 staticRouter.use(
   STATIC_PATHS.TORRENTS,
   cors(),
-  express.static(torrentsPhysicalPath, { maxAge: STATIC_MAX_AGE })
+  express.static(torrentsPhysicalPath, { maxAge: 0 }) // Don't cache because we could regenerate the torrent file
 )
 
 // Videos path for webseeding
@@ -36,10 +38,9 @@ staticRouter.use(
 )
 
 // Video previews path for express
-const previewsPhysicalPath = CONFIG.STORAGE.PREVIEWS_DIR
 staticRouter.use(
-  STATIC_PATHS.PREVIEWS,
-  express.static(previewsPhysicalPath, { maxAge: STATIC_MAX_AGE })
+  STATIC_PATHS.PREVIEWS + ':uuid.jpg',
+  asyncMiddleware(getPreview)
 )
 
 // ---------------------------------------------------------------------------
@@ -47,3 +48,12 @@ staticRouter.use(
 export {
   staticRouter
 }
+
+// ---------------------------------------------------------------------------
+
+async function getPreview (req: express.Request, res: express.Response, next: express.NextFunction) {
+  const path = await VideosPreviewCache.Instance.getPreviewPath(req.params.uuid)
+  if (!path) return res.sendStatus(404)
+
+  return res.sendFile(path, { maxAge: STATIC_MAX_AGE })
+}