aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/object-storage-proxy.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2022-12-02 14:47:21 +0100
committerChocobozzz <me@florianbigard.com>2022-12-02 15:25:20 +0100
commit71e3e879c0616882ee82a0e44f8c2e5ee9698a3e (patch)
tree14452d26d240eb6d44178b76fc2dabda4cfc9428 /server/controllers/object-storage-proxy.ts
parent04509c43254dc232c61681ac4bb98e09fd126115 (diff)
downloadPeerTube-71e3e879c0616882ee82a0e44f8c2e5ee9698a3e.tar.gz
PeerTube-71e3e879c0616882ee82a0e44f8c2e5ee9698a3e.tar.zst
PeerTube-71e3e879c0616882ee82a0e44f8c2e5ee9698a3e.zip
Support reinjecting token in private m3u8 playlist
Diffstat (limited to 'server/controllers/object-storage-proxy.ts')
-rw-r--r--server/controllers/object-storage-proxy.ts20
1 files changed, 19 insertions, 1 deletions
diff --git a/server/controllers/object-storage-proxy.ts b/server/controllers/object-storage-proxy.ts
index 3ce279671..aa853a383 100644
--- a/server/controllers/object-storage-proxy.ts
+++ b/server/controllers/object-storage-proxy.ts
@@ -1,7 +1,10 @@
1import cors from 'cors' 1import cors from 'cors'
2import express from 'express' 2import express from 'express'
3import { PassThrough, pipeline } from 'stream'
3import { logger } from '@server/helpers/logger' 4import { logger } from '@server/helpers/logger'
5import { StreamReplacer } from '@server/helpers/stream-replacer'
4import { OBJECT_STORAGE_PROXY_PATHS } from '@server/initializers/constants' 6import { OBJECT_STORAGE_PROXY_PATHS } from '@server/initializers/constants'
7import { injectQueryToPlaylistUrls } from '@server/lib/hls'
5import { getHLSFileReadStream, getWebTorrentFileReadStream } from '@server/lib/object-storage' 8import { getHLSFileReadStream, getWebTorrentFileReadStream } from '@server/lib/object-storage'
6import { 9import {
7 asyncMiddleware, 10 asyncMiddleware,
@@ -11,6 +14,7 @@ import {
11 optionalAuthenticate 14 optionalAuthenticate
12} from '@server/middlewares' 15} from '@server/middlewares'
13import { HttpStatusCode } from '@shared/models' 16import { HttpStatusCode } from '@shared/models'
17import { buildReinjectVideoFileTokenQuery, doReinjectVideoFileToken } from './shared/m3u8-playlist'
14 18
15const objectStorageProxyRouter = express.Router() 19const objectStorageProxyRouter = express.Router()
16 20
@@ -67,7 +71,20 @@ async function proxifyHLS (req: express.Request, res: express.Response) {
67 rangeHeader: req.header('range') 71 rangeHeader: req.header('range')
68 }) 72 })
69 73
70 return stream.pipe(res) 74 const streamReplacer = filename.endsWith('.m3u8') && doReinjectVideoFileToken(req)
75 ? new StreamReplacer(line => injectQueryToPlaylistUrls(line, buildReinjectVideoFileTokenQuery(req)))
76 : new PassThrough()
77
78 return pipeline(
79 stream,
80 streamReplacer,
81 res,
82 err => {
83 if (!err) return
84
85 handleObjectStorageFailure(res, err)
86 }
87 )
71 } catch (err) { 88 } catch (err) {
72 return handleObjectStorageFailure(res, err) 89 return handleObjectStorageFailure(res, err)
73 } 90 }
@@ -75,6 +92,7 @@ async function proxifyHLS (req: express.Request, res: express.Response) {
75 92
76function handleObjectStorageFailure (res: express.Response, err: Error) { 93function handleObjectStorageFailure (res: express.Response, err: Error) {
77 if (err.name === 'NoSuchKey') { 94 if (err.name === 'NoSuchKey') {
95 logger.debug('Could not find key in object storage to proxify private HLS video file.', { err })
78 return res.sendStatus(HttpStatusCode.NOT_FOUND_404) 96 return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
79 } 97 }
80 98