aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/object-storage-proxy.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/controllers/object-storage-proxy.ts')
-rw-r--r--server/controllers/object-storage-proxy.ts78
1 files changed, 78 insertions, 0 deletions
diff --git a/server/controllers/object-storage-proxy.ts b/server/controllers/object-storage-proxy.ts
new file mode 100644
index 000000000..6fedcfd8f
--- /dev/null
+++ b/server/controllers/object-storage-proxy.ts
@@ -0,0 +1,78 @@
1import cors from 'cors'
2import express from 'express'
3import { OBJECT_STORAGE_PROXY_PATHS } from '@server/initializers/constants'
4import { getHLSFileReadStream, getWebTorrentFileReadStream } from '@server/lib/object-storage'
5import {
6 asyncMiddleware,
7 ensureCanAccessPrivateVideoHLSFiles,
8 ensureCanAccessVideoPrivateWebTorrentFiles,
9 optionalAuthenticate
10} from '@server/middlewares'
11import { HttpStatusCode } from '@shared/models'
12
13const objectStorageProxyRouter = express.Router()
14
15objectStorageProxyRouter.use(cors())
16
17objectStorageProxyRouter.get(OBJECT_STORAGE_PROXY_PATHS.PRIVATE_WEBSEED + ':filename',
18 optionalAuthenticate,
19 asyncMiddleware(ensureCanAccessVideoPrivateWebTorrentFiles),
20 asyncMiddleware(proxifyWebTorrent)
21)
22
23objectStorageProxyRouter.get(OBJECT_STORAGE_PROXY_PATHS.STREAMING_PLAYLISTS.PRIVATE_HLS + ':videoUUID/:filename',
24 optionalAuthenticate,
25 asyncMiddleware(ensureCanAccessPrivateVideoHLSFiles),
26 asyncMiddleware(proxifyHLS)
27)
28
29// ---------------------------------------------------------------------------
30
31export {
32 objectStorageProxyRouter
33}
34
35async function proxifyWebTorrent (req: express.Request, res: express.Response) {
36 const filename = req.params.filename
37
38 try {
39 const stream = await getWebTorrentFileReadStream({
40 filename,
41 rangeHeader: req.header('range')
42 })
43
44 return stream.pipe(res)
45 } catch (err) {
46 return handleObjectStorageFailure(res, err)
47 }
48}
49
50async function proxifyHLS (req: express.Request, res: express.Response) {
51 const playlist = res.locals.videoStreamingPlaylist
52 const video = res.locals.onlyVideo
53 const filename = req.params.filename
54
55 try {
56 const stream = await getHLSFileReadStream({
57 playlist: playlist.withVideo(video),
58 filename,
59 rangeHeader: req.header('range')
60 })
61
62 return stream.pipe(res)
63 } catch (err) {
64 return handleObjectStorageFailure(res, err)
65 }
66}
67
68function handleObjectStorageFailure (res: express.Response, err: Error) {
69 if (err.name === 'NoSuchKey') {
70 return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
71 }
72
73 return res.fail({
74 status: HttpStatusCode.INTERNAL_SERVER_ERROR_500,
75 message: err.message,
76 type: err.name
77 })
78}