]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/download.ts
More robust actor image lazy load
[github/Chocobozzz/PeerTube.git] / server / controllers / download.ts
CommitLineData
90a8bd30
C
1import * as cors from 'cors'
2import * as express from 'express'
4bc45da3 3import { logger } from '@server/helpers/logger'
90a8bd30 4import { VideosTorrentCache } from '@server/lib/files-cache/videos-torrent-cache'
4bc45da3 5import { Hooks } from '@server/lib/plugins/hooks'
90a8bd30 6import { getVideoFilePath } from '@server/lib/video-paths'
4bc45da3 7import { MStreamingPlaylist, MVideo, MVideoFile, MVideoFullLight } from '@server/types/models'
90a8bd30
C
8import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
9import { VideoStreamingPlaylistType } from '@shared/models'
10import { STATIC_DOWNLOAD_PATHS } from '../initializers/constants'
11import { asyncMiddleware, videosDownloadValidator } from '../middlewares'
12
13const downloadRouter = express.Router()
14
15downloadRouter.use(cors())
16
17downloadRouter.use(
18 STATIC_DOWNLOAD_PATHS.TORRENTS + ':filename',
4bc45da3 19 asyncMiddleware(downloadTorrent)
90a8bd30
C
20)
21
22downloadRouter.use(
23 STATIC_DOWNLOAD_PATHS.VIDEOS + ':id-:resolution([0-9]+).:extension',
24 asyncMiddleware(videosDownloadValidator),
4bc45da3 25 asyncMiddleware(downloadVideoFile)
90a8bd30
C
26)
27
28downloadRouter.use(
29 STATIC_DOWNLOAD_PATHS.HLS_VIDEOS + ':id-:resolution([0-9]+)-fragmented.:extension',
30 asyncMiddleware(videosDownloadValidator),
4bc45da3 31 asyncMiddleware(downloadHLSVideoFile)
90a8bd30
C
32)
33
34// ---------------------------------------------------------------------------
35
36export {
37 downloadRouter
38}
39
40// ---------------------------------------------------------------------------
41
42async function downloadTorrent (req: express.Request, res: express.Response) {
43 const result = await VideosTorrentCache.Instance.getFilePath(req.params.filename)
76148b27
RK
44 if (!result) {
45 return res.fail({
46 status: HttpStatusCode.NOT_FOUND_404,
47 message: 'Torrent file not found'
48 })
49 }
90a8bd30 50
4bc45da3
C
51 const allowParameters = { torrentPath: result.path, downloadName: result.downloadName }
52
53 const allowedResult = await Hooks.wrapFun(
54 isTorrentDownloadAllowed,
55 allowParameters,
56 'filter:api.download.torrent.allowed.result'
57 )
58
59 if (!checkAllowResult(res, allowParameters, allowedResult)) return
60
90a8bd30
C
61 return res.download(result.path, result.downloadName)
62}
63
4bc45da3 64async function downloadVideoFile (req: express.Request, res: express.Response) {
90a8bd30
C
65 const video = res.locals.videoAll
66
67 const videoFile = getVideoFile(req, video.VideoFiles)
76148b27
RK
68 if (!videoFile) {
69 return res.fail({
70 status: HttpStatusCode.NOT_FOUND_404,
71 message: 'Video file not found'
72 })
73 }
90a8bd30 74
4bc45da3
C
75 const allowParameters = { video, videoFile }
76
77 const allowedResult = await Hooks.wrapFun(
78 isVideoDownloadAllowed,
79 allowParameters,
80 'filter:api.download.video.allowed.result'
81 )
82
83 if (!checkAllowResult(res, allowParameters, allowedResult)) return
84
90a8bd30
C
85 return res.download(getVideoFilePath(video, videoFile), `${video.name}-${videoFile.resolution}p${videoFile.extname}`)
86}
87
4bc45da3 88async function downloadHLSVideoFile (req: express.Request, res: express.Response) {
90a8bd30 89 const video = res.locals.videoAll
4bc45da3
C
90 const streamingPlaylist = getHLSPlaylist(video)
91 if (!streamingPlaylist) return res.status(HttpStatusCode.NOT_FOUND_404).end
90a8bd30 92
4bc45da3 93 const videoFile = getVideoFile(req, streamingPlaylist.VideoFiles)
76148b27
RK
94 if (!videoFile) {
95 return res.fail({
96 status: HttpStatusCode.NOT_FOUND_404,
97 message: 'Video file not found'
98 })
99 }
90a8bd30 100
4bc45da3
C
101 const allowParameters = { video, streamingPlaylist, videoFile }
102
103 const allowedResult = await Hooks.wrapFun(
104 isVideoDownloadAllowed,
105 allowParameters,
106 'filter:api.download.video.allowed.result'
107 )
108
109 if (!checkAllowResult(res, allowParameters, allowedResult)) return
110
111 const filename = `${video.name}-${videoFile.resolution}p-${streamingPlaylist.getStringType()}${videoFile.extname}`
112 return res.download(getVideoFilePath(streamingPlaylist, videoFile), filename)
90a8bd30
C
113}
114
115function getVideoFile (req: express.Request, files: MVideoFile[]) {
116 const resolution = parseInt(req.params.resolution, 10)
117 return files.find(f => f.resolution === resolution)
118}
119
120function getHLSPlaylist (video: MVideoFullLight) {
121 const playlist = video.VideoStreamingPlaylists.find(p => p.type === VideoStreamingPlaylistType.HLS)
122 if (!playlist) return undefined
123
124 return Object.assign(playlist, { Video: video })
125}
4bc45da3
C
126
127type AllowedResult = {
128 allowed: boolean
129 errorMessage?: string
130}
131
132function isTorrentDownloadAllowed (_object: {
133 torrentPath: string
134}): AllowedResult {
135 return { allowed: true }
136}
137
138function isVideoDownloadAllowed (_object: {
139 video: MVideo
140 videoFile: MVideoFile
141 streamingPlaylist?: MStreamingPlaylist
142}): AllowedResult {
143 return { allowed: true }
144}
145
146function checkAllowResult (res: express.Response, allowParameters: any, result?: AllowedResult) {
147 if (!result || result.allowed !== true) {
148 logger.info('Download is not allowed.', { result, allowParameters })
4bc45da3 149
76148b27
RK
150 res.fail({
151 status: HttpStatusCode.FORBIDDEN_403,
152 message: result?.errorMessage || 'Refused download'
153 })
4bc45da3
C
154 return false
155 }
156
157 return true
158}