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