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