From 90a8bd305de4153ec21137a73ff482dcc2e3e19b Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 16 Feb 2021 16:25:53 +0100 Subject: Dissociate video file names and video uuid --- server/controllers/download.ts | 78 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 server/controllers/download.ts (limited to 'server/controllers/download.ts') diff --git a/server/controllers/download.ts b/server/controllers/download.ts new file mode 100644 index 000000000..27caa1518 --- /dev/null +++ b/server/controllers/download.ts @@ -0,0 +1,78 @@ +import * as cors from 'cors' +import * as express from 'express' +import { VideosTorrentCache } from '@server/lib/files-cache/videos-torrent-cache' +import { getVideoFilePath } from '@server/lib/video-paths' +import { MVideoFile, MVideoFullLight } from '@server/types/models' +import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes' +import { VideoStreamingPlaylistType } from '@shared/models' +import { STATIC_DOWNLOAD_PATHS } from '../initializers/constants' +import { asyncMiddleware, videosDownloadValidator } from '../middlewares' + +const downloadRouter = express.Router() + +downloadRouter.use(cors()) + +downloadRouter.use( + STATIC_DOWNLOAD_PATHS.TORRENTS + ':filename', + downloadTorrent +) + +downloadRouter.use( + STATIC_DOWNLOAD_PATHS.VIDEOS + ':id-:resolution([0-9]+).:extension', + asyncMiddleware(videosDownloadValidator), + downloadVideoFile +) + +downloadRouter.use( + STATIC_DOWNLOAD_PATHS.HLS_VIDEOS + ':id-:resolution([0-9]+)-fragmented.:extension', + asyncMiddleware(videosDownloadValidator), + downloadHLSVideoFile +) + +// --------------------------------------------------------------------------- + +export { + downloadRouter +} + +// --------------------------------------------------------------------------- + +async function downloadTorrent (req: express.Request, res: express.Response) { + const result = await VideosTorrentCache.Instance.getFilePath(req.params.filename) + if (!result) return res.sendStatus(HttpStatusCode.NOT_FOUND_404) + + return res.download(result.path, result.downloadName) +} + +function downloadVideoFile (req: express.Request, res: express.Response) { + const video = res.locals.videoAll + + const videoFile = getVideoFile(req, video.VideoFiles) + if (!videoFile) return res.status(HttpStatusCode.NOT_FOUND_404).end() + + return res.download(getVideoFilePath(video, videoFile), `${video.name}-${videoFile.resolution}p${videoFile.extname}`) +} + +function downloadHLSVideoFile (req: express.Request, res: express.Response) { + const video = res.locals.videoAll + const playlist = getHLSPlaylist(video) + if (!playlist) return res.status(HttpStatusCode.NOT_FOUND_404).end + + const videoFile = getVideoFile(req, playlist.VideoFiles) + if (!videoFile) return res.status(HttpStatusCode.NOT_FOUND_404).end() + + const filename = `${video.name}-${videoFile.resolution}p-${playlist.getStringType()}${videoFile.extname}` + return res.download(getVideoFilePath(playlist, videoFile), filename) +} + +function getVideoFile (req: express.Request, files: MVideoFile[]) { + const resolution = parseInt(req.params.resolution, 10) + return files.find(f => f.resolution === resolution) +} + +function getHLSPlaylist (video: MVideoFullLight) { + const playlist = video.VideoStreamingPlaylists.find(p => p.type === VideoStreamingPlaylistType.HLS) + if (!playlist) return undefined + + return Object.assign(playlist, { Video: video }) +} -- cgit v1.2.3