]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/controllers/download.ts
Stricter models typing
[github/Chocobozzz/PeerTube.git] / server / controllers / download.ts
... / ...
CommitLineData
1import * as cors from 'cors'
2import * as express from 'express'
3import { logger } from '@server/helpers/logger'
4import { VideosTorrentCache } from '@server/lib/files-cache/videos-torrent-cache'
5import { Hooks } from '@server/lib/plugins/hooks'
6import { getVideoFilePath } from '@server/lib/video-paths'
7import { MStreamingPlaylist, MVideo, MVideoFile, MVideoFullLight } from '@server/types/models'
8import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
9import { VideoStreamingPlaylistType } from '@shared/models'
10import { STATIC_DOWNLOAD_PATHS } from '../initializers/constants'
11import { asyncMiddleware, videosDownloadValidator } from '../middlewares'
12
13const downloadRouter = express.Router()
14
15downloadRouter.use(cors())
16
17downloadRouter.use(
18 STATIC_DOWNLOAD_PATHS.TORRENTS + ':filename',
19 asyncMiddleware(downloadTorrent)
20)
21
22downloadRouter.use(
23 STATIC_DOWNLOAD_PATHS.VIDEOS + ':id-:resolution([0-9]+).:extension',
24 asyncMiddleware(videosDownloadValidator),
25 asyncMiddleware(downloadVideoFile)
26)
27
28downloadRouter.use(
29 STATIC_DOWNLOAD_PATHS.HLS_VIDEOS + ':id-:resolution([0-9]+)-fragmented.:extension',
30 asyncMiddleware(videosDownloadValidator),
31 asyncMiddleware(downloadHLSVideoFile)
32)
33
34// ---------------------------------------------------------------------------
35
36export {
37 downloadRouter
38}
39
40// ---------------------------------------------------------------------------
41
42async 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
59async 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
78async 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
100function getVideoFile (req: express.Request, files: MVideoFile[]) {
101 const resolution = parseInt(req.params.resolution, 10)
102 return files.find(f => f.resolution === resolution)
103}
104
105function 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
112type AllowedResult = {
113 allowed: boolean
114 errorMessage?: string
115}
116
117function isTorrentDownloadAllowed (_object: {
118 torrentPath: string
119}): AllowedResult {
120 return { allowed: true }
121}
122
123function isVideoDownloadAllowed (_object: {
124 video: MVideo
125 videoFile: MVideoFile
126 streamingPlaylist?: MStreamingPlaylist
127}): AllowedResult {
128 return { allowed: true }
129}
130
131function 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}