From 57e11a20f12c69927e5739ecf541ed340934ff90 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 19 Dec 2022 16:06:58 +0100 Subject: [PATCH] Forward 206 status code for object storage proxy --- server/controllers/object-storage-proxy.ts | 15 +++++++++++++-- .../shared/object-storage-helpers.ts | 5 ++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/server/controllers/object-storage-proxy.ts b/server/controllers/object-storage-proxy.ts index aa853a383..32b8d21da 100644 --- a/server/controllers/object-storage-proxy.ts +++ b/server/controllers/object-storage-proxy.ts @@ -15,6 +15,7 @@ import { } from '@server/middlewares' import { HttpStatusCode } from '@shared/models' import { buildReinjectVideoFileTokenQuery, doReinjectVideoFileToken } from './shared/m3u8-playlist' +import { GetObjectCommandOutput } from '@aws-sdk/client-s3' const objectStorageProxyRouter = express.Router() @@ -46,11 +47,13 @@ async function proxifyWebTorrent (req: express.Request, res: express.Response) { logger.debug('Proxifying WebTorrent file %s from object storage.', filename) try { - const stream = await getWebTorrentFileReadStream({ + const { response: s3Response, stream } = await getWebTorrentFileReadStream({ filename, rangeHeader: req.header('range') }) + setS3Headers(res, s3Response) + return stream.pipe(res) } catch (err) { return handleObjectStorageFailure(res, err) @@ -65,12 +68,14 @@ async function proxifyHLS (req: express.Request, res: express.Response) { logger.debug('Proxifying HLS file %s from object storage.', filename) try { - const stream = await getHLSFileReadStream({ + const { response: s3Response, stream } = await getHLSFileReadStream({ playlist: playlist.withVideo(video), filename, rangeHeader: req.header('range') }) + setS3Headers(res, s3Response) + const streamReplacer = filename.endsWith('.m3u8') && doReinjectVideoFileToken(req) ? new StreamReplacer(line => injectQueryToPlaylistUrls(line, buildReinjectVideoFileTokenQuery(req))) : new PassThrough() @@ -102,3 +107,9 @@ function handleObjectStorageFailure (res: express.Response, err: Error) { type: err.name }) } + +function setS3Headers (res: express.Response, s3Response: GetObjectCommandOutput) { + if (s3Response.$metadata.httpStatusCode === HttpStatusCode.PARTIAL_CONTENT_206) { + res.status(HttpStatusCode.PARTIAL_CONTENT_206) + } +} diff --git a/server/lib/object-storage/shared/object-storage-helpers.ts b/server/lib/object-storage/shared/object-storage-helpers.ts index 3046d76bc..8dff08ab4 100644 --- a/server/lib/object-storage/shared/object-storage-helpers.ts +++ b/server/lib/object-storage/shared/object-storage-helpers.ts @@ -187,7 +187,10 @@ async function createObjectReadStream (options: { const response = await getClient().send(command) - return response.Body as Readable + return { + response, + stream: response.Body as Readable + } } // ---------------------------------------------------------------------------