2022-10-19 10:43:53 +02:00
|
|
|
import cors from 'cors'
|
|
|
|
import express from 'express'
|
2022-12-02 14:47:21 +01:00
|
|
|
import { PassThrough, pipeline } from 'stream'
|
2022-10-26 16:23:39 +02:00
|
|
|
import { logger } from '@server/helpers/logger'
|
2022-12-02 14:47:21 +01:00
|
|
|
import { StreamReplacer } from '@server/helpers/stream-replacer'
|
2022-10-19 10:43:53 +02:00
|
|
|
import { OBJECT_STORAGE_PROXY_PATHS } from '@server/initializers/constants'
|
2022-12-02 14:47:21 +01:00
|
|
|
import { injectQueryToPlaylistUrls } from '@server/lib/hls'
|
2022-10-19 10:43:53 +02:00
|
|
|
import { getHLSFileReadStream, getWebTorrentFileReadStream } from '@server/lib/object-storage'
|
|
|
|
import {
|
|
|
|
asyncMiddleware,
|
|
|
|
ensureCanAccessPrivateVideoHLSFiles,
|
|
|
|
ensureCanAccessVideoPrivateWebTorrentFiles,
|
2022-10-26 16:23:39 +02:00
|
|
|
ensurePrivateObjectStorageProxyIsEnabled,
|
2022-10-19 10:43:53 +02:00
|
|
|
optionalAuthenticate
|
|
|
|
} from '@server/middlewares'
|
|
|
|
import { HttpStatusCode } from '@shared/models'
|
2022-12-02 14:47:21 +01:00
|
|
|
import { buildReinjectVideoFileTokenQuery, doReinjectVideoFileToken } from './shared/m3u8-playlist'
|
2022-12-19 16:06:58 +01:00
|
|
|
import { GetObjectCommandOutput } from '@aws-sdk/client-s3'
|
2022-10-19 10:43:53 +02:00
|
|
|
|
|
|
|
const objectStorageProxyRouter = express.Router()
|
|
|
|
|
|
|
|
objectStorageProxyRouter.use(cors())
|
|
|
|
|
|
|
|
objectStorageProxyRouter.get(OBJECT_STORAGE_PROXY_PATHS.PRIVATE_WEBSEED + ':filename',
|
2022-10-26 16:23:39 +02:00
|
|
|
ensurePrivateObjectStorageProxyIsEnabled,
|
2022-10-19 10:43:53 +02:00
|
|
|
optionalAuthenticate,
|
|
|
|
asyncMiddleware(ensureCanAccessVideoPrivateWebTorrentFiles),
|
|
|
|
asyncMiddleware(proxifyWebTorrent)
|
|
|
|
)
|
|
|
|
|
|
|
|
objectStorageProxyRouter.get(OBJECT_STORAGE_PROXY_PATHS.STREAMING_PLAYLISTS.PRIVATE_HLS + ':videoUUID/:filename',
|
2022-10-26 16:23:39 +02:00
|
|
|
ensurePrivateObjectStorageProxyIsEnabled,
|
2022-10-19 10:43:53 +02:00
|
|
|
optionalAuthenticate,
|
|
|
|
asyncMiddleware(ensureCanAccessPrivateVideoHLSFiles),
|
|
|
|
asyncMiddleware(proxifyHLS)
|
|
|
|
)
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
objectStorageProxyRouter
|
|
|
|
}
|
|
|
|
|
|
|
|
async function proxifyWebTorrent (req: express.Request, res: express.Response) {
|
|
|
|
const filename = req.params.filename
|
|
|
|
|
2022-10-26 16:23:39 +02:00
|
|
|
logger.debug('Proxifying WebTorrent file %s from object storage.', filename)
|
|
|
|
|
2022-10-19 10:43:53 +02:00
|
|
|
try {
|
2022-12-19 16:06:58 +01:00
|
|
|
const { response: s3Response, stream } = await getWebTorrentFileReadStream({
|
2022-10-19 10:43:53 +02:00
|
|
|
filename,
|
|
|
|
rangeHeader: req.header('range')
|
|
|
|
})
|
|
|
|
|
2022-12-19 16:06:58 +01:00
|
|
|
setS3Headers(res, s3Response)
|
|
|
|
|
2022-10-19 10:43:53 +02:00
|
|
|
return stream.pipe(res)
|
|
|
|
} catch (err) {
|
|
|
|
return handleObjectStorageFailure(res, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function proxifyHLS (req: express.Request, res: express.Response) {
|
|
|
|
const playlist = res.locals.videoStreamingPlaylist
|
|
|
|
const video = res.locals.onlyVideo
|
|
|
|
const filename = req.params.filename
|
|
|
|
|
2022-10-26 16:23:39 +02:00
|
|
|
logger.debug('Proxifying HLS file %s from object storage.', filename)
|
|
|
|
|
2022-10-19 10:43:53 +02:00
|
|
|
try {
|
2022-12-19 16:06:58 +01:00
|
|
|
const { response: s3Response, stream } = await getHLSFileReadStream({
|
2022-10-19 10:43:53 +02:00
|
|
|
playlist: playlist.withVideo(video),
|
|
|
|
filename,
|
|
|
|
rangeHeader: req.header('range')
|
|
|
|
})
|
|
|
|
|
2022-12-19 16:06:58 +01:00
|
|
|
setS3Headers(res, s3Response)
|
|
|
|
|
2022-12-02 14:47:21 +01:00
|
|
|
const streamReplacer = filename.endsWith('.m3u8') && doReinjectVideoFileToken(req)
|
|
|
|
? new StreamReplacer(line => injectQueryToPlaylistUrls(line, buildReinjectVideoFileTokenQuery(req)))
|
|
|
|
: new PassThrough()
|
|
|
|
|
|
|
|
return pipeline(
|
|
|
|
stream,
|
|
|
|
streamReplacer,
|
|
|
|
res,
|
|
|
|
err => {
|
|
|
|
if (!err) return
|
|
|
|
|
|
|
|
handleObjectStorageFailure(res, err)
|
|
|
|
}
|
|
|
|
)
|
2022-10-19 10:43:53 +02:00
|
|
|
} catch (err) {
|
|
|
|
return handleObjectStorageFailure(res, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleObjectStorageFailure (res: express.Response, err: Error) {
|
|
|
|
if (err.name === 'NoSuchKey') {
|
2022-12-02 14:47:21 +01:00
|
|
|
logger.debug('Could not find key in object storage to proxify private HLS video file.', { err })
|
2022-10-19 10:43:53 +02:00
|
|
|
return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.fail({
|
|
|
|
status: HttpStatusCode.INTERNAL_SERVER_ERROR_500,
|
|
|
|
message: err.message,
|
|
|
|
type: err.name
|
|
|
|
})
|
|
|
|
}
|
2022-12-19 16:06:58 +01:00
|
|
|
|
|
|
|
function setS3Headers (res: express.Response, s3Response: GetObjectCommandOutput) {
|
|
|
|
if (s3Response.$metadata.httpStatusCode === HttpStatusCode.PARTIAL_CONTENT_206) {
|
|
|
|
res.status(HttpStatusCode.PARTIAL_CONTENT_206)
|
|
|
|
}
|
|
|
|
}
|