1 import express from 'express'
2 import { query } from 'express-validator'
3 import LRUCache from 'lru-cache'
4 import { basename, dirname } from 'path'
5 import { exists, isUUIDValid } from '@server/helpers/custom-validators/misc'
6 import { logger } from '@server/helpers/logger'
7 import { LRU_CACHE } from '@server/initializers/constants'
8 import { VideoModel } from '@server/models/video/video'
9 import { VideoFileModel } from '@server/models/video/video-file'
10 import { MStreamingPlaylist, MVideoFile, MVideoThumbnail } from '@server/types/models'
11 import { HttpStatusCode } from '@shared/models'
12 import { areValidationErrors, checkCanAccessVideoStaticFiles } from './shared'
16 video?: MVideoThumbnail
18 playlist?: MStreamingPlaylist }
20 const staticFileTokenBypass = new LRUCache<string, LRUValue>({
21 max: LRU_CACHE.STATIC_VIDEO_FILES_RIGHTS_CHECK.MAX_SIZE,
22 ttl: LRU_CACHE.STATIC_VIDEO_FILES_RIGHTS_CHECK.TTL
25 const ensureCanAccessVideoPrivateWebTorrentFiles = [
26 query('videoFileToken').optional().custom(exists),
28 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
29 if (areValidationErrors(req, res)) return
31 const token = extractTokenOrDie(req, res)
34 const cacheKey = token + '-' + req.originalUrl
36 if (staticFileTokenBypass.has(cacheKey)) {
37 const { allowed, file, video } = staticFileTokenBypass.get(cacheKey)
39 if (allowed === true) {
40 res.locals.onlyVideo = video
41 res.locals.videoFile = file
46 return res.sendStatus(HttpStatusCode.FORBIDDEN_403)
49 const result = await isWebTorrentAllowed(req, res)
51 staticFileTokenBypass.set(cacheKey, result)
53 if (result.allowed !== true) return
55 res.locals.onlyVideo = result.video
56 res.locals.videoFile = result.file
62 const ensureCanAccessPrivateVideoHLSFiles = [
63 query('videoFileToken').optional().custom(exists),
65 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
66 if (areValidationErrors(req, res)) return
68 const videoUUID = basename(dirname(req.originalUrl))
70 if (!isUUIDValid(videoUUID)) {
71 logger.debug('Path does not contain valid video UUID to serve static file %s', req.originalUrl)
73 return res.sendStatus(HttpStatusCode.FORBIDDEN_403)
76 const token = extractTokenOrDie(req, res)
79 const cacheKey = token + '-' + videoUUID
81 if (staticFileTokenBypass.has(cacheKey)) {
82 const { allowed, file, playlist, video } = staticFileTokenBypass.get(cacheKey)
84 if (allowed === true) {
85 res.locals.onlyVideo = video
86 res.locals.videoFile = file
87 res.locals.videoStreamingPlaylist = playlist
92 return res.sendStatus(HttpStatusCode.FORBIDDEN_403)
95 const result = await isHLSAllowed(req, res, videoUUID)
97 staticFileTokenBypass.set(cacheKey, result)
99 if (result.allowed !== true) return
101 res.locals.onlyVideo = result.video
102 res.locals.videoFile = result.file
103 res.locals.videoStreamingPlaylist = result.playlist
110 ensureCanAccessVideoPrivateWebTorrentFiles,
111 ensureCanAccessPrivateVideoHLSFiles
114 // ---------------------------------------------------------------------------
116 async function isWebTorrentAllowed (req: express.Request, res: express.Response) {
117 const filename = basename(req.path)
119 const file = await VideoFileModel.loadWithVideoByFilename(filename)
121 logger.debug('Unknown static file %s to serve', req.originalUrl, { filename })
123 res.sendStatus(HttpStatusCode.FORBIDDEN_403)
124 return { allowed: false }
127 const video = await VideoModel.load(file.getVideo().id)
132 allowed: await checkCanAccessVideoStaticFiles({ req, res, video, paramId: video.uuid })
136 async function isHLSAllowed (req: express.Request, res: express.Response, videoUUID: string) {
137 const filename = basename(req.path)
139 const video = await VideoModel.loadWithFiles(videoUUID)
142 logger.debug('Unknown static file %s to serve', req.originalUrl, { videoUUID })
144 res.sendStatus(HttpStatusCode.FORBIDDEN_403)
145 return { allowed: false }
148 const file = await VideoFileModel.loadByFilename(filename)
153 playlist: video.getHLSPlaylist(),
154 allowed: await checkCanAccessVideoStaticFiles({ req, res, video, paramId: video.uuid })
158 function extractTokenOrDie (req: express.Request, res: express.Response) {
159 const token = res.locals.oauth?.token.accessToken || req.query.videoFileToken
163 message: 'Bearer token is missing in headers or video file token is missing in URL query parameters',
164 status: HttpStatusCode.FORBIDDEN_403