Merge branch 'master' into develop
[oweals/peertube.git] / server / middlewares / validators / redundancy.ts
1 import * as express from 'express'
2 import 'express-validator'
3 import { body, param } from 'express-validator/check'
4 import { exists, isBooleanValid, isIdOrUUIDValid, toIntOrNull } from '../../helpers/custom-validators/misc'
5 import { doesVideoExist } from '../../helpers/custom-validators/videos'
6 import { logger } from '../../helpers/logger'
7 import { areValidationErrors } from './utils'
8 import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy'
9 import { isHostValid } from '../../helpers/custom-validators/servers'
10 import { ServerModel } from '../../models/server/server'
11
12 const videoFileRedundancyGetValidator = [
13   param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'),
14   param('resolution')
15     .customSanitizer(toIntOrNull)
16     .custom(exists).withMessage('Should have a valid resolution'),
17   param('fps')
18     .optional()
19     .customSanitizer(toIntOrNull)
20     .custom(exists).withMessage('Should have a valid fps'),
21
22   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
23     logger.debug('Checking videoFileRedundancyGetValidator parameters', { parameters: req.params })
24
25     if (areValidationErrors(req, res)) return
26     if (!await doesVideoExist(req.params.videoId, res)) return
27
28     const video = res.locals.video
29     const videoFile = video.VideoFiles.find(f => {
30       return f.resolution === req.params.resolution && (!req.params.fps || f.fps === req.params.fps)
31     })
32
33     if (!videoFile) return res.status(404).json({ error: 'Video file not found.' })
34     res.locals.videoFile = videoFile
35
36     const videoRedundancy = await VideoRedundancyModel.loadLocalByFileId(videoFile.id)
37     if (!videoRedundancy) return res.status(404).json({ error: 'Video redundancy not found.' })
38     res.locals.videoRedundancy = videoRedundancy
39
40     return next()
41   }
42 ]
43
44 const videoPlaylistRedundancyGetValidator = [
45   param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'),
46   param('streamingPlaylistType').custom(exists).withMessage('Should have a valid streaming playlist type'),
47
48   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
49     logger.debug('Checking videoPlaylistRedundancyGetValidator parameters', { parameters: req.params })
50
51     if (areValidationErrors(req, res)) return
52     if (!await doesVideoExist(req.params.videoId, res)) return
53
54     const video = res.locals.video
55     const videoStreamingPlaylist = video.VideoStreamingPlaylists.find(p => p === req.params.streamingPlaylistType)
56
57     if (!videoStreamingPlaylist) return res.status(404).json({ error: 'Video playlist not found.' })
58     res.locals.videoStreamingPlaylist = videoStreamingPlaylist
59
60     const videoRedundancy = await VideoRedundancyModel.loadLocalByStreamingPlaylistId(videoStreamingPlaylist.id)
61     if (!videoRedundancy) return res.status(404).json({ error: 'Video redundancy not found.' })
62     res.locals.videoRedundancy = videoRedundancy
63
64     return next()
65   }
66 ]
67
68 const updateServerRedundancyValidator = [
69   param('host').custom(isHostValid).withMessage('Should have a valid host'),
70   body('redundancyAllowed')
71     .toBoolean()
72     .custom(isBooleanValid).withMessage('Should have a valid redundancyAllowed attribute'),
73
74   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
75     logger.debug('Checking updateServerRedundancy parameters', { parameters: req.params })
76
77     if (areValidationErrors(req, res)) return
78
79     const server = await ServerModel.loadByHost(req.params.host)
80
81     if (!server) {
82       return res
83         .status(404)
84         .json({
85           error: `Server ${req.params.host} not found.`
86         })
87         .end()
88     }
89
90     res.locals.server = server
91     return next()
92   }
93 ]
94
95 // ---------------------------------------------------------------------------
96
97 export {
98   videoFileRedundancyGetValidator,
99   videoPlaylistRedundancyGetValidator,
100   updateServerRedundancyValidator
101 }