]>
Commit | Line | Data |
---|---|---|
f7ce623d | 1 | import { HttpStatusCode } from '@shared/models' |
41fb13c3 C |
2 | import cors from 'cors' |
3 | import express from 'express' | |
ea8107bf C |
4 | import { CONFIG } from '../initializers/config' |
5 | import { HLS_STREAMING_PLAYLIST_DIRECTORY, STATIC_MAX_AGE, STATIC_PATHS } from '../initializers/constants' | |
65fcc311 C |
6 | |
7 | const staticRouter = express.Router() | |
8 | ||
ea8107bf | 9 | // Cors is very important to let other servers access torrent and video files |
62945f06 C |
10 | staticRouter.use(cors()) |
11 | ||
90a8bd30 | 12 | // Videos path for webseed |
65fcc311 C |
13 | staticRouter.use( |
14 | STATIC_PATHS.WEBSEED, | |
f7ce623d C |
15 | express.static(CONFIG.STORAGE.VIDEOS_DIR, { fallthrough: false }), |
16 | handleStaticError | |
65fcc311 | 17 | ) |
6040f87d | 18 | staticRouter.use( |
b9fffa29 | 19 | STATIC_PATHS.REDUNDANCY, |
f7ce623d C |
20 | express.static(CONFIG.STORAGE.REDUNDANCY_DIR, { fallthrough: false }), |
21 | handleStaticError | |
6040f87d C |
22 | ) |
23 | ||
09209296 C |
24 | // HLS |
25 | staticRouter.use( | |
9c6ca37f | 26 | STATIC_PATHS.STREAMING_PLAYLISTS.HLS, |
f7ce623d C |
27 | express.static(HLS_STREAMING_PLAYLIST_DIRECTORY, { fallthrough: false }), |
28 | handleStaticError | |
09209296 C |
29 | ) |
30 | ||
65fcc311 C |
31 | // Thumbnails path for express |
32 | const thumbnailsPhysicalPath = CONFIG.STORAGE.THUMBNAILS_DIR | |
33 | staticRouter.use( | |
34 | STATIC_PATHS.THUMBNAILS, | |
f7ce623d C |
35 | express.static(thumbnailsPhysicalPath, { maxAge: STATIC_MAX_AGE.SERVER, fallthrough: false }), |
36 | handleStaticError | |
65fcc311 C |
37 | ) |
38 | ||
65fcc311 C |
39 | // --------------------------------------------------------------------------- |
40 | ||
41 | export { | |
42 | staticRouter | |
43 | } | |
f7ce623d C |
44 | |
45 | // --------------------------------------------------------------------------- | |
46 | ||
47 | function handleStaticError (err: any, req: express.Request, res: express.Response, next: express.NextFunction) { | |
48 | const message = err.message || '' | |
49 | ||
50 | if (message.includes('ENOENT')) { | |
51 | return res.fail({ | |
52 | status: err.status || HttpStatusCode.INTERNAL_SERVER_ERROR_500, | |
53 | message: err.message, | |
54 | type: err.name | |
55 | }) | |
56 | } | |
57 | ||
58 | return next(err) | |
59 | } |