1 import cors from 'cors'
2 import express from 'express'
3 import { readFile } from 'fs-extra'
4 import { join } from 'path'
5 import { injectQueryToPlaylistUrls } from '@server/lib/hls'
8 ensureCanAccessPrivateVideoHLSFiles,
9 ensureCanAccessVideoPrivateWebTorrentFiles,
12 } from '@server/middlewares'
13 import { HttpStatusCode } from '@shared/models'
14 import { CONFIG } from '../initializers/config'
15 import { DIRECTORIES, STATIC_MAX_AGE, STATIC_PATHS } from '../initializers/constants'
16 import { buildReinjectVideoFileTokenQuery, doReinjectVideoFileToken } from './shared/m3u8-playlist'
18 const staticRouter = express.Router()
20 // Cors is very important to let other servers access torrent and video files
21 staticRouter.use(cors())
23 // ---------------------------------------------------------------------------
24 // WebTorrent/Classic videos
25 // ---------------------------------------------------------------------------
27 const privateWebTorrentStaticMiddlewares = CONFIG.STATIC_FILES.PRIVATE_FILES_REQUIRE_AUTH === true
28 ? [ optionalAuthenticate, asyncMiddleware(ensureCanAccessVideoPrivateWebTorrentFiles) ]
32 STATIC_PATHS.PRIVATE_WEBSEED,
33 ...privateWebTorrentStaticMiddlewares,
34 express.static(DIRECTORIES.VIDEOS.PRIVATE, { fallthrough: false }),
39 express.static(DIRECTORIES.VIDEOS.PUBLIC, { fallthrough: false }),
44 STATIC_PATHS.REDUNDANCY,
45 express.static(CONFIG.STORAGE.REDUNDANCY_DIR, { fallthrough: false }),
49 // ---------------------------------------------------------------------------
51 // ---------------------------------------------------------------------------
53 const privateHLSStaticMiddlewares = CONFIG.STATIC_FILES.PRIVATE_FILES_REQUIRE_AUTH === true
54 ? [ optionalAuthenticate, asyncMiddleware(ensureCanAccessPrivateVideoHLSFiles) ]
58 STATIC_PATHS.STREAMING_PLAYLISTS.PRIVATE_HLS + ':videoUUID/:playlistName.m3u8',
59 ...privateHLSStaticMiddlewares,
60 asyncMiddleware(servePrivateM3U8)
64 STATIC_PATHS.STREAMING_PLAYLISTS.PRIVATE_HLS,
65 ...privateHLSStaticMiddlewares,
66 express.static(DIRECTORIES.HLS_STREAMING_PLAYLIST.PRIVATE, { fallthrough: false }),
70 STATIC_PATHS.STREAMING_PLAYLISTS.HLS,
71 express.static(DIRECTORIES.HLS_STREAMING_PLAYLIST.PUBLIC, { fallthrough: false }),
75 // Thumbnails path for express
76 const thumbnailsPhysicalPath = CONFIG.STORAGE.THUMBNAILS_DIR
78 STATIC_PATHS.THUMBNAILS,
79 express.static(thumbnailsPhysicalPath, { maxAge: STATIC_MAX_AGE.SERVER, fallthrough: false }),
83 // ---------------------------------------------------------------------------
89 // ---------------------------------------------------------------------------
91 async function servePrivateM3U8 (req: express.Request, res: express.Response) {
92 const path = join(DIRECTORIES.HLS_STREAMING_PLAYLIST.PRIVATE, req.params.videoUUID, req.params.playlistName + '.m3u8')
93 const filename = req.params.playlistName + '.m3u8'
95 let playlistContent: string
98 playlistContent = await readFile(path, 'utf-8')
100 if (err.message.includes('ENOENT')) {
102 status: HttpStatusCode.NOT_FOUND_404,
103 message: 'File not found'
110 // Inject token in playlist so players that cannot alter the HTTP request can still watch the video
111 const transformedContent = doReinjectVideoFileToken(req)
112 ? injectQueryToPlaylistUrls(playlistContent, buildReinjectVideoFileTokenQuery(req, filename.endsWith('master.m3u8')))
115 return res.set('content-type', 'application/vnd.apple.mpegurl').send(transformedContent).end()