]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/redundancy.ts
Type toActivityPubObject functions
[github/Chocobozzz/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 const videoFile = video.VideoFiles.find(f => {
29 return f.resolution === req.params.resolution && (!req.params.fps || f.fps === req.params.fps)
30 })
31
32 if (!videoFile) return res.status(404).json({ error: 'Video file not found.' })
33 res.locals.videoFile = videoFile
34
35 const videoRedundancy = await VideoRedundancyModel.loadLocalByFileId(videoFile.id)
36 if (!videoRedundancy) return res.status(404).json({ error: 'Video redundancy not found.' })
37 res.locals.videoRedundancy = videoRedundancy
38
39 return next()
40 }
41 ]
42
43 const videoPlaylistRedundancyGetValidator = [
44 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'),
45 param('streamingPlaylistType').custom(exists).withMessage('Should have a valid streaming playlist type'),
46
47 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
48 logger.debug('Checking videoPlaylistRedundancyGetValidator parameters', { parameters: req.params })
49
50 if (areValidationErrors(req, res)) return
51 if (!await doesVideoExist(req.params.videoId, res)) return
52
53 const video = res.locals.videoAll
54 const videoStreamingPlaylist = video.VideoStreamingPlaylists.find(p => p === req.params.streamingPlaylistType)
55
56 if (!videoStreamingPlaylist) return res.status(404).json({ error: 'Video playlist not found.' })
57 res.locals.videoStreamingPlaylist = videoStreamingPlaylist
58
59 const videoRedundancy = await VideoRedundancyModel.loadLocalByStreamingPlaylistId(videoStreamingPlaylist.id)
60 if (!videoRedundancy) return res.status(404).json({ error: 'Video redundancy not found.' })
61 res.locals.videoRedundancy = videoRedundancy
62
63 return next()
64 }
65 ]
66
67 const updateServerRedundancyValidator = [
68 param('host').custom(isHostValid).withMessage('Should have a valid host'),
69 body('redundancyAllowed')
70 .customSanitizer(toBooleanOrNull)
71 .custom(isBooleanValid).withMessage('Should have a valid redundancyAllowed attribute'),
72
73 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
74 logger.debug('Checking updateServerRedundancy parameters', { parameters: req.params })
75
76 if (areValidationErrors(req, res)) return
77
78 const server = await ServerModel.loadByHost(req.params.host)
79
80 if (!server) {
81 return res
82 .status(404)
83 .json({
84 error: `Server ${req.params.host} not found.`
85 })
86 .end()
87 }
88
89 res.locals.server = server
90 return next()
91 }
92 ]
93
94 // ---------------------------------------------------------------------------
95
96 export {
97 videoFileRedundancyGetValidator,
98 videoPlaylistRedundancyGetValidator,
99 updateServerRedundancyValidator
100 }