]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/static.ts
0b5c12b7603cf540afa19d04de7509befbf40431
[github/Chocobozzz/PeerTube.git] / server / controllers / static.ts
1 import { HttpStatusCode } from '@shared/models'
2 import cors from 'cors'
3 import express from 'express'
4 import { CONFIG } from '../initializers/config'
5 import { HLS_STREAMING_PLAYLIST_DIRECTORY, STATIC_MAX_AGE, STATIC_PATHS } from '../initializers/constants'
6
7 const staticRouter = express.Router()
8
9 // Cors is very important to let other servers access torrent and video files
10 staticRouter.use(cors())
11
12 // Videos path for webseed
13 staticRouter.use(
14 STATIC_PATHS.WEBSEED,
15 express.static(CONFIG.STORAGE.VIDEOS_DIR, { fallthrough: false }),
16 handleStaticError
17 )
18 staticRouter.use(
19 STATIC_PATHS.REDUNDANCY,
20 express.static(CONFIG.STORAGE.REDUNDANCY_DIR, { fallthrough: false }),
21 handleStaticError
22 )
23
24 // HLS
25 staticRouter.use(
26 STATIC_PATHS.STREAMING_PLAYLISTS.HLS,
27 express.static(HLS_STREAMING_PLAYLIST_DIRECTORY, { fallthrough: false }),
28 handleStaticError
29 )
30
31 // Thumbnails path for express
32 const thumbnailsPhysicalPath = CONFIG.STORAGE.THUMBNAILS_DIR
33 staticRouter.use(
34 STATIC_PATHS.THUMBNAILS,
35 express.static(thumbnailsPhysicalPath, { maxAge: STATIC_MAX_AGE.SERVER, fallthrough: false }),
36 handleStaticError
37 )
38
39 // ---------------------------------------------------------------------------
40
41 export {
42 staticRouter
43 }
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 }