]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/static.ts
Rename Pod -> Server
[github/Chocobozzz/PeerTube.git] / server / controllers / static.ts
CommitLineData
4d4e5cd4
C
1import * as express from 'express'
2import * as cors from 'cors'
65fcc311
C
3
4import {
5 CONFIG,
6 STATIC_MAX_AGE,
7 STATIC_PATHS
8} from '../initializers'
f981dae8 9import { VideosPreviewCache } from '../lib'
eb080476 10import { asyncMiddleware } from '../middlewares'
65fcc311
C
11
12const staticRouter = express.Router()
13
14/*
60862425 15 Cors is very important to let other servers access torrent and video files
65fcc311
C
16*/
17
18const torrentsPhysicalPath = CONFIG.STORAGE.TORRENTS_DIR
19staticRouter.use(
20 STATIC_PATHS.TORRENTS,
21 cors(),
49379960 22 express.static(torrentsPhysicalPath, { maxAge: 0 }) // Don't cache because we could regenerate the torrent file
65fcc311
C
23)
24
25// Videos path for webseeding
26const videosPhysicalPath = CONFIG.STORAGE.VIDEOS_DIR
27staticRouter.use(
28 STATIC_PATHS.WEBSEED,
29 cors(),
30 express.static(videosPhysicalPath, { maxAge: STATIC_MAX_AGE })
31)
32
33// Thumbnails path for express
34const thumbnailsPhysicalPath = CONFIG.STORAGE.THUMBNAILS_DIR
35staticRouter.use(
36 STATIC_PATHS.THUMBNAILS,
37 express.static(thumbnailsPhysicalPath, { maxAge: STATIC_MAX_AGE })
38)
39
40// Video previews path for express
65fcc311 41staticRouter.use(
f981dae8 42 STATIC_PATHS.PREVIEWS + ':uuid.jpg',
eb080476 43 asyncMiddleware(getPreview)
65fcc311
C
44)
45
46// ---------------------------------------------------------------------------
47
48export {
49 staticRouter
50}
f981dae8
C
51
52// ---------------------------------------------------------------------------
53
eb080476
C
54async 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)
f981dae8 57
eb080476 58 return res.sendFile(path, { maxAge: STATIC_MAX_AGE })
f981dae8 59}