aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-05-29 18:30:11 +0200
committerChocobozzz <me@florianbigard.com>2018-05-29 18:30:11 +0200
commit02756fbd11190e75b8bed9fad5751027e2e0de49 (patch)
treef90d6182fbb6226cb319bd0cb759ec0ec3f05512 /server/controllers
parenta20776fcbbe488475238228716cad370056ad5bd (diff)
downloadPeerTube-02756fbd11190e75b8bed9fad5751027e2e0de49.tar.gz
PeerTube-02756fbd11190e75b8bed9fad5751027e2e0de49.tar.zst
PeerTube-02756fbd11190e75b8bed9fad5751027e2e0de49.zip
Improve torrent/video download
Diffstat (limited to 'server/controllers')
-rw-r--r--server/controllers/static.ts38
1 files changed, 36 insertions, 2 deletions
diff --git a/server/controllers/static.ts b/server/controllers/static.ts
index c1bf384a4..8bebe6fa7 100644
--- a/server/controllers/static.ts
+++ b/server/controllers/static.ts
@@ -1,8 +1,9 @@
1import * as cors from 'cors' 1import * as cors from 'cors'
2import * as express from 'express' 2import * as express from 'express'
3import { CONFIG, STATIC_MAX_AGE, STATIC_PATHS } from '../initializers' 3import { CONFIG, STATIC_DOWNLOAD_PATHS, STATIC_MAX_AGE, STATIC_PATHS } from '../initializers'
4import { VideosPreviewCache } from '../lib/cache' 4import { VideosPreviewCache } from '../lib/cache'
5import { asyncMiddleware } from '../middlewares' 5import { asyncMiddleware, videosGetValidator } from '../middlewares'
6import { VideoModel } from '../models/video/video'
6 7
7const staticRouter = express.Router() 8const staticRouter = express.Router()
8 9
@@ -16,6 +17,11 @@ staticRouter.use(
16 cors(), 17 cors(),
17 express.static(torrentsPhysicalPath, { maxAge: 0 }) // Don't cache because we could regenerate the torrent file 18 express.static(torrentsPhysicalPath, { maxAge: 0 }) // Don't cache because we could regenerate the torrent file
18) 19)
20staticRouter.use(
21 STATIC_DOWNLOAD_PATHS.TORRENTS + ':id-:resolution([0-9]+).torrent',
22 asyncMiddleware(videosGetValidator),
23 asyncMiddleware(downloadTorrent)
24)
19 25
20// Videos path for webseeding 26// Videos path for webseeding
21const videosPhysicalPath = CONFIG.STORAGE.VIDEOS_DIR 27const videosPhysicalPath = CONFIG.STORAGE.VIDEOS_DIR
@@ -24,6 +30,11 @@ staticRouter.use(
24 cors(), 30 cors(),
25 express.static(videosPhysicalPath, { maxAge: STATIC_MAX_AGE }) 31 express.static(videosPhysicalPath, { maxAge: STATIC_MAX_AGE })
26) 32)
33staticRouter.use(
34 STATIC_DOWNLOAD_PATHS.VIDEOS + ':id-:resolution([0-9]+).:extension',
35 asyncMiddleware(videosGetValidator),
36 asyncMiddleware(downloadVideoFile)
37)
27 38
28// Thumbnails path for express 39// Thumbnails path for express
29const thumbnailsPhysicalPath = CONFIG.STORAGE.THUMBNAILS_DIR 40const thumbnailsPhysicalPath = CONFIG.STORAGE.THUMBNAILS_DIR
@@ -64,3 +75,26 @@ async function getPreview (req: express.Request, res: express.Response, next: ex
64 75
65 return res.sendFile(path, { maxAge: STATIC_MAX_AGE }) 76 return res.sendFile(path, { maxAge: STATIC_MAX_AGE })
66} 77}
78
79async function downloadTorrent (req: express.Request, res: express.Response, next: express.NextFunction) {
80 const { video, videoFile } = getVideoAndFileOr404(req, res)
81 if (!videoFile) return res.status(404).end()
82
83 return res.download(video.getTorrentFilePath(videoFile), `${video.name}-${videoFile.resolution}p.torrent`)
84}
85
86async function downloadVideoFile (req: express.Request, res: express.Response, next: express.NextFunction) {
87 const { video, videoFile } = getVideoAndFileOr404(req, res)
88 if (!videoFile) return res.status(404).end()
89
90 return res.download(video.getVideoFilePath(videoFile), `${video.name}-${videoFile.resolution}p${videoFile.extname}`)
91}
92
93function getVideoAndFileOr404 (req: express.Request, res: express.Response) {
94 const resolution = parseInt(req.params.resolution, 10)
95 const video: VideoModel = res.locals.video
96
97 const videoFile = video.VideoFiles.find(f => f.resolution === resolution)
98
99 return { video, videoFile }
100}