]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/redundancy.ts
Cleanup useless express validator messages
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / redundancy.ts
1 import express from 'express'
2 import { body, param, query } from 'express-validator'
3 import { isVideoRedundancyTarget } from '@server/helpers/custom-validators/video-redundancies'
4 import { HttpStatusCode } from '../../../shared/models/http/http-error-codes'
5 import {
6 exists,
7 isBooleanValid,
8 isIdOrUUIDValid,
9 isIdValid,
10 toBooleanOrNull,
11 toCompleteUUID,
12 toIntOrNull
13 } from '../../helpers/custom-validators/misc'
14 import { isHostValid } from '../../helpers/custom-validators/servers'
15 import { logger } from '../../helpers/logger'
16 import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy'
17 import { ServerModel } from '../../models/server/server'
18 import { areValidationErrors, doesVideoExist, isValidVideoIdParam } from './shared'
19
20 const videoFileRedundancyGetValidator = [
21 isValidVideoIdParam('videoId'),
22
23 param('resolution')
24 .customSanitizer(toIntOrNull)
25 .custom(exists),
26 param('fps')
27 .optional()
28 .customSanitizer(toIntOrNull)
29 .custom(exists),
30
31 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
32 logger.debug('Checking videoFileRedundancyGetValidator parameters', { parameters: req.params })
33
34 if (areValidationErrors(req, res)) return
35 if (!await doesVideoExist(req.params.videoId, res)) return
36
37 const video = res.locals.videoAll
38
39 const paramResolution = req.params.resolution as unknown as number // We casted to int above
40 const paramFPS = req.params.fps as unknown as number // We casted to int above
41
42 const videoFile = video.VideoFiles.find(f => {
43 return f.resolution === paramResolution && (!req.params.fps || paramFPS)
44 })
45
46 if (!videoFile) {
47 return res.fail({
48 status: HttpStatusCode.NOT_FOUND_404,
49 message: 'Video file not found.'
50 })
51 }
52 res.locals.videoFile = videoFile
53
54 const videoRedundancy = await VideoRedundancyModel.loadLocalByFileId(videoFile.id)
55 if (!videoRedundancy) {
56 return res.fail({
57 status: HttpStatusCode.NOT_FOUND_404,
58 message: 'Video redundancy not found.'
59 })
60 }
61 res.locals.videoRedundancy = videoRedundancy
62
63 return next()
64 }
65 ]
66
67 const videoPlaylistRedundancyGetValidator = [
68 isValidVideoIdParam('videoId'),
69
70 param('streamingPlaylistType')
71 .customSanitizer(toIntOrNull)
72 .custom(exists),
73
74 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
75 logger.debug('Checking videoPlaylistRedundancyGetValidator parameters', { parameters: req.params })
76
77 if (areValidationErrors(req, res)) return
78 if (!await doesVideoExist(req.params.videoId, res)) return
79
80 const video = res.locals.videoAll
81
82 const paramPlaylistType = req.params.streamingPlaylistType as unknown as number // We casted to int above
83 const videoStreamingPlaylist = video.VideoStreamingPlaylists.find(p => p.type === paramPlaylistType)
84
85 if (!videoStreamingPlaylist) {
86 return res.fail({
87 status: HttpStatusCode.NOT_FOUND_404,
88 message: 'Video playlist not found.'
89 })
90 }
91 res.locals.videoStreamingPlaylist = videoStreamingPlaylist
92
93 const videoRedundancy = await VideoRedundancyModel.loadLocalByStreamingPlaylistId(videoStreamingPlaylist.id)
94 if (!videoRedundancy) {
95 return res.fail({
96 status: HttpStatusCode.NOT_FOUND_404,
97 message: 'Video redundancy not found.'
98 })
99 }
100 res.locals.videoRedundancy = videoRedundancy
101
102 return next()
103 }
104 ]
105
106 const updateServerRedundancyValidator = [
107 param('host')
108 .custom(isHostValid),
109
110 body('redundancyAllowed')
111 .customSanitizer(toBooleanOrNull)
112 .custom(isBooleanValid).withMessage('Should have a valid redundancyAllowed boolean'),
113
114 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
115 logger.debug('Checking updateServerRedundancy parameters', { parameters: req.params })
116
117 if (areValidationErrors(req, res)) return
118
119 const server = await ServerModel.loadByHost(req.params.host)
120
121 if (!server) {
122 return res.fail({
123 status: HttpStatusCode.NOT_FOUND_404,
124 message: `Server ${req.params.host} not found.`
125 })
126 }
127
128 res.locals.server = server
129 return next()
130 }
131 ]
132
133 const listVideoRedundanciesValidator = [
134 query('target')
135 .custom(isVideoRedundancyTarget),
136
137 (req: express.Request, res: express.Response, next: express.NextFunction) => {
138 logger.debug('Checking listVideoRedundanciesValidator parameters', { parameters: req.query })
139
140 if (areValidationErrors(req, res)) return
141
142 return next()
143 }
144 ]
145
146 const addVideoRedundancyValidator = [
147 body('videoId')
148 .customSanitizer(toCompleteUUID)
149 .custom(isIdOrUUIDValid),
150
151 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
152 logger.debug('Checking addVideoRedundancyValidator parameters', { parameters: req.query })
153
154 if (areValidationErrors(req, res)) return
155
156 if (!await doesVideoExist(req.body.videoId, res, 'only-video')) return
157
158 if (res.locals.onlyVideo.remote === false) {
159 return res.fail({ message: 'Cannot create a redundancy on a local video' })
160 }
161
162 if (res.locals.onlyVideo.isLive) {
163 return res.fail({ message: 'Cannot create a redundancy of a live video' })
164 }
165
166 const alreadyExists = await VideoRedundancyModel.isLocalByVideoUUIDExists(res.locals.onlyVideo.uuid)
167 if (alreadyExists) {
168 return res.fail({
169 status: HttpStatusCode.CONFLICT_409,
170 message: 'This video is already duplicated by your instance.'
171 })
172 }
173
174 return next()
175 }
176 ]
177
178 const removeVideoRedundancyValidator = [
179 param('redundancyId')
180 .custom(isIdValid),
181
182 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
183 logger.debug('Checking removeVideoRedundancyValidator parameters', { parameters: req.query })
184
185 if (areValidationErrors(req, res)) return
186
187 const redundancy = await VideoRedundancyModel.loadByIdWithVideo(parseInt(req.params.redundancyId, 10))
188 if (!redundancy) {
189 return res.fail({
190 status: HttpStatusCode.NOT_FOUND_404,
191 message: 'Video redundancy not found'
192 })
193 }
194
195 res.locals.videoRedundancy = redundancy
196
197 return next()
198 }
199 ]
200
201 // ---------------------------------------------------------------------------
202
203 export {
204 videoFileRedundancyGetValidator,
205 videoPlaylistRedundancyGetValidator,
206 updateServerRedundancyValidator,
207 listVideoRedundanciesValidator,
208 addVideoRedundancyValidator,
209 removeVideoRedundancyValidator
210 }