aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/object-storage-proxy.ts
blob: c530b57f83ed8b586855c3493fb72c90f771f255 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import cors from 'cors'
import express from 'express'
import { PassThrough, pipeline } from 'stream'
import { logger } from '@server/helpers/logger'
import { StreamReplacer } from '@server/helpers/stream-replacer'
import { OBJECT_STORAGE_PROXY_PATHS } from '@server/initializers/constants'
import { injectQueryToPlaylistUrls } from '@server/lib/hls'
import { getHLSFileReadStream, getWebTorrentFileReadStream } from '@server/lib/object-storage'
import {
  asyncMiddleware,
  ensureCanAccessPrivateVideoHLSFiles,
  ensureCanAccessVideoPrivateWebTorrentFiles,
  ensurePrivateObjectStorageProxyIsEnabled,
  optionalAuthenticate
} 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()

objectStorageProxyRouter.use(cors())

objectStorageProxyRouter.get(OBJECT_STORAGE_PROXY_PATHS.PRIVATE_WEBSEED + ':filename',
  ensurePrivateObjectStorageProxyIsEnabled,
  optionalAuthenticate,
  asyncMiddleware(ensureCanAccessVideoPrivateWebTorrentFiles),
  asyncMiddleware(proxifyWebTorrent)
)

objectStorageProxyRouter.get(OBJECT_STORAGE_PROXY_PATHS.STREAMING_PLAYLISTS.PRIVATE_HLS + ':videoUUID/:filename',
  ensurePrivateObjectStorageProxyIsEnabled,
  optionalAuthenticate,
  asyncMiddleware(ensureCanAccessPrivateVideoHLSFiles),
  asyncMiddleware(proxifyHLS)
)

// ---------------------------------------------------------------------------

export {
  objectStorageProxyRouter
}

async function proxifyWebTorrent (req: express.Request, res: express.Response) {
  const filename = req.params.filename

  logger.debug('Proxifying WebTorrent file %s from object storage.', filename)

  try {
    const { response: s3Response, stream } = await getWebTorrentFileReadStream({
      filename,
      rangeHeader: req.header('range')
    })

    setS3Headers(res, s3Response)

    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

  logger.debug('Proxifying HLS file %s from object storage.', filename)

  try {
    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, filename.endsWith('master.m3u8'))))
      : new PassThrough()

    return pipeline(
      stream,
      streamReplacer,
      res,
      err => {
        if (!err) return

        handleObjectStorageFailure(res, err)
      }
    )
  } catch (err) {
    return handleObjectStorageFailure(res, err)
  }
}

function handleObjectStorageFailure (res: express.Response, err: Error) {
  if (err.name === 'NoSuchKey') {
    logger.debug('Could not find key in object storage to proxify private HLS video file.', { err })
    return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
  }

  return res.fail({
    status: HttpStatusCode.INTERNAL_SERVER_ERROR_500,
    message: err.message,
    type: err.name
  })
}

function setS3Headers (res: express.Response, s3Response: GetObjectCommandOutput) {
  if (s3Response.$metadata.httpStatusCode === HttpStatusCode.PARTIAL_CONTENT_206) {
    res.setHeader('Content-Range', s3Response.ContentRange)
    res.status(HttpStatusCode.PARTIAL_CONTENT_206)
  }
}