mirror of https://github.com/Chocobozzz/PeerTube
Forward 206 status code for object storage proxy
parent
28a398d6d7
commit
57e11a20f1
|
@ -15,6 +15,7 @@ import {
|
||||||
} from '@server/middlewares'
|
} from '@server/middlewares'
|
||||||
import { HttpStatusCode } from '@shared/models'
|
import { HttpStatusCode } from '@shared/models'
|
||||||
import { buildReinjectVideoFileTokenQuery, doReinjectVideoFileToken } from './shared/m3u8-playlist'
|
import { buildReinjectVideoFileTokenQuery, doReinjectVideoFileToken } from './shared/m3u8-playlist'
|
||||||
|
import { GetObjectCommandOutput } from '@aws-sdk/client-s3'
|
||||||
|
|
||||||
const objectStorageProxyRouter = express.Router()
|
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)
|
logger.debug('Proxifying WebTorrent file %s from object storage.', filename)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const stream = await getWebTorrentFileReadStream({
|
const { response: s3Response, stream } = await getWebTorrentFileReadStream({
|
||||||
filename,
|
filename,
|
||||||
rangeHeader: req.header('range')
|
rangeHeader: req.header('range')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
setS3Headers(res, s3Response)
|
||||||
|
|
||||||
return stream.pipe(res)
|
return stream.pipe(res)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return handleObjectStorageFailure(res, 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)
|
logger.debug('Proxifying HLS file %s from object storage.', filename)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const stream = await getHLSFileReadStream({
|
const { response: s3Response, stream } = await getHLSFileReadStream({
|
||||||
playlist: playlist.withVideo(video),
|
playlist: playlist.withVideo(video),
|
||||||
filename,
|
filename,
|
||||||
rangeHeader: req.header('range')
|
rangeHeader: req.header('range')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
setS3Headers(res, s3Response)
|
||||||
|
|
||||||
const streamReplacer = filename.endsWith('.m3u8') && doReinjectVideoFileToken(req)
|
const streamReplacer = filename.endsWith('.m3u8') && doReinjectVideoFileToken(req)
|
||||||
? new StreamReplacer(line => injectQueryToPlaylistUrls(line, buildReinjectVideoFileTokenQuery(req)))
|
? new StreamReplacer(line => injectQueryToPlaylistUrls(line, buildReinjectVideoFileTokenQuery(req)))
|
||||||
: new PassThrough()
|
: new PassThrough()
|
||||||
|
@ -102,3 +107,9 @@ function handleObjectStorageFailure (res: express.Response, err: Error) {
|
||||||
type: err.name
|
type: err.name
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function setS3Headers (res: express.Response, s3Response: GetObjectCommandOutput) {
|
||||||
|
if (s3Response.$metadata.httpStatusCode === HttpStatusCode.PARTIAL_CONTENT_206) {
|
||||||
|
res.status(HttpStatusCode.PARTIAL_CONTENT_206)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -187,7 +187,10 @@ async function createObjectReadStream (options: {
|
||||||
|
|
||||||
const response = await getClient().send(command)
|
const response = await getClient().send(command)
|
||||||
|
|
||||||
return response.Body as Readable
|
return {
|
||||||
|
response,
|
||||||
|
stream: response.Body as Readable
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
Loading…
Reference in New Issue