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 { HttpStatusCode, VideoStorage, VideoStreamingPlaylistType } from '@shared/models'
9 import { STATIC_DOWNLOAD_PATHS } from '../initializers/constants'
10 import { asyncMiddleware, videosDownloadValidator } from '../middlewares'
12 const downloadRouter = express.Router()
14 downloadRouter.use(cors())
17 STATIC_DOWNLOAD_PATHS.TORRENTS + ':filename',
18 asyncMiddleware(downloadTorrent)
22 STATIC_DOWNLOAD_PATHS.VIDEOS + ':id-:resolution([0-9]+).:extension',
23 asyncMiddleware(videosDownloadValidator),
24 asyncMiddleware(downloadVideoFile)
28 STATIC_DOWNLOAD_PATHS.HLS_VIDEOS + ':id-:resolution([0-9]+)-fragmented.:extension',
29 asyncMiddleware(videosDownloadValidator),
30 asyncMiddleware(downloadHLSVideoFile)
33 // ---------------------------------------------------------------------------
39 // ---------------------------------------------------------------------------
41 async function downloadTorrent (req: express.Request, res: express.Response) {
42 const result = await VideosTorrentCache.Instance.getFilePath(req.params.filename)
45 status: HttpStatusCode.NOT_FOUND_404,
46 message: 'Torrent file not found'
50 const allowParameters = { torrentPath: result.path, downloadName: result.downloadName }
52 const allowedResult = await Hooks.wrapFun(
53 isTorrentDownloadAllowed,
55 'filter:api.download.torrent.allowed.result'
58 if (!checkAllowResult(res, allowParameters, allowedResult)) return
60 return res.download(result.path, result.downloadName)
63 async function downloadVideoFile (req: express.Request, res: express.Response) {
64 const video = res.locals.videoAll
66 const videoFile = getVideoFile(req, video.VideoFiles)
69 status: HttpStatusCode.NOT_FOUND_404,
70 message: 'Video file not found'
74 const allowParameters = { video, videoFile }
76 const allowedResult = await Hooks.wrapFun(
77 isVideoDownloadAllowed,
79 'filter:api.download.video.allowed.result'
82 if (!checkAllowResult(res, allowParameters, allowedResult)) return
84 if (videoFile.storage === VideoStorage.OBJECT_STORAGE) {
85 return res.redirect(videoFile.getObjectStorageUrl())
88 await VideoPathManager.Instance.makeAvailableVideoFile(video, videoFile, path => {
89 const filename = `${video.name}-${videoFile.resolution}p${videoFile.extname}`
91 return res.download(path, filename)
95 async function downloadHLSVideoFile (req: express.Request, res: express.Response) {
96 const video = res.locals.videoAll
97 const streamingPlaylist = getHLSPlaylist(video)
98 if (!streamingPlaylist) return res.status(HttpStatusCode.NOT_FOUND_404).end
100 const videoFile = getVideoFile(req, streamingPlaylist.VideoFiles)
103 status: HttpStatusCode.NOT_FOUND_404,
104 message: 'Video file not found'
108 const allowParameters = { video, streamingPlaylist, videoFile }
110 const allowedResult = await Hooks.wrapFun(
111 isVideoDownloadAllowed,
113 'filter:api.download.video.allowed.result'
116 if (!checkAllowResult(res, allowParameters, allowedResult)) return
118 if (videoFile.storage === VideoStorage.OBJECT_STORAGE) {
119 return res.redirect(videoFile.getObjectStorageUrl())
122 await VideoPathManager.Instance.makeAvailableVideoFile(streamingPlaylist, videoFile, path => {
123 const filename = `${video.name}-${videoFile.resolution}p-${streamingPlaylist.getStringType()}${videoFile.extname}`
125 return res.download(path, filename)
129 function getVideoFile (req: express.Request, files: MVideoFile[]) {
130 const resolution = parseInt(req.params.resolution, 10)
131 return files.find(f => f.resolution === resolution)
134 function getHLSPlaylist (video: MVideoFullLight) {
135 const playlist = video.VideoStreamingPlaylists.find(p => p.type === VideoStreamingPlaylistType.HLS)
136 if (!playlist) return undefined
138 return Object.assign(playlist, { Video: video })
141 type AllowedResult = {
143 errorMessage?: string
146 function isTorrentDownloadAllowed (_object: {
149 return { allowed: true }
152 function isVideoDownloadAllowed (_object: {
154 videoFile: MVideoFile
155 streamingPlaylist?: MStreamingPlaylist
157 return { allowed: true }
160 function checkAllowResult (res: express.Response, allowParameters: any, result?: AllowedResult) {
161 if (!result || result.allowed !== true) {
162 logger.info('Download is not allowed.', { result, allowParameters })
165 status: HttpStatusCode.FORBIDDEN_403,
166 message: result?.errorMessage || 'Refused download'