diff options
Diffstat (limited to 'server')
-rw-r--r-- | server/controllers/download.ts | 39 | ||||
-rw-r--r-- | server/lib/object-storage/index.ts | 1 |
2 files changed, 22 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' | |||
2 | import express from 'express' | 2 | import express from 'express' |
3 | import { logger } from '@server/helpers/logger' | 3 | import { logger } from '@server/helpers/logger' |
4 | import { VideosTorrentCache } from '@server/lib/files-cache/videos-torrent-cache' | 4 | import { VideosTorrentCache } from '@server/lib/files-cache/videos-torrent-cache' |
5 | import { generateHLSFilePresignedUrl, generateWebVideoPresignedUrl } from '@server/lib/object-storage' | ||
5 | import { Hooks } from '@server/lib/plugins/hooks' | 6 | import { Hooks } from '@server/lib/plugins/hooks' |
6 | import { VideoPathManager } from '@server/lib/video-path-manager' | 7 | import { VideoPathManager } from '@server/lib/video-path-manager' |
7 | import { MStreamingPlaylist, MVideo, MVideoFile, MVideoFullLight } from '@server/types/models' | 8 | import { MStreamingPlaylist, MStreamingPlaylistVideo, MVideo, MVideoFile, MVideoFullLight } from '@server/types/models' |
8 | import { addQueryParams, forceNumber } from '@shared/core-utils' | 9 | import { forceNumber } from '@shared/core-utils' |
9 | import { HttpStatusCode, VideoStorage, VideoStreamingPlaylistType } from '@shared/models' | 10 | import { HttpStatusCode, VideoStorage, VideoStreamingPlaylistType } from '@shared/models' |
10 | import { STATIC_DOWNLOAD_PATHS } from '../initializers/constants' | 11 | import { STATIC_DOWNLOAD_PATHS } from '../initializers/constants' |
11 | import { asyncMiddleware, optionalAuthenticate, videosDownloadValidator } from '../middlewares' | 12 | import { 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 | ||
195 | function redirectToObjectStorage (options: { | 196 | async 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 | } |
diff --git a/server/lib/object-storage/index.ts b/server/lib/object-storage/index.ts index 6525f8dfb..3ad6cab63 100644 --- a/server/lib/object-storage/index.ts +++ b/server/lib/object-storage/index.ts | |||
@@ -1,4 +1,5 @@ | |||
1 | export * from './keys' | 1 | export * from './keys' |
2 | export * from './proxy' | 2 | export * from './proxy' |
3 | export * from './pre-signed-urls' | ||
3 | export * from './urls' | 4 | export * from './urls' |
4 | export * from './videos' | 5 | export * from './videos' |