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 { addQueryParams, forceNumber } from '@shared/core-utils'
9 import { HttpStatusCode, VideoStorage, VideoStreamingPlaylistType } from '@shared/models'
10 import { STATIC_DOWNLOAD_PATHS } from '../initializers/constants'
11 import { asyncMiddleware, optionalAuthenticate, videosDownloadValidator } from '../middlewares'
13 const downloadRouter = express.Router()
15 downloadRouter.use(cors())
18 STATIC_DOWNLOAD_PATHS.TORRENTS + ':filename',
19 asyncMiddleware(downloadTorrent)
23 STATIC_DOWNLOAD_PATHS.VIDEOS + ':id-:resolution([0-9]+).:extension',
25 asyncMiddleware(videosDownloadValidator),
26 asyncMiddleware(downloadVideoFile)
30 STATIC_DOWNLOAD_PATHS.HLS_VIDEOS + ':id-:resolution([0-9]+)-fragmented.:extension',
32 asyncMiddleware(videosDownloadValidator),
33 asyncMiddleware(downloadHLSVideoFile)
36 // ---------------------------------------------------------------------------
42 // ---------------------------------------------------------------------------
44 async function downloadTorrent (req: express.Request, res: express.Response) {
45 const result = await VideosTorrentCache.Instance.getFilePath(req.params.filename)
48 status: HttpStatusCode.NOT_FOUND_404,
49 message: 'Torrent file not found'
53 const allowParameters = { torrentPath: result.path, downloadName: result.downloadName }
55 const allowedResult = await Hooks.wrapFun(
56 isTorrentDownloadAllowed,
58 'filter:api.download.torrent.allowed.result'
61 if (!checkAllowResult(res, allowParameters, allowedResult)) return
63 return res.download(result.path, result.downloadName)
66 async function downloadVideoFile (req: express.Request, res: express.Response) {
67 const video = res.locals.videoAll
69 const videoFile = getVideoFile(req, video.VideoFiles)
72 status: HttpStatusCode.NOT_FOUND_404,
73 message: 'Video file not found'
77 const allowParameters = { video, videoFile }
79 const allowedResult = await Hooks.wrapFun(
80 isVideoDownloadAllowed,
82 'filter:api.download.video.allowed.result'
85 if (!checkAllowResult(res, allowParameters, allowedResult)) return
87 if (videoFile.storage === VideoStorage.OBJECT_STORAGE) {
88 return redirectToObjectStorage({ req, res, video, file: videoFile })
91 await VideoPathManager.Instance.makeAvailableVideoFile(videoFile.withVideoOrPlaylist(video), path => {
92 // Express uses basename on filename parameter
93 const videoName = video.name.replace(/[/\\]/g, '_')
94 const filename = `${videoName}-${videoFile.resolution}p${videoFile.extname}`
96 return res.download(path, filename)
100 async function downloadHLSVideoFile (req: express.Request, res: express.Response) {
101 const video = res.locals.videoAll
102 const streamingPlaylist = getHLSPlaylist(video)
103 if (!streamingPlaylist) return res.status(HttpStatusCode.NOT_FOUND_404).end
105 const videoFile = getVideoFile(req, streamingPlaylist.VideoFiles)
108 status: HttpStatusCode.NOT_FOUND_404,
109 message: 'Video file not found'
113 const allowParameters = { video, streamingPlaylist, videoFile }
115 const allowedResult = await Hooks.wrapFun(
116 isVideoDownloadAllowed,
118 'filter:api.download.video.allowed.result'
121 if (!checkAllowResult(res, allowParameters, allowedResult)) return
123 if (videoFile.storage === VideoStorage.OBJECT_STORAGE) {
124 return redirectToObjectStorage({ req, res, video, file: videoFile })
127 await VideoPathManager.Instance.makeAvailableVideoFile(videoFile.withVideoOrPlaylist(streamingPlaylist), path => {
128 const filename = `${video.name}-${videoFile.resolution}p-${streamingPlaylist.getStringType()}${videoFile.extname}`
130 return res.download(path, filename)
134 function getVideoFile (req: express.Request, files: MVideoFile[]) {
135 const resolution = forceNumber(req.params.resolution)
136 return files.find(f => f.resolution === resolution)
139 function getHLSPlaylist (video: MVideoFullLight) {
140 const playlist = video.VideoStreamingPlaylists.find(p => p.type === VideoStreamingPlaylistType.HLS)
141 if (!playlist) return undefined
143 return Object.assign(playlist, { Video: video })
146 type AllowedResult = {
148 errorMessage?: string
151 function isTorrentDownloadAllowed (_object: {
154 return { allowed: true }
157 function isVideoDownloadAllowed (_object: {
159 videoFile: MVideoFile
160 streamingPlaylist?: MStreamingPlaylist
162 return { allowed: true }
165 function checkAllowResult (res: express.Response, allowParameters: any, result?: AllowedResult) {
166 if (!result || result.allowed !== true) {
167 logger.info('Download is not allowed.', { result, allowParameters })
170 status: HttpStatusCode.FORBIDDEN_403,
171 message: result?.errorMessage || 'Refused download'
179 function redirectToObjectStorage (options: {
181 res: express.Response
185 const { req, res, video, file } = options
187 const baseUrl = file.getObjectStorageUrl(video)
189 const url = video.hasPrivateStaticPath() && req.query.videoFileToken
190 ? addQueryParams(baseUrl, { videoFileToken: req.query.videoFileToken })
193 return res.redirect(url)