]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/controllers/static.ts
Fix s3 mock cleanup
[github/Chocobozzz/PeerTube.git] / server / controllers / static.ts
... / ...
CommitLineData
1import cors from 'cors'
2import express from 'express'
3import { readFile } from 'fs-extra'
4import { join } from 'path'
5import { injectQueryToPlaylistUrls } from '@server/lib/hls'
6import {
7 asyncMiddleware,
8 ensureCanAccessPrivateVideoHLSFiles,
9 ensureCanAccessVideoPrivateWebTorrentFiles,
10 handleStaticError,
11 optionalAuthenticate
12} from '@server/middlewares'
13import { HttpStatusCode } from '@shared/models'
14import { CONFIG } from '../initializers/config'
15import { DIRECTORIES, STATIC_MAX_AGE, STATIC_PATHS } from '../initializers/constants'
16import { buildReinjectVideoFileTokenQuery, doReinjectVideoFileToken } from './shared/m3u8-playlist'
17
18const staticRouter = express.Router()
19
20// Cors is very important to let other servers access torrent and video files
21staticRouter.use(cors())
22
23// ---------------------------------------------------------------------------
24// WebTorrent/Classic videos
25// ---------------------------------------------------------------------------
26
27const privateWebTorrentStaticMiddlewares = CONFIG.STATIC_FILES.PRIVATE_FILES_REQUIRE_AUTH === true
28 ? [ optionalAuthenticate, asyncMiddleware(ensureCanAccessVideoPrivateWebTorrentFiles) ]
29 : []
30
31staticRouter.use(
32 STATIC_PATHS.PRIVATE_WEBSEED,
33 ...privateWebTorrentStaticMiddlewares,
34 express.static(DIRECTORIES.VIDEOS.PRIVATE, { fallthrough: false }),
35 handleStaticError
36)
37staticRouter.use(
38 STATIC_PATHS.WEBSEED,
39 express.static(DIRECTORIES.VIDEOS.PUBLIC, { fallthrough: false }),
40 handleStaticError
41)
42
43staticRouter.use(
44 STATIC_PATHS.REDUNDANCY,
45 express.static(CONFIG.STORAGE.REDUNDANCY_DIR, { fallthrough: false }),
46 handleStaticError
47)
48
49// ---------------------------------------------------------------------------
50// HLS
51// ---------------------------------------------------------------------------
52
53const privateHLSStaticMiddlewares = CONFIG.STATIC_FILES.PRIVATE_FILES_REQUIRE_AUTH === true
54 ? [ optionalAuthenticate, asyncMiddleware(ensureCanAccessPrivateVideoHLSFiles) ]
55 : []
56
57staticRouter.use(
58 STATIC_PATHS.STREAMING_PLAYLISTS.PRIVATE_HLS + ':videoUUID/:playlistName.m3u8',
59 ...privateHLSStaticMiddlewares,
60 asyncMiddleware(servePrivateM3U8)
61)
62
63staticRouter.use(
64 STATIC_PATHS.STREAMING_PLAYLISTS.PRIVATE_HLS,
65 ...privateHLSStaticMiddlewares,
66 express.static(DIRECTORIES.HLS_STREAMING_PLAYLIST.PRIVATE, { fallthrough: false }),
67 handleStaticError
68)
69staticRouter.use(
70 STATIC_PATHS.STREAMING_PLAYLISTS.HLS,
71 express.static(DIRECTORIES.HLS_STREAMING_PLAYLIST.PUBLIC, { fallthrough: false }),
72 handleStaticError
73)
74
75// Thumbnails path for express
76const thumbnailsPhysicalPath = CONFIG.STORAGE.THUMBNAILS_DIR
77staticRouter.use(
78 STATIC_PATHS.THUMBNAILS,
79 express.static(thumbnailsPhysicalPath, { maxAge: STATIC_MAX_AGE.SERVER, fallthrough: false }),
80 handleStaticError
81)
82
83// ---------------------------------------------------------------------------
84
85export {
86 staticRouter
87}
88
89// ---------------------------------------------------------------------------
90
91async 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'
94
95 let playlistContent: string
96
97 try {
98 playlistContent = await readFile(path, 'utf-8')
99 } catch (err) {
100 if (err.message.includes('ENOENT')) {
101 return res.fail({
102 status: HttpStatusCode.NOT_FOUND_404,
103 message: 'File not found'
104 })
105 }
106
107 throw err
108 }
109
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')))
113 : playlistContent
114
115 return res.set('content-type', 'application/vnd.apple.mpegurl').send(transformedContent).end()
116}