-function getPreview (req: express.Request, res: express.Response, next: express.NextFunction) {
- VideosPreviewCache.Instance.getPreviewPath(req.params.uuid)
- .then(path => {
- if (!path) return res.sendStatus(404)
+async function getPreview (req: express.Request, res: express.Response, next: express.NextFunction) {
+ const path = await VideosPreviewCache.Instance.getPreviewPath(req.params.uuid)
+ if (!path) return res.sendStatus(404)
+
+ return res.sendFile(path, { maxAge: STATIC_MAX_AGE })
+}
+
+async function downloadTorrent (req: express.Request, res: express.Response, next: express.NextFunction) {
+ const { video, videoFile } = getVideoAndFile(req, res)
+ if (!videoFile) return res.status(404).end()
+
+ return res.download(video.getTorrentFilePath(videoFile), `${video.name}-${videoFile.resolution}p.torrent`)
+}
+
+async function downloadVideoFile (req: express.Request, res: express.Response, next: express.NextFunction) {
+ const { video, videoFile } = getVideoAndFile(req, res)
+ if (!videoFile) return res.status(404).end()
+
+ return res.download(video.getVideoFilePath(videoFile), `${video.name}-${videoFile.resolution}p${videoFile.extname}`)
+}
+
+function getVideoAndFile (req: express.Request, res: express.Response) {
+ const resolution = parseInt(req.params.resolution, 10)
+ const video: VideoModel = res.locals.video
+
+ const videoFile = video.VideoFiles.find(f => f.resolution === resolution)