diff options
author | Chocobozzz <me@florianbigard.com> | 2018-05-29 18:30:11 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-05-29 18:30:11 +0200 |
commit | 02756fbd11190e75b8bed9fad5751027e2e0de49 (patch) | |
tree | f90d6182fbb6226cb319bd0cb759ec0ec3f05512 /server/controllers/static.ts | |
parent | a20776fcbbe488475238228716cad370056ad5bd (diff) | |
download | PeerTube-02756fbd11190e75b8bed9fad5751027e2e0de49.tar.gz PeerTube-02756fbd11190e75b8bed9fad5751027e2e0de49.tar.zst PeerTube-02756fbd11190e75b8bed9fad5751027e2e0de49.zip |
Improve torrent/video download
Diffstat (limited to 'server/controllers/static.ts')
-rw-r--r-- | server/controllers/static.ts | 38 |
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 @@ | |||
1 | import * as cors from 'cors' | 1 | import * as cors from 'cors' |
2 | import * as express from 'express' | 2 | import * as express from 'express' |
3 | import { CONFIG, STATIC_MAX_AGE, STATIC_PATHS } from '../initializers' | 3 | import { CONFIG, STATIC_DOWNLOAD_PATHS, STATIC_MAX_AGE, STATIC_PATHS } from '../initializers' |
4 | import { VideosPreviewCache } from '../lib/cache' | 4 | import { VideosPreviewCache } from '../lib/cache' |
5 | import { asyncMiddleware } from '../middlewares' | 5 | import { asyncMiddleware, videosGetValidator } from '../middlewares' |
6 | import { VideoModel } from '../models/video/video' | ||
6 | 7 | ||
7 | const staticRouter = express.Router() | 8 | const 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 | ) |
20 | staticRouter.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 |
21 | const videosPhysicalPath = CONFIG.STORAGE.VIDEOS_DIR | 27 | const 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 | ) |
33 | staticRouter.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 |
29 | const thumbnailsPhysicalPath = CONFIG.STORAGE.THUMBNAILS_DIR | 40 | const 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 | |||
79 | async 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 | |||
86 | async 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 | |||
93 | function 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 | } | ||