aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers
diff options
context:
space:
mode:
Diffstat (limited to 'server/controllers')
-rw-r--r--server/controllers/download.ts39
1 files changed, 21 insertions, 18 deletions
diff --git a/server/controllers/download.ts b/server/controllers/download.ts
index d675a2d6c..4c3ab0163 100644
--- a/server/controllers/download.ts
+++ b/server/controllers/download.ts
@@ -2,10 +2,11 @@ import cors from 'cors'
2import express from 'express' 2import express from 'express'
3import { logger } from '@server/helpers/logger' 3import { logger } from '@server/helpers/logger'
4import { VideosTorrentCache } from '@server/lib/files-cache/videos-torrent-cache' 4import { VideosTorrentCache } from '@server/lib/files-cache/videos-torrent-cache'
5import { generateHLSFilePresignedUrl, generateWebVideoPresignedUrl } from '@server/lib/object-storage'
5import { Hooks } from '@server/lib/plugins/hooks' 6import { Hooks } from '@server/lib/plugins/hooks'
6import { VideoPathManager } from '@server/lib/video-path-manager' 7import { VideoPathManager } from '@server/lib/video-path-manager'
7import { MStreamingPlaylist, MVideo, MVideoFile, MVideoFullLight } from '@server/types/models' 8import { MStreamingPlaylist, MStreamingPlaylistVideo, MVideo, MVideoFile, MVideoFullLight } from '@server/types/models'
8import { addQueryParams, forceNumber } from '@shared/core-utils' 9import { forceNumber } from '@shared/core-utils'
9import { HttpStatusCode, VideoStorage, VideoStreamingPlaylistType } from '@shared/models' 10import { HttpStatusCode, VideoStorage, VideoStreamingPlaylistType } from '@shared/models'
10import { STATIC_DOWNLOAD_PATHS } from '../initializers/constants' 11import { STATIC_DOWNLOAD_PATHS } from '../initializers/constants'
11import { asyncMiddleware, optionalAuthenticate, videosDownloadValidator } from '../middlewares' 12import { asyncMiddleware, optionalAuthenticate, videosDownloadValidator } from '../middlewares'
@@ -94,16 +95,16 @@ async function downloadVideoFile (req: express.Request, res: express.Response) {
94 95
95 if (!checkAllowResult(res, allowParameters, allowedResult)) return 96 if (!checkAllowResult(res, allowParameters, allowedResult)) return
96 97
98 // Express uses basename on filename parameter
99 const videoName = video.name.replace(/[/\\]/g, '_')
100 const downloadFilename = `${videoName}-${videoFile.resolution}p${videoFile.extname}`
101
97 if (videoFile.storage === VideoStorage.OBJECT_STORAGE) { 102 if (videoFile.storage === VideoStorage.OBJECT_STORAGE) {
98 return redirectToObjectStorage({ req, res, video, file: videoFile }) 103 return redirectToObjectStorage({ req, res, video, file: videoFile, downloadFilename })
99 } 104 }
100 105
101 await VideoPathManager.Instance.makeAvailableVideoFile(videoFile.withVideoOrPlaylist(video), path => { 106 await VideoPathManager.Instance.makeAvailableVideoFile(videoFile.withVideoOrPlaylist(video), path => {
102 // Express uses basename on filename parameter 107 return res.download(path, downloadFilename)
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 })
108} 109}
109 110
@@ -136,14 +137,14 @@ async function downloadHLSVideoFile (req: express.Request, res: express.Response
136 137
137 if (!checkAllowResult(res, allowParameters, allowedResult)) return 138 if (!checkAllowResult(res, allowParameters, allowedResult)) return
138 139
140 const downloadFilename = `${video.name}-${videoFile.resolution}p-${streamingPlaylist.getStringType()}${videoFile.extname}`
141
139 if (videoFile.storage === VideoStorage.OBJECT_STORAGE) { 142 if (videoFile.storage === VideoStorage.OBJECT_STORAGE) {
140 return redirectToObjectStorage({ req, res, video, file: videoFile }) 143 return redirectToObjectStorage({ req, res, video, streamingPlaylist, file: videoFile, downloadFilename })
141 } 144 }
142 145
143 await VideoPathManager.Instance.makeAvailableVideoFile(videoFile.withVideoOrPlaylist(streamingPlaylist), path => { 146 await VideoPathManager.Instance.makeAvailableVideoFile(videoFile.withVideoOrPlaylist(streamingPlaylist), path => {
144 const filename = `${video.name}-${videoFile.resolution}p-${streamingPlaylist.getStringType()}${videoFile.extname}` 147 return res.download(path, downloadFilename)
145
146 return res.download(path, filename)
147 }) 148 })
148} 149}
149 150
@@ -192,19 +193,21 @@ function checkAllowResult (res: express.Response, allowParameters: any, result?:
192 return true 193 return true
193} 194}
194 195
195function redirectToObjectStorage (options: { 196async function redirectToObjectStorage (options: {
196 req: express.Request 197 req: express.Request
197 res: express.Response 198 res: express.Response
198 video: MVideo 199 video: MVideo
199 file: MVideoFile 200 file: MVideoFile
201 streamingPlaylist?: MStreamingPlaylistVideo
202 downloadFilename: string
200}) { 203}) {
201 const { req, res, video, file } = options 204 const { res, video, streamingPlaylist, file, downloadFilename } = options
202 205
203 const baseUrl = file.getObjectStorageUrl(video) 206 const url = streamingPlaylist
207 ? await generateHLSFilePresignedUrl({ streamingPlaylist, file, downloadFilename })
208 : await generateWebVideoPresignedUrl({ file, downloadFilename })
204 209
205 const url = video.hasPrivateStaticPath() && req.query.videoFileToken 210 logger.debug('Generating pre-signed URL %s for video %s', url, video.uuid)
206 ? addQueryParams(baseUrl, { videoFileToken: req.query.videoFileToken })
207 : baseUrl
208 211
209 return res.redirect(url) 212 return res.redirect(url)
210} 213}