]>
Commit | Line | Data |
---|---|---|
41fb13c3 C |
1 | import cors from 'cors' |
2 | import express from 'express' | |
4bc45da3 | 3 | import { logger } from '@server/helpers/logger' |
90a8bd30 | 4 | import { VideosTorrentCache } from '@server/lib/files-cache/videos-torrent-cache' |
4bc45da3 | 5 | import { Hooks } from '@server/lib/plugins/hooks' |
0305db28 | 6 | import { VideoPathManager } from '@server/lib/video-path-manager' |
4bc45da3 | 7 | import { MStreamingPlaylist, MVideo, MVideoFile, MVideoFullLight } from '@server/types/models' |
0305db28 | 8 | import { HttpStatusCode, VideoStorage, VideoStreamingPlaylistType } from '@shared/models' |
90a8bd30 C |
9 | import { STATIC_DOWNLOAD_PATHS } from '../initializers/constants' |
10 | import { asyncMiddleware, videosDownloadValidator } from '../middlewares' | |
11 | ||
12 | const downloadRouter = express.Router() | |
13 | ||
14 | downloadRouter.use(cors()) | |
15 | ||
16 | downloadRouter.use( | |
17 | STATIC_DOWNLOAD_PATHS.TORRENTS + ':filename', | |
4bc45da3 | 18 | asyncMiddleware(downloadTorrent) |
90a8bd30 C |
19 | ) |
20 | ||
21 | downloadRouter.use( | |
22 | STATIC_DOWNLOAD_PATHS.VIDEOS + ':id-:resolution([0-9]+).:extension', | |
23 | asyncMiddleware(videosDownloadValidator), | |
4bc45da3 | 24 | asyncMiddleware(downloadVideoFile) |
90a8bd30 C |
25 | ) |
26 | ||
27 | downloadRouter.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 | ||
35 | export { | |
36 | downloadRouter | |
37 | } | |
38 | ||
39 | // --------------------------------------------------------------------------- | |
40 | ||
41 | async 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 | 63 | async 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 | ||
0305db28 JB |
84 | if (videoFile.storage === VideoStorage.OBJECT_STORAGE) { |
85 | return res.redirect(videoFile.getObjectStorageUrl()) | |
86 | } | |
87 | ||
ad5db104 | 88 | await VideoPathManager.Instance.makeAvailableVideoFile(videoFile.withVideoOrPlaylist(video), path => { |
62f53731 C |
89 | // Express uses basename on filename parameter |
90 | const videoName = video.name.replace(/[/\\]/g, '_') | |
91 | const filename = `${videoName}-${videoFile.resolution}p${videoFile.extname}` | |
0305db28 JB |
92 | |
93 | return res.download(path, filename) | |
94 | }) | |
90a8bd30 C |
95 | } |
96 | ||
4bc45da3 | 97 | async function downloadHLSVideoFile (req: express.Request, res: express.Response) { |
90a8bd30 | 98 | const video = res.locals.videoAll |
4bc45da3 C |
99 | const streamingPlaylist = getHLSPlaylist(video) |
100 | if (!streamingPlaylist) return res.status(HttpStatusCode.NOT_FOUND_404).end | |
90a8bd30 | 101 | |
4bc45da3 | 102 | const videoFile = getVideoFile(req, streamingPlaylist.VideoFiles) |
76148b27 RK |
103 | if (!videoFile) { |
104 | return res.fail({ | |
105 | status: HttpStatusCode.NOT_FOUND_404, | |
106 | message: 'Video file not found' | |
107 | }) | |
108 | } | |
90a8bd30 | 109 | |
4bc45da3 C |
110 | const allowParameters = { video, streamingPlaylist, videoFile } |
111 | ||
112 | const allowedResult = await Hooks.wrapFun( | |
113 | isVideoDownloadAllowed, | |
114 | allowParameters, | |
115 | 'filter:api.download.video.allowed.result' | |
116 | ) | |
117 | ||
118 | if (!checkAllowResult(res, allowParameters, allowedResult)) return | |
119 | ||
0305db28 JB |
120 | if (videoFile.storage === VideoStorage.OBJECT_STORAGE) { |
121 | return res.redirect(videoFile.getObjectStorageUrl()) | |
122 | } | |
123 | ||
ad5db104 | 124 | await VideoPathManager.Instance.makeAvailableVideoFile(videoFile.withVideoOrPlaylist(streamingPlaylist), path => { |
0305db28 JB |
125 | const filename = `${video.name}-${videoFile.resolution}p-${streamingPlaylist.getStringType()}${videoFile.extname}` |
126 | ||
127 | return res.download(path, filename) | |
128 | }) | |
90a8bd30 C |
129 | } |
130 | ||
131 | function getVideoFile (req: express.Request, files: MVideoFile[]) { | |
132 | const resolution = parseInt(req.params.resolution, 10) | |
133 | return files.find(f => f.resolution === resolution) | |
134 | } | |
135 | ||
136 | function getHLSPlaylist (video: MVideoFullLight) { | |
137 | const playlist = video.VideoStreamingPlaylists.find(p => p.type === VideoStreamingPlaylistType.HLS) | |
138 | if (!playlist) return undefined | |
139 | ||
140 | return Object.assign(playlist, { Video: video }) | |
141 | } | |
4bc45da3 C |
142 | |
143 | type AllowedResult = { | |
144 | allowed: boolean | |
145 | errorMessage?: string | |
146 | } | |
147 | ||
148 | function isTorrentDownloadAllowed (_object: { | |
149 | torrentPath: string | |
150 | }): AllowedResult { | |
151 | return { allowed: true } | |
152 | } | |
153 | ||
154 | function isVideoDownloadAllowed (_object: { | |
155 | video: MVideo | |
156 | videoFile: MVideoFile | |
157 | streamingPlaylist?: MStreamingPlaylist | |
158 | }): AllowedResult { | |
159 | return { allowed: true } | |
160 | } | |
161 | ||
162 | function checkAllowResult (res: express.Response, allowParameters: any, result?: AllowedResult) { | |
163 | if (!result || result.allowed !== true) { | |
164 | logger.info('Download is not allowed.', { result, allowParameters }) | |
4bc45da3 | 165 | |
76148b27 RK |
166 | res.fail({ |
167 | status: HttpStatusCode.FORBIDDEN_403, | |
168 | message: result?.errorMessage || 'Refused download' | |
169 | }) | |
4bc45da3 C |
170 | return false |
171 | } | |
172 | ||
173 | return true | |
174 | } |