]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/static.ts
Use async/await in controllers
[github/Chocobozzz/PeerTube.git] / server / controllers / static.ts
1 import * as express from 'express'
2 import * as cors from 'cors'
3
4 import {
5 CONFIG,
6 STATIC_MAX_AGE,
7 STATIC_PATHS
8 } from '../initializers'
9 import { VideosPreviewCache } from '../lib'
10 import { asyncMiddleware } from '../middlewares'
11
12 const staticRouter = express.Router()
13
14 /*
15 Cors is very important to let other pods access torrent and video files
16 */
17
18 const torrentsPhysicalPath = CONFIG.STORAGE.TORRENTS_DIR
19 staticRouter.use(
20 STATIC_PATHS.TORRENTS,
21 cors(),
22 express.static(torrentsPhysicalPath, { maxAge: 0 }) // Don't cache because we could regenerate the torrent file
23 )
24
25 // Videos path for webseeding
26 const videosPhysicalPath = CONFIG.STORAGE.VIDEOS_DIR
27 staticRouter.use(
28 STATIC_PATHS.WEBSEED,
29 cors(),
30 express.static(videosPhysicalPath, { maxAge: STATIC_MAX_AGE })
31 )
32
33 // Thumbnails path for express
34 const thumbnailsPhysicalPath = CONFIG.STORAGE.THUMBNAILS_DIR
35 staticRouter.use(
36 STATIC_PATHS.THUMBNAILS,
37 express.static(thumbnailsPhysicalPath, { maxAge: STATIC_MAX_AGE })
38 )
39
40 // Video previews path for express
41 staticRouter.use(
42 STATIC_PATHS.PREVIEWS + ':uuid.jpg',
43 asyncMiddleware(getPreview)
44 )
45
46 // ---------------------------------------------------------------------------
47
48 export {
49 staticRouter
50 }
51
52 // ---------------------------------------------------------------------------
53
54 async function getPreview (req: express.Request, res: express.Response, next: express.NextFunction) {
55 const path = await VideosPreviewCache.Instance.getPreviewPath(req.params.uuid)
56 if (!path) return res.sendStatus(404)
57
58 return res.sendFile(path, { maxAge: STATIC_MAX_AGE })
59 }