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