]>
Commit | Line | Data |
---|---|---|
1 | import cors from 'cors' | |
2 | import 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 { VideoPathManager } from '@server/lib/video-path-manager' | |
7 | import { MStreamingPlaylist, MVideo, MVideoFile, MVideoFullLight } from '@server/types/models' | |
8 | import { addQueryParams, forceNumber } from '@shared/core-utils' | |
9 | import { HttpStatusCode, VideoStorage, VideoStreamingPlaylistType } from '@shared/models' | |
10 | import { STATIC_DOWNLOAD_PATHS } from '../initializers/constants' | |
11 | import { asyncMiddleware, optionalAuthenticate, 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 | optionalAuthenticate, | |
25 | asyncMiddleware(videosDownloadValidator), | |
26 | asyncMiddleware(downloadVideoFile) | |
27 | ) | |
28 | ||
29 | downloadRouter.use( | |
30 | STATIC_DOWNLOAD_PATHS.HLS_VIDEOS + ':id-:resolution([0-9]+)-fragmented.:extension', | |
31 | optionalAuthenticate, | |
32 | asyncMiddleware(videosDownloadValidator), | |
33 | asyncMiddleware(downloadHLSVideoFile) | |
34 | ) | |
35 | ||
36 | // --------------------------------------------------------------------------- | |
37 | ||
38 | export { | |
39 | downloadRouter | |
40 | } | |
41 | ||
42 | // --------------------------------------------------------------------------- | |
43 | ||
44 | async function downloadTorrent (req: express.Request, res: express.Response) { | |
45 | const result = await VideosTorrentCache.Instance.getFilePath(req.params.filename) | |
46 | if (!result) { | |
47 | return res.fail({ | |
48 | status: HttpStatusCode.NOT_FOUND_404, | |
49 | message: 'Torrent file not found' | |
50 | }) | |
51 | } | |
52 | ||
53 | const allowParameters = { | |
54 | req, | |
55 | res, | |
56 | torrentPath: result.path, | |
57 | downloadName: result.downloadName | |
58 | } | |
59 | ||
60 | const allowedResult = await Hooks.wrapFun( | |
61 | isTorrentDownloadAllowed, | |
62 | allowParameters, | |
63 | 'filter:api.download.torrent.allowed.result' | |
64 | ) | |
65 | ||
66 | if (!checkAllowResult(res, allowParameters, allowedResult)) return | |
67 | ||
68 | return res.download(result.path, result.downloadName) | |
69 | } | |
70 | ||
71 | async function downloadVideoFile (req: express.Request, res: express.Response) { | |
72 | const video = res.locals.videoAll | |
73 | ||
74 | const videoFile = getVideoFile(req, video.VideoFiles) | |
75 | if (!videoFile) { | |
76 | return res.fail({ | |
77 | status: HttpStatusCode.NOT_FOUND_404, | |
78 | message: 'Video file not found' | |
79 | }) | |
80 | } | |
81 | ||
82 | const allowParameters = { | |
83 | req, | |
84 | res, | |
85 | video, | |
86 | videoFile | |
87 | } | |
88 | ||
89 | const allowedResult = await Hooks.wrapFun( | |
90 | isVideoDownloadAllowed, | |
91 | allowParameters, | |
92 | 'filter:api.download.video.allowed.result' | |
93 | ) | |
94 | ||
95 | if (!checkAllowResult(res, allowParameters, allowedResult)) return | |
96 | ||
97 | if (videoFile.storage === VideoStorage.OBJECT_STORAGE) { | |
98 | return redirectToObjectStorage({ req, res, video, file: videoFile }) | |
99 | } | |
100 | ||
101 | await VideoPathManager.Instance.makeAvailableVideoFile(videoFile.withVideoOrPlaylist(video), path => { | |
102 | // Express uses basename on filename parameter | |
103 | const videoName = video.name.replace(/[/\\]/g, '_') | |
104 | const filename = `${videoName}-${videoFile.resolution}p${videoFile.extname}` | |
105 | ||
106 | return res.download(path, filename) | |
107 | }) | |
108 | } | |
109 | ||
110 | async function downloadHLSVideoFile (req: express.Request, res: express.Response) { | |
111 | const video = res.locals.videoAll | |
112 | const streamingPlaylist = getHLSPlaylist(video) | |
113 | if (!streamingPlaylist) return res.status(HttpStatusCode.NOT_FOUND_404).end | |
114 | ||
115 | const videoFile = getVideoFile(req, streamingPlaylist.VideoFiles) | |
116 | if (!videoFile) { | |
117 | return res.fail({ | |
118 | status: HttpStatusCode.NOT_FOUND_404, | |
119 | message: 'Video file not found' | |
120 | }) | |
121 | } | |
122 | ||
123 | const allowParameters = { | |
124 | req, | |
125 | res, | |
126 | video, | |
127 | streamingPlaylist, | |
128 | videoFile | |
129 | } | |
130 | ||
131 | const allowedResult = await Hooks.wrapFun( | |
132 | isVideoDownloadAllowed, | |
133 | allowParameters, | |
134 | 'filter:api.download.video.allowed.result' | |
135 | ) | |
136 | ||
137 | if (!checkAllowResult(res, allowParameters, allowedResult)) return | |
138 | ||
139 | if (videoFile.storage === VideoStorage.OBJECT_STORAGE) { | |
140 | return redirectToObjectStorage({ req, res, video, file: videoFile }) | |
141 | } | |
142 | ||
143 | await VideoPathManager.Instance.makeAvailableVideoFile(videoFile.withVideoOrPlaylist(streamingPlaylist), path => { | |
144 | const filename = `${video.name}-${videoFile.resolution}p-${streamingPlaylist.getStringType()}${videoFile.extname}` | |
145 | ||
146 | return res.download(path, filename) | |
147 | }) | |
148 | } | |
149 | ||
150 | function getVideoFile (req: express.Request, files: MVideoFile[]) { | |
151 | const resolution = forceNumber(req.params.resolution) | |
152 | return files.find(f => f.resolution === resolution) | |
153 | } | |
154 | ||
155 | function getHLSPlaylist (video: MVideoFullLight) { | |
156 | const playlist = video.VideoStreamingPlaylists.find(p => p.type === VideoStreamingPlaylistType.HLS) | |
157 | if (!playlist) return undefined | |
158 | ||
159 | return Object.assign(playlist, { Video: video }) | |
160 | } | |
161 | ||
162 | type AllowedResult = { | |
163 | allowed: boolean | |
164 | errorMessage?: string | |
165 | } | |
166 | ||
167 | function isTorrentDownloadAllowed (_object: { | |
168 | torrentPath: string | |
169 | }): AllowedResult { | |
170 | return { allowed: true } | |
171 | } | |
172 | ||
173 | function isVideoDownloadAllowed (_object: { | |
174 | video: MVideo | |
175 | videoFile: MVideoFile | |
176 | streamingPlaylist?: MStreamingPlaylist | |
177 | }): AllowedResult { | |
178 | return { allowed: true } | |
179 | } | |
180 | ||
181 | function checkAllowResult (res: express.Response, allowParameters: any, result?: AllowedResult) { | |
182 | if (!result || result.allowed !== true) { | |
183 | logger.info('Download is not allowed.', { result, allowParameters }) | |
184 | ||
185 | res.fail({ | |
186 | status: HttpStatusCode.FORBIDDEN_403, | |
187 | message: result?.errorMessage || 'Refused download' | |
188 | }) | |
189 | return false | |
190 | } | |
191 | ||
192 | return true | |
193 | } | |
194 | ||
195 | function redirectToObjectStorage (options: { | |
196 | req: express.Request | |
197 | res: express.Response | |
198 | video: MVideo | |
199 | file: MVideoFile | |
200 | }) { | |
201 | const { req, res, video, file } = options | |
202 | ||
203 | const baseUrl = file.getObjectStorageUrl(video) | |
204 | ||
205 | const url = video.hasPrivateStaticPath() && req.query.videoFileToken | |
206 | ? addQueryParams(baseUrl, { videoFileToken: req.query.videoFileToken }) | |
207 | : baseUrl | |
208 | ||
209 | return res.redirect(url) | |
210 | } |