]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/download.ts
Dissociate video file names and video uuid
[github/Chocobozzz/PeerTube.git] / server / controllers / download.ts
CommitLineData
90a8bd30
C
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}