diff options
author | Chocobozzz <me@florianbigard.com> | 2021-02-16 16:25:53 +0100 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2021-02-18 13:38:09 +0100 |
commit | 90a8bd305de4153ec21137a73ff482dcc2e3e19b (patch) | |
tree | 2e35b5504ec11bc51579c92a70c77ed3d5ace816 /server/controllers/static.ts | |
parent | 684cdacbbd775b5f404dd7b373e02dd21baf5ff0 (diff) | |
download | PeerTube-90a8bd305de4153ec21137a73ff482dcc2e3e19b.tar.gz PeerTube-90a8bd305de4153ec21137a73ff482dcc2e3e19b.tar.zst PeerTube-90a8bd305de4153ec21137a73ff482dcc2e3e19b.zip |
Dissociate video file names and video uuid
Diffstat (limited to 'server/controllers/static.ts')
-rw-r--r-- | server/controllers/static.ts | 88 |
1 files changed, 3 insertions, 85 deletions
diff --git a/server/controllers/static.ts b/server/controllers/static.ts index 2064857eb..7cc7f2c62 100644 --- a/server/controllers/static.ts +++ b/server/controllers/static.ts | |||
@@ -3,10 +3,7 @@ import * as express from 'express' | |||
3 | import { join } from 'path' | 3 | import { join } from 'path' |
4 | import { getRegisteredPlugins, getRegisteredThemes } from '@server/controllers/api/config' | 4 | import { getRegisteredPlugins, getRegisteredThemes } from '@server/controllers/api/config' |
5 | import { serveIndexHTML } from '@server/lib/client-html' | 5 | import { serveIndexHTML } from '@server/lib/client-html' |
6 | import { getTorrentFilePath, getVideoFilePath } from '@server/lib/video-paths' | ||
7 | import { MVideoFile, MVideoFullLight } from '@server/types/models' | ||
8 | import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes' | 6 | import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes' |
9 | import { VideoStreamingPlaylistType } from '@shared/models/videos/video-streaming-playlist.type' | ||
10 | import { HttpNodeinfoDiasporaSoftwareNsSchema20 } from '../../shared/models/nodeinfo' | 7 | import { HttpNodeinfoDiasporaSoftwareNsSchema20 } from '../../shared/models/nodeinfo' |
11 | import { root } from '../helpers/core-utils' | 8 | import { root } from '../helpers/core-utils' |
12 | import { CONFIG, isEmailEnabled } from '../initializers/config' | 9 | import { CONFIG, isEmailEnabled } from '../initializers/config' |
@@ -16,14 +13,13 @@ import { | |||
16 | HLS_STREAMING_PLAYLIST_DIRECTORY, | 13 | HLS_STREAMING_PLAYLIST_DIRECTORY, |
17 | PEERTUBE_VERSION, | 14 | PEERTUBE_VERSION, |
18 | ROUTE_CACHE_LIFETIME, | 15 | ROUTE_CACHE_LIFETIME, |
19 | STATIC_DOWNLOAD_PATHS, | ||
20 | STATIC_MAX_AGE, | 16 | STATIC_MAX_AGE, |
21 | STATIC_PATHS, | 17 | STATIC_PATHS, |
22 | WEBSERVER | 18 | WEBSERVER |
23 | } from '../initializers/constants' | 19 | } from '../initializers/constants' |
24 | import { getThemeOrDefault } from '../lib/plugins/theme-utils' | 20 | import { getThemeOrDefault } from '../lib/plugins/theme-utils' |
25 | import { getEnabledResolutions } from '../lib/video-transcoding' | 21 | import { getEnabledResolutions } from '../lib/video-transcoding' |
26 | import { asyncMiddleware, videosDownloadValidator } from '../middlewares' | 22 | import { asyncMiddleware } from '../middlewares' |
27 | import { cacheRoute } from '../middlewares/cache' | 23 | import { cacheRoute } from '../middlewares/cache' |
28 | import { UserModel } from '../models/account/user' | 24 | import { UserModel } from '../models/account/user' |
29 | import { VideoModel } from '../models/video/video' | 25 | import { VideoModel } from '../models/video/video' |
@@ -37,47 +33,23 @@ staticRouter.use(cors()) | |||
37 | Cors is very important to let other servers access torrent and video files | 33 | Cors is very important to let other servers access torrent and video files |
38 | */ | 34 | */ |
39 | 35 | ||
36 | // FIXME: deprecated in 3.2, use lazy-statics instead | ||
40 | const torrentsPhysicalPath = CONFIG.STORAGE.TORRENTS_DIR | 37 | const torrentsPhysicalPath = CONFIG.STORAGE.TORRENTS_DIR |
41 | staticRouter.use( | 38 | staticRouter.use( |
42 | STATIC_PATHS.TORRENTS, | 39 | STATIC_PATHS.TORRENTS, |
43 | cors(), | ||
44 | express.static(torrentsPhysicalPath, { maxAge: 0 }) // Don't cache because we could regenerate the torrent file | 40 | express.static(torrentsPhysicalPath, { maxAge: 0 }) // Don't cache because we could regenerate the torrent file |
45 | ) | 41 | ) |
46 | staticRouter.use( | ||
47 | STATIC_DOWNLOAD_PATHS.TORRENTS + ':id-:resolution([0-9]+).torrent', | ||
48 | asyncMiddleware(videosDownloadValidator), | ||
49 | downloadTorrent | ||
50 | ) | ||
51 | staticRouter.use( | ||
52 | STATIC_DOWNLOAD_PATHS.TORRENTS + ':id-:resolution([0-9]+)-hls.torrent', | ||
53 | asyncMiddleware(videosDownloadValidator), | ||
54 | downloadHLSVideoFileTorrent | ||
55 | ) | ||
56 | 42 | ||
57 | // Videos path for webseeding | 43 | // Videos path for webseed |
58 | staticRouter.use( | 44 | staticRouter.use( |
59 | STATIC_PATHS.WEBSEED, | 45 | STATIC_PATHS.WEBSEED, |
60 | cors(), | ||
61 | express.static(CONFIG.STORAGE.VIDEOS_DIR, { fallthrough: false }) // 404 because we don't have this video | 46 | express.static(CONFIG.STORAGE.VIDEOS_DIR, { fallthrough: false }) // 404 because we don't have this video |
62 | ) | 47 | ) |
63 | staticRouter.use( | 48 | staticRouter.use( |
64 | STATIC_PATHS.REDUNDANCY, | 49 | STATIC_PATHS.REDUNDANCY, |
65 | cors(), | ||
66 | express.static(CONFIG.STORAGE.REDUNDANCY_DIR, { fallthrough: false }) // 404 because we don't have this video | 50 | express.static(CONFIG.STORAGE.REDUNDANCY_DIR, { fallthrough: false }) // 404 because we don't have this video |
67 | ) | 51 | ) |
68 | 52 | ||
69 | staticRouter.use( | ||
70 | STATIC_DOWNLOAD_PATHS.VIDEOS + ':id-:resolution([0-9]+).:extension', | ||
71 | asyncMiddleware(videosDownloadValidator), | ||
72 | downloadVideoFile | ||
73 | ) | ||
74 | |||
75 | staticRouter.use( | ||
76 | STATIC_DOWNLOAD_PATHS.HLS_VIDEOS + ':id-:resolution([0-9]+)-fragmented.:extension', | ||
77 | asyncMiddleware(videosDownloadValidator), | ||
78 | downloadHLSVideoFile | ||
79 | ) | ||
80 | |||
81 | // HLS | 53 | // HLS |
82 | staticRouter.use( | 54 | staticRouter.use( |
83 | STATIC_PATHS.STREAMING_PLAYLISTS.HLS, | 55 | STATIC_PATHS.STREAMING_PLAYLISTS.HLS, |
@@ -327,60 +299,6 @@ async function generateNodeinfo (req: express.Request, res: express.Response) { | |||
327 | return res.send(json).end() | 299 | return res.send(json).end() |
328 | } | 300 | } |
329 | 301 | ||
330 | function downloadTorrent (req: express.Request, res: express.Response) { | ||
331 | const video = res.locals.videoAll | ||
332 | |||
333 | const videoFile = getVideoFile(req, video.VideoFiles) | ||
334 | if (!videoFile) return res.status(HttpStatusCode.NOT_FOUND_404).end() | ||
335 | |||
336 | return res.download(getTorrentFilePath(video, videoFile), `${video.name}-${videoFile.resolution}p.torrent`) | ||
337 | } | ||
338 | |||
339 | function downloadHLSVideoFileTorrent (req: express.Request, res: express.Response) { | ||
340 | const video = res.locals.videoAll | ||
341 | |||
342 | const playlist = getHLSPlaylist(video) | ||
343 | if (!playlist) return res.status(HttpStatusCode.NOT_FOUND_404).end | ||
344 | |||
345 | const videoFile = getVideoFile(req, playlist.VideoFiles) | ||
346 | if (!videoFile) return res.status(HttpStatusCode.NOT_FOUND_404).end() | ||
347 | |||
348 | return res.download(getTorrentFilePath(playlist, videoFile), `${video.name}-${videoFile.resolution}p-hls.torrent`) | ||
349 | } | ||
350 | |||
351 | function downloadVideoFile (req: express.Request, res: express.Response) { | ||
352 | const video = res.locals.videoAll | ||
353 | |||
354 | const videoFile = getVideoFile(req, video.VideoFiles) | ||
355 | if (!videoFile) return res.status(HttpStatusCode.NOT_FOUND_404).end() | ||
356 | |||
357 | return res.download(getVideoFilePath(video, videoFile), `${video.name}-${videoFile.resolution}p${videoFile.extname}`) | ||
358 | } | ||
359 | |||
360 | function downloadHLSVideoFile (req: express.Request, res: express.Response) { | ||
361 | const video = res.locals.videoAll | ||
362 | const playlist = getHLSPlaylist(video) | ||
363 | if (!playlist) return res.status(HttpStatusCode.NOT_FOUND_404).end | ||
364 | |||
365 | const videoFile = getVideoFile(req, playlist.VideoFiles) | ||
366 | if (!videoFile) return res.status(HttpStatusCode.NOT_FOUND_404).end() | ||
367 | |||
368 | const filename = `${video.name}-${videoFile.resolution}p-${playlist.getStringType()}${videoFile.extname}` | ||
369 | return res.download(getVideoFilePath(playlist, videoFile), filename) | ||
370 | } | ||
371 | |||
372 | function getVideoFile (req: express.Request, files: MVideoFile[]) { | ||
373 | const resolution = parseInt(req.params.resolution, 10) | ||
374 | return files.find(f => f.resolution === resolution) | ||
375 | } | ||
376 | |||
377 | function getHLSPlaylist (video: MVideoFullLight) { | ||
378 | const playlist = video.VideoStreamingPlaylists.find(p => p.type === VideoStreamingPlaylistType.HLS) | ||
379 | if (!playlist) return undefined | ||
380 | |||
381 | return Object.assign(playlist, { Video: video }) | ||
382 | } | ||
383 | |||
384 | function getCup (req: express.Request, res: express.Response, next: express.NextFunction) { | 302 | function getCup (req: express.Request, res: express.Response, next: express.NextFunction) { |
385 | res.status(HttpStatusCode.I_AM_A_TEAPOT_418) | 303 | res.status(HttpStatusCode.I_AM_A_TEAPOT_418) |
386 | res.setHeader('Accept-Additions', 'Non-Dairy;1,Sugar;1') | 304 | res.setHeader('Accept-Additions', 'Non-Dairy;1,Sugar;1') |