]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/download.ts
abd1df26fe14216bd827b0622896e9dc002b5866
[github/Chocobozzz/PeerTube.git] / server / controllers / download.ts
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 { HttpStatusCode, VideoStorage, VideoStreamingPlaylistType } from '@shared/models'
9 import { STATIC_DOWNLOAD_PATHS } from '../initializers/constants'
10 import { asyncMiddleware, optionalAuthenticate, videosDownloadValidator } from '../middlewares'
11
12 const downloadRouter = express.Router()
13
14 downloadRouter.use(cors())
15
16 downloadRouter.use(
17 STATIC_DOWNLOAD_PATHS.TORRENTS + ':filename',
18 asyncMiddleware(downloadTorrent)
19 )
20
21 downloadRouter.use(
22 STATIC_DOWNLOAD_PATHS.VIDEOS + ':id-:resolution([0-9]+).:extension',
23 optionalAuthenticate,
24 asyncMiddleware(videosDownloadValidator),
25 asyncMiddleware(downloadVideoFile)
26 )
27
28 downloadRouter.use(
29 STATIC_DOWNLOAD_PATHS.HLS_VIDEOS + ':id-:resolution([0-9]+)-fragmented.:extension',
30 optionalAuthenticate,
31 asyncMiddleware(videosDownloadValidator),
32 asyncMiddleware(downloadHLSVideoFile)
33 )
34
35 // ---------------------------------------------------------------------------
36
37 export {
38 downloadRouter
39 }
40
41 // ---------------------------------------------------------------------------
42
43 async function downloadTorrent (req: express.Request, res: express.Response) {
44 const result = await VideosTorrentCache.Instance.getFilePath(req.params.filename)
45 if (!result) {
46 return res.fail({
47 status: HttpStatusCode.NOT_FOUND_404,
48 message: 'Torrent file not found'
49 })
50 }
51
52 const allowParameters = { torrentPath: result.path, downloadName: result.downloadName }
53
54 const allowedResult = await Hooks.wrapFun(
55 isTorrentDownloadAllowed,
56 allowParameters,
57 'filter:api.download.torrent.allowed.result'
58 )
59
60 if (!checkAllowResult(res, allowParameters, allowedResult)) return
61
62 return res.download(result.path, result.downloadName)
63 }
64
65 async function downloadVideoFile (req: express.Request, res: express.Response) {
66 const video = res.locals.videoAll
67
68 const videoFile = getVideoFile(req, video.VideoFiles)
69 if (!videoFile) {
70 return res.fail({
71 status: HttpStatusCode.NOT_FOUND_404,
72 message: 'Video file not found'
73 })
74 }
75
76 const allowParameters = { video, videoFile }
77
78 const allowedResult = await Hooks.wrapFun(
79 isVideoDownloadAllowed,
80 allowParameters,
81 'filter:api.download.video.allowed.result'
82 )
83
84 if (!checkAllowResult(res, allowParameters, allowedResult)) return
85
86 if (videoFile.storage === VideoStorage.OBJECT_STORAGE) {
87 return res.redirect(videoFile.getObjectStorageUrl())
88 }
89
90 await VideoPathManager.Instance.makeAvailableVideoFile(videoFile.withVideoOrPlaylist(video), path => {
91 // Express uses basename on filename parameter
92 const videoName = video.name.replace(/[/\\]/g, '_')
93 const filename = `${videoName}-${videoFile.resolution}p${videoFile.extname}`
94
95 return res.download(path, filename)
96 })
97 }
98
99 async function downloadHLSVideoFile (req: express.Request, res: express.Response) {
100 const video = res.locals.videoAll
101 const streamingPlaylist = getHLSPlaylist(video)
102 if (!streamingPlaylist) return res.status(HttpStatusCode.NOT_FOUND_404).end
103
104 const videoFile = getVideoFile(req, streamingPlaylist.VideoFiles)
105 if (!videoFile) {
106 return res.fail({
107 status: HttpStatusCode.NOT_FOUND_404,
108 message: 'Video file not found'
109 })
110 }
111
112 const allowParameters = { video, streamingPlaylist, videoFile }
113
114 const allowedResult = await Hooks.wrapFun(
115 isVideoDownloadAllowed,
116 allowParameters,
117 'filter:api.download.video.allowed.result'
118 )
119
120 if (!checkAllowResult(res, allowParameters, allowedResult)) return
121
122 if (videoFile.storage === VideoStorage.OBJECT_STORAGE) {
123 return res.redirect(videoFile.getObjectStorageUrl())
124 }
125
126 await VideoPathManager.Instance.makeAvailableVideoFile(videoFile.withVideoOrPlaylist(streamingPlaylist), path => {
127 const filename = `${video.name}-${videoFile.resolution}p-${streamingPlaylist.getStringType()}${videoFile.extname}`
128
129 return res.download(path, filename)
130 })
131 }
132
133 function getVideoFile (req: express.Request, files: MVideoFile[]) {
134 const resolution = parseInt(req.params.resolution, 10)
135 return files.find(f => f.resolution === resolution)
136 }
137
138 function getHLSPlaylist (video: MVideoFullLight) {
139 const playlist = video.VideoStreamingPlaylists.find(p => p.type === VideoStreamingPlaylistType.HLS)
140 if (!playlist) return undefined
141
142 return Object.assign(playlist, { Video: video })
143 }
144
145 type AllowedResult = {
146 allowed: boolean
147 errorMessage?: string
148 }
149
150 function isTorrentDownloadAllowed (_object: {
151 torrentPath: string
152 }): AllowedResult {
153 return { allowed: true }
154 }
155
156 function isVideoDownloadAllowed (_object: {
157 video: MVideo
158 videoFile: MVideoFile
159 streamingPlaylist?: MStreamingPlaylist
160 }): AllowedResult {
161 return { allowed: true }
162 }
163
164 function checkAllowResult (res: express.Response, allowParameters: any, result?: AllowedResult) {
165 if (!result || result.allowed !== true) {
166 logger.info('Download is not allowed.', { result, allowParameters })
167
168 res.fail({
169 status: HttpStatusCode.FORBIDDEN_403,
170 message: result?.errorMessage || 'Refused download'
171 })
172 return false
173 }
174
175 return true
176 }