aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/download.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-02-16 16:25:53 +0100
committerChocobozzz <chocobozzz@cpy.re>2021-02-18 13:38:09 +0100
commit90a8bd305de4153ec21137a73ff482dcc2e3e19b (patch)
tree2e35b5504ec11bc51579c92a70c77ed3d5ace816 /server/controllers/download.ts
parent684cdacbbd775b5f404dd7b373e02dd21baf5ff0 (diff)
downloadPeerTube-90a8bd305de4153ec21137a73ff482dcc2e3e19b.tar.gz
PeerTube-90a8bd305de4153ec21137a73ff482dcc2e3e19b.tar.zst
PeerTube-90a8bd305de4153ec21137a73ff482dcc2e3e19b.zip
Dissociate video file names and video uuid
Diffstat (limited to 'server/controllers/download.ts')
-rw-r--r--server/controllers/download.ts78
1 files changed, 78 insertions, 0 deletions
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 @@
1import * as cors from 'cors'
2import * as express from 'express'
3import { VideosTorrentCache } from '@server/lib/files-cache/videos-torrent-cache'
4import { getVideoFilePath } from '@server/lib/video-paths'
5import { MVideoFile, MVideoFullLight } from '@server/types/models'
6import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
7import { VideoStreamingPlaylistType } from '@shared/models'
8import { STATIC_DOWNLOAD_PATHS } from '../initializers/constants'
9import { asyncMiddleware, videosDownloadValidator } from '../middlewares'
10
11const downloadRouter = express.Router()
12
13downloadRouter.use(cors())
14
15downloadRouter.use(
16 STATIC_DOWNLOAD_PATHS.TORRENTS + ':filename',
17 downloadTorrent
18)
19
20downloadRouter.use(
21 STATIC_DOWNLOAD_PATHS.VIDEOS + ':id-:resolution([0-9]+).:extension',
22 asyncMiddleware(videosDownloadValidator),
23 downloadVideoFile
24)
25
26downloadRouter.use(
27 STATIC_DOWNLOAD_PATHS.HLS_VIDEOS + ':id-:resolution([0-9]+)-fragmented.:extension',
28 asyncMiddleware(videosDownloadValidator),
29 downloadHLSVideoFile
30)
31
32// ---------------------------------------------------------------------------
33
34export {
35 downloadRouter
36}
37
38// ---------------------------------------------------------------------------
39
40async function downloadTorrent (req: express.Request, res: express.Response) {
41 const result = await VideosTorrentCache.Instance.getFilePath(req.params.filename)
42 if (!result) return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
43
44 return res.download(result.path, result.downloadName)
45}
46
47function downloadVideoFile (req: express.Request, res: express.Response) {
48 const video = res.locals.videoAll
49
50 const videoFile = getVideoFile(req, video.VideoFiles)
51 if (!videoFile) return res.status(HttpStatusCode.NOT_FOUND_404).end()
52
53 return res.download(getVideoFilePath(video, videoFile), `${video.name}-${videoFile.resolution}p${videoFile.extname}`)
54}
55
56function downloadHLSVideoFile (req: express.Request, res: express.Response) {
57 const video = res.locals.videoAll
58 const playlist = getHLSPlaylist(video)
59 if (!playlist) return res.status(HttpStatusCode.NOT_FOUND_404).end
60
61 const videoFile = getVideoFile(req, playlist.VideoFiles)
62 if (!videoFile) return res.status(HttpStatusCode.NOT_FOUND_404).end()
63
64 const filename = `${video.name}-${videoFile.resolution}p-${playlist.getStringType()}${videoFile.extname}`
65 return res.download(getVideoFilePath(playlist, videoFile), filename)
66}
67
68function getVideoFile (req: express.Request, files: MVideoFile[]) {
69 const resolution = parseInt(req.params.resolution, 10)
70 return files.find(f => f.resolution === resolution)
71}
72
73function getHLSPlaylist (video: MVideoFullLight) {
74 const playlist = video.VideoStreamingPlaylists.find(p => p.type === VideoStreamingPlaylistType.HLS)
75 if (!playlist) return undefined
76
77 return Object.assign(playlist, { Video: video })
78}