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