]>
Commit | Line | Data |
---|---|---|
4d4e5cd4 | 1 | import * as cors from 'cors' |
50d6de9c C |
2 | import * as express from 'express' |
3 | import { CONFIG, STATIC_MAX_AGE, STATIC_PATHS } from '../initializers' | |
4 | import { VideosPreviewCache } from '../lib/cache' | |
eb080476 | 5 | import { asyncMiddleware } from '../middlewares' |
65fcc311 C |
6 | |
7 | const staticRouter = express.Router() | |
8 | ||
9 | /* | |
60862425 | 10 | Cors is very important to let other servers access torrent and video files |
65fcc311 C |
11 | */ |
12 | ||
13 | const torrentsPhysicalPath = CONFIG.STORAGE.TORRENTS_DIR | |
14 | staticRouter.use( | |
15 | STATIC_PATHS.TORRENTS, | |
16 | cors(), | |
49379960 | 17 | express.static(torrentsPhysicalPath, { maxAge: 0 }) // Don't cache because we could regenerate the torrent file |
65fcc311 C |
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 | // Video previews path for express | |
65fcc311 | 36 | staticRouter.use( |
f981dae8 | 37 | STATIC_PATHS.PREVIEWS + ':uuid.jpg', |
eb080476 | 38 | asyncMiddleware(getPreview) |
65fcc311 C |
39 | ) |
40 | ||
41 | // --------------------------------------------------------------------------- | |
42 | ||
43 | export { | |
44 | staticRouter | |
45 | } | |
f981dae8 C |
46 | |
47 | // --------------------------------------------------------------------------- | |
48 | ||
eb080476 C |
49 | async function getPreview (req: express.Request, res: express.Response, next: express.NextFunction) { |
50 | const path = await VideosPreviewCache.Instance.getPreviewPath(req.params.uuid) | |
51 | if (!path) return res.sendStatus(404) | |
f981dae8 | 52 | |
eb080476 | 53 | return res.sendFile(path, { maxAge: STATIC_MAX_AGE }) |
f981dae8 | 54 | } |