diff options
Diffstat (limited to 'server/controllers/static.ts')
-rw-r--r-- | server/controllers/static.ts | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/server/controllers/static.ts b/server/controllers/static.ts index 6ef9154b9..52e48267f 100644 --- a/server/controllers/static.ts +++ b/server/controllers/static.ts | |||
@@ -1,5 +1,8 @@ | |||
1 | import cors from 'cors' | 1 | import cors from 'cors' |
2 | import express from 'express' | 2 | import express from 'express' |
3 | import { readFile } from 'fs-extra' | ||
4 | import { join } from 'path' | ||
5 | import { injectQueryToPlaylistUrls } from '@server/lib/hls' | ||
3 | import { | 6 | import { |
4 | asyncMiddleware, | 7 | asyncMiddleware, |
5 | ensureCanAccessPrivateVideoHLSFiles, | 8 | ensureCanAccessPrivateVideoHLSFiles, |
@@ -7,8 +10,10 @@ import { | |||
7 | handleStaticError, | 10 | handleStaticError, |
8 | optionalAuthenticate | 11 | optionalAuthenticate |
9 | } from '@server/middlewares' | 12 | } from '@server/middlewares' |
13 | import { HttpStatusCode } from '@shared/models' | ||
10 | import { CONFIG } from '../initializers/config' | 14 | import { CONFIG } from '../initializers/config' |
11 | import { DIRECTORIES, STATIC_MAX_AGE, STATIC_PATHS } from '../initializers/constants' | 15 | import { DIRECTORIES, STATIC_MAX_AGE, STATIC_PATHS } from '../initializers/constants' |
16 | import { buildReinjectVideoFileTokenQuery, doReinjectVideoFileToken } from './shared/m3u8-playlist' | ||
12 | 17 | ||
13 | const staticRouter = express.Router() | 18 | const staticRouter = express.Router() |
14 | 19 | ||
@@ -50,6 +55,12 @@ const privateHLSStaticMiddlewares = CONFIG.STATIC_FILES.PRIVATE_FILES_REQUIRE_AU | |||
50 | : [] | 55 | : [] |
51 | 56 | ||
52 | staticRouter.use( | 57 | staticRouter.use( |
58 | STATIC_PATHS.STREAMING_PLAYLISTS.PRIVATE_HLS + ':videoUUID/:playlistName.m3u8', | ||
59 | ...privateHLSStaticMiddlewares, | ||
60 | asyncMiddleware(servePrivateM3U8) | ||
61 | ) | ||
62 | |||
63 | staticRouter.use( | ||
53 | STATIC_PATHS.STREAMING_PLAYLISTS.PRIVATE_HLS, | 64 | STATIC_PATHS.STREAMING_PLAYLISTS.PRIVATE_HLS, |
54 | ...privateHLSStaticMiddlewares, | 65 | ...privateHLSStaticMiddlewares, |
55 | express.static(DIRECTORIES.HLS_STREAMING_PLAYLIST.PRIVATE, { fallthrough: false }), | 66 | express.static(DIRECTORIES.HLS_STREAMING_PLAYLIST.PRIVATE, { fallthrough: false }), |
@@ -74,3 +85,31 @@ staticRouter.use( | |||
74 | export { | 85 | export { |
75 | staticRouter | 86 | staticRouter |
76 | } | 87 | } |
88 | |||
89 | // --------------------------------------------------------------------------- | ||
90 | |||
91 | async function servePrivateM3U8 (req: express.Request, res: express.Response) { | ||
92 | const path = join(DIRECTORIES.HLS_STREAMING_PLAYLIST.PRIVATE, req.params.videoUUID, req.params.playlistName + '.m3u8') | ||
93 | |||
94 | let playlistContent: string | ||
95 | |||
96 | try { | ||
97 | playlistContent = await readFile(path, 'utf-8') | ||
98 | } catch (err) { | ||
99 | if (err.message.includes('ENOENT')) { | ||
100 | return res.fail({ | ||
101 | status: HttpStatusCode.NOT_FOUND_404, | ||
102 | message: 'File not found' | ||
103 | }) | ||
104 | } | ||
105 | |||
106 | throw err | ||
107 | } | ||
108 | |||
109 | // Inject token in playlist so players that cannot alter the HTTP request can still watch the video | ||
110 | const transformedContent = doReinjectVideoFileToken(req) | ||
111 | ? injectQueryToPlaylistUrls(playlistContent, buildReinjectVideoFileTokenQuery(req)) | ||
112 | : playlistContent | ||
113 | |||
114 | return res.set('content-type', 'application/vnd.apple.mpegurl').send(transformedContent).end() | ||
115 | } | ||