]>
Commit | Line | Data |
---|---|---|
90a8bd30 C |
1 | import * as cors from 'cors' |
2 | import * as 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' |
90a8bd30 | 6 | import { getVideoFilePath } from '@server/lib/video-paths' |
4bc45da3 | 7 | import { MStreamingPlaylist, MVideo, MVideoFile, MVideoFullLight } from '@server/types/models' |
90a8bd30 C |
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', | |
4bc45da3 | 19 | asyncMiddleware(downloadTorrent) |
90a8bd30 C |
20 | ) |
21 | ||
22 | downloadRouter.use( | |
23 | STATIC_DOWNLOAD_PATHS.VIDEOS + ':id-:resolution([0-9]+).:extension', | |
24 | asyncMiddleware(videosDownloadValidator), | |
4bc45da3 | 25 | asyncMiddleware(downloadVideoFile) |
90a8bd30 C |
26 | ) |
27 | ||
28 | downloadRouter.use( | |
29 | STATIC_DOWNLOAD_PATHS.HLS_VIDEOS + ':id-:resolution([0-9]+)-fragmented.:extension', | |
30 | asyncMiddleware(videosDownloadValidator), | |
4bc45da3 | 31 | asyncMiddleware(downloadHLSVideoFile) |
90a8bd30 C |
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 | ||
4bc45da3 C |
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 | ||
90a8bd30 C |
56 | return res.download(result.path, result.downloadName) |
57 | } | |
58 | ||
4bc45da3 | 59 | async function downloadVideoFile (req: express.Request, res: express.Response) { |
90a8bd30 C |
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 | ||
4bc45da3 C |
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 | ||
90a8bd30 C |
75 | return res.download(getVideoFilePath(video, videoFile), `${video.name}-${videoFile.resolution}p${videoFile.extname}`) |
76 | } | |
77 | ||
4bc45da3 | 78 | async function downloadHLSVideoFile (req: express.Request, res: express.Response) { |
90a8bd30 | 79 | const video = res.locals.videoAll |
4bc45da3 C |
80 | const streamingPlaylist = getHLSPlaylist(video) |
81 | if (!streamingPlaylist) return res.status(HttpStatusCode.NOT_FOUND_404).end | |
90a8bd30 | 82 | |
4bc45da3 | 83 | const videoFile = getVideoFile(req, streamingPlaylist.VideoFiles) |
90a8bd30 C |
84 | if (!videoFile) return res.status(HttpStatusCode.NOT_FOUND_404).end() |
85 | ||
4bc45da3 C |
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) | |
90a8bd30 C |
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 | } | |
4bc45da3 C |
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) | |
eebd9838 | 135 | .json({ error: result?.errorMessage || 'Refused download' }) |
4bc45da3 C |
136 | |
137 | return false | |
138 | } | |
139 | ||
140 | return true | |
141 | } |