X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fcontrollers%2Fdownload.ts;h=65b9a1d1b740bb1199ee3d5e5ce909e59db3416b;hb=77239b425a8e00822a53c9907415832a473c3eb6;hp=a270180c0228e6b3250779e1705160e1b296be4d;hpb=e771ff815dba3b4a95633f4e1e10dacd222dfe61;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/controllers/download.ts b/server/controllers/download.ts index a270180c0..65b9a1d1b 100644 --- a/server/controllers/download.ts +++ b/server/controllers/download.ts @@ -5,9 +5,10 @@ import { VideosTorrentCache } from '@server/lib/files-cache/videos-torrent-cache import { Hooks } from '@server/lib/plugins/hooks' import { VideoPathManager } from '@server/lib/video-path-manager' import { MStreamingPlaylist, MVideo, MVideoFile, MVideoFullLight } from '@server/types/models' +import { addQueryParams, forceNumber } from '@shared/core-utils' import { HttpStatusCode, VideoStorage, VideoStreamingPlaylistType } from '@shared/models' import { STATIC_DOWNLOAD_PATHS } from '../initializers/constants' -import { asyncMiddleware, videosDownloadValidator } from '../middlewares' +import { asyncMiddleware, optionalAuthenticate, videosDownloadValidator } from '../middlewares' const downloadRouter = express.Router() @@ -20,12 +21,14 @@ downloadRouter.use( downloadRouter.use( STATIC_DOWNLOAD_PATHS.VIDEOS + ':id-:resolution([0-9]+).:extension', + optionalAuthenticate, asyncMiddleware(videosDownloadValidator), asyncMiddleware(downloadVideoFile) ) downloadRouter.use( STATIC_DOWNLOAD_PATHS.HLS_VIDEOS + ':id-:resolution([0-9]+)-fragmented.:extension', + optionalAuthenticate, asyncMiddleware(videosDownloadValidator), asyncMiddleware(downloadHLSVideoFile) ) @@ -82,7 +85,7 @@ async function downloadVideoFile (req: express.Request, res: express.Response) { if (!checkAllowResult(res, allowParameters, allowedResult)) return if (videoFile.storage === VideoStorage.OBJECT_STORAGE) { - return res.redirect(videoFile.getObjectStorageUrl()) + return redirectToObjectStorage({ req, res, video, file: videoFile }) } await VideoPathManager.Instance.makeAvailableVideoFile(videoFile.withVideoOrPlaylist(video), path => { @@ -118,7 +121,7 @@ async function downloadHLSVideoFile (req: express.Request, res: express.Response if (!checkAllowResult(res, allowParameters, allowedResult)) return if (videoFile.storage === VideoStorage.OBJECT_STORAGE) { - return res.redirect(videoFile.getObjectStorageUrl()) + return redirectToObjectStorage({ req, res, video, file: videoFile }) } await VideoPathManager.Instance.makeAvailableVideoFile(videoFile.withVideoOrPlaylist(streamingPlaylist), path => { @@ -129,7 +132,7 @@ async function downloadHLSVideoFile (req: express.Request, res: express.Response } function getVideoFile (req: express.Request, files: MVideoFile[]) { - const resolution = parseInt(req.params.resolution, 10) + const resolution = forceNumber(req.params.resolution) return files.find(f => f.resolution === resolution) } @@ -172,3 +175,20 @@ function checkAllowResult (res: express.Response, allowParameters: any, result?: return true } + +function redirectToObjectStorage (options: { + req: express.Request + res: express.Response + video: MVideo + file: MVideoFile +}) { + const { req, res, video, file } = options + + const baseUrl = file.getObjectStorageUrl(video) + + const url = video.hasPrivateStaticPath() && req.query.videoFileToken + ? addQueryParams(baseUrl, { videoFileToken: req.query.videoFileToken }) + : baseUrl + + return res.redirect(url) +}