]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/download.ts
Limit live bitrate
[github/Chocobozzz/PeerTube.git] / server / controllers / download.ts
CommitLineData
90a8bd30
C
1import * as cors from 'cors'
2import * as express from 'express'
4bc45da3 3import { logger } from '@server/helpers/logger'
90a8bd30 4import { VideosTorrentCache } from '@server/lib/files-cache/videos-torrent-cache'
4bc45da3 5import { Hooks } from '@server/lib/plugins/hooks'
90a8bd30 6import { getVideoFilePath } from '@server/lib/video-paths'
4bc45da3 7import { MStreamingPlaylist, MVideo, MVideoFile, MVideoFullLight } from '@server/types/models'
4c7e60bc 8import { HttpStatusCode, VideoStreamingPlaylistType } from '@shared/models'
90a8bd30
C
9import { STATIC_DOWNLOAD_PATHS } from '../initializers/constants'
10import { asyncMiddleware, videosDownloadValidator } from '../middlewares'
11
12const downloadRouter = express.Router()
13
14downloadRouter.use(cors())
15
16downloadRouter.use(
17 STATIC_DOWNLOAD_PATHS.TORRENTS + ':filename',
4bc45da3 18 asyncMiddleware(downloadTorrent)
90a8bd30
C
19)
20
21downloadRouter.use(
22 STATIC_DOWNLOAD_PATHS.VIDEOS + ':id-:resolution([0-9]+).:extension',
23 asyncMiddleware(videosDownloadValidator),
4bc45da3 24 asyncMiddleware(downloadVideoFile)
90a8bd30
C
25)
26
27downloadRouter.use(
28 STATIC_DOWNLOAD_PATHS.HLS_VIDEOS + ':id-:resolution([0-9]+)-fragmented.:extension',
29 asyncMiddleware(videosDownloadValidator),
4bc45da3 30 asyncMiddleware(downloadHLSVideoFile)
90a8bd30
C
31)
32
33// ---------------------------------------------------------------------------
34
35export {
36 downloadRouter
37}
38
39// ---------------------------------------------------------------------------
40
41async function downloadTorrent (req: express.Request, res: express.Response) {
42 const result = await VideosTorrentCache.Instance.getFilePath(req.params.filename)
76148b27
RK
43 if (!result) {
44 return res.fail({
45 status: HttpStatusCode.NOT_FOUND_404,
46 message: 'Torrent file not found'
47 })
48 }
90a8bd30 49
4bc45da3
C
50 const allowParameters = { torrentPath: result.path, downloadName: result.downloadName }
51
52 const allowedResult = await Hooks.wrapFun(
53 isTorrentDownloadAllowed,
54 allowParameters,
55 'filter:api.download.torrent.allowed.result'
56 )
57
58 if (!checkAllowResult(res, allowParameters, allowedResult)) return
59
90a8bd30
C
60 return res.download(result.path, result.downloadName)
61}
62
4bc45da3 63async function downloadVideoFile (req: express.Request, res: express.Response) {
90a8bd30
C
64 const video = res.locals.videoAll
65
66 const videoFile = getVideoFile(req, video.VideoFiles)
76148b27
RK
67 if (!videoFile) {
68 return res.fail({
69 status: HttpStatusCode.NOT_FOUND_404,
70 message: 'Video file not found'
71 })
72 }
90a8bd30 73
4bc45da3
C
74 const allowParameters = { video, videoFile }
75
76 const allowedResult = await Hooks.wrapFun(
77 isVideoDownloadAllowed,
78 allowParameters,
79 'filter:api.download.video.allowed.result'
80 )
81
82 if (!checkAllowResult(res, allowParameters, allowedResult)) return
83
90a8bd30
C
84 return res.download(getVideoFilePath(video, videoFile), `${video.name}-${videoFile.resolution}p${videoFile.extname}`)
85}
86
4bc45da3 87async function downloadHLSVideoFile (req: express.Request, res: express.Response) {
90a8bd30 88 const video = res.locals.videoAll
4bc45da3
C
89 const streamingPlaylist = getHLSPlaylist(video)
90 if (!streamingPlaylist) return res.status(HttpStatusCode.NOT_FOUND_404).end
90a8bd30 91
4bc45da3 92 const videoFile = getVideoFile(req, streamingPlaylist.VideoFiles)
76148b27
RK
93 if (!videoFile) {
94 return res.fail({
95 status: HttpStatusCode.NOT_FOUND_404,
96 message: 'Video file not found'
97 })
98 }
90a8bd30 99
4bc45da3
C
100 const allowParameters = { video, streamingPlaylist, videoFile }
101
102 const allowedResult = await Hooks.wrapFun(
103 isVideoDownloadAllowed,
104 allowParameters,
105 'filter:api.download.video.allowed.result'
106 )
107
108 if (!checkAllowResult(res, allowParameters, allowedResult)) return
109
110 const filename = `${video.name}-${videoFile.resolution}p-${streamingPlaylist.getStringType()}${videoFile.extname}`
111 return res.download(getVideoFilePath(streamingPlaylist, videoFile), filename)
90a8bd30
C
112}
113
114function getVideoFile (req: express.Request, files: MVideoFile[]) {
115 const resolution = parseInt(req.params.resolution, 10)
116 return files.find(f => f.resolution === resolution)
117}
118
119function getHLSPlaylist (video: MVideoFullLight) {
120 const playlist = video.VideoStreamingPlaylists.find(p => p.type === VideoStreamingPlaylistType.HLS)
121 if (!playlist) return undefined
122
123 return Object.assign(playlist, { Video: video })
124}
4bc45da3
C
125
126type AllowedResult = {
127 allowed: boolean
128 errorMessage?: string
129}
130
131function isTorrentDownloadAllowed (_object: {
132 torrentPath: string
133}): AllowedResult {
134 return { allowed: true }
135}
136
137function isVideoDownloadAllowed (_object: {
138 video: MVideo
139 videoFile: MVideoFile
140 streamingPlaylist?: MStreamingPlaylist
141}): AllowedResult {
142 return { allowed: true }
143}
144
145function checkAllowResult (res: express.Response, allowParameters: any, result?: AllowedResult) {
146 if (!result || result.allowed !== true) {
147 logger.info('Download is not allowed.', { result, allowParameters })
4bc45da3 148
76148b27
RK
149 res.fail({
150 status: HttpStatusCode.FORBIDDEN_403,
151 message: result?.errorMessage || 'Refused download'
152 })
4bc45da3
C
153 return false
154 }
155
156 return true
157}