aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/static.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/controllers/static.ts')
-rw-r--r--server/controllers/static.ts88
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'
3import { join } from 'path' 3import { join } from 'path'
4import { getRegisteredPlugins, getRegisteredThemes } from '@server/controllers/api/config' 4import { getRegisteredPlugins, getRegisteredThemes } from '@server/controllers/api/config'
5import { serveIndexHTML } from '@server/lib/client-html' 5import { serveIndexHTML } from '@server/lib/client-html'
6import { getTorrentFilePath, getVideoFilePath } from '@server/lib/video-paths'
7import { MVideoFile, MVideoFullLight } from '@server/types/models'
8import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes' 6import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
9import { VideoStreamingPlaylistType } from '@shared/models/videos/video-streaming-playlist.type'
10import { HttpNodeinfoDiasporaSoftwareNsSchema20 } from '../../shared/models/nodeinfo' 7import { HttpNodeinfoDiasporaSoftwareNsSchema20 } from '../../shared/models/nodeinfo'
11import { root } from '../helpers/core-utils' 8import { root } from '../helpers/core-utils'
12import { CONFIG, isEmailEnabled } from '../initializers/config' 9import { 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'
24import { getThemeOrDefault } from '../lib/plugins/theme-utils' 20import { getThemeOrDefault } from '../lib/plugins/theme-utils'
25import { getEnabledResolutions } from '../lib/video-transcoding' 21import { getEnabledResolutions } from '../lib/video-transcoding'
26import { asyncMiddleware, videosDownloadValidator } from '../middlewares' 22import { asyncMiddleware } from '../middlewares'
27import { cacheRoute } from '../middlewares/cache' 23import { cacheRoute } from '../middlewares/cache'
28import { UserModel } from '../models/account/user' 24import { UserModel } from '../models/account/user'
29import { VideoModel } from '../models/video/video' 25import { 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
40const torrentsPhysicalPath = CONFIG.STORAGE.TORRENTS_DIR 37const torrentsPhysicalPath = CONFIG.STORAGE.TORRENTS_DIR
41staticRouter.use( 38staticRouter.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)
46staticRouter.use(
47 STATIC_DOWNLOAD_PATHS.TORRENTS + ':id-:resolution([0-9]+).torrent',
48 asyncMiddleware(videosDownloadValidator),
49 downloadTorrent
50)
51staticRouter.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
58staticRouter.use( 44staticRouter.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)
63staticRouter.use( 48staticRouter.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
69staticRouter.use(
70 STATIC_DOWNLOAD_PATHS.VIDEOS + ':id-:resolution([0-9]+).:extension',
71 asyncMiddleware(videosDownloadValidator),
72 downloadVideoFile
73)
74
75staticRouter.use(
76 STATIC_DOWNLOAD_PATHS.HLS_VIDEOS + ':id-:resolution([0-9]+)-fragmented.:extension',
77 asyncMiddleware(videosDownloadValidator),
78 downloadHLSVideoFile
79)
80
81// HLS 53// HLS
82staticRouter.use( 54staticRouter.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
330function 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
339function 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
351function 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
360function 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
372function getVideoFile (req: express.Request, files: MVideoFile[]) {
373 const resolution = parseInt(req.params.resolution, 10)
374 return files.find(f => f.resolution === resolution)
375}
376
377function 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
384function getCup (req: express.Request, res: express.Response, next: express.NextFunction) { 302function 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')