]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/redundancy.ts
esModuleInterop to true
[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).withMessage('Should have a valid resolution'),
26 param('fps')
27 .optional()
28 .customSanitizer(toIntOrNull)
29 .custom(exists).withMessage('Should have a valid fps'),
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).withMessage('Should have a valid streaming playlist type'),
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').custom(isHostValid).withMessage('Should have a valid host'),
108 body('redundancyAllowed')
109 .customSanitizer(toBooleanOrNull)
110 .custom(isBooleanValid).withMessage('Should have a valid redundancyAllowed attribute'),
111
112 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
113 logger.debug('Checking updateServerRedundancy parameters', { parameters: req.params })
114
115 if (areValidationErrors(req, res)) return
116
117 const server = await ServerModel.loadByHost(req.params.host)
118
119 if (!server) {
120 return res.fail({
121 status: HttpStatusCode.NOT_FOUND_404,
122 message: `Server ${req.params.host} not found.`
123 })
124 }
125
126 res.locals.server = server
127 return next()
128 }
129 ]
130
131 const listVideoRedundanciesValidator = [
132 query('target')
133 .custom(isVideoRedundancyTarget).withMessage('Should have a valid video redundancies target'),
134
135 (req: express.Request, res: express.Response, next: express.NextFunction) => {
136 logger.debug('Checking listVideoRedundanciesValidator parameters', { parameters: req.query })
137
138 if (areValidationErrors(req, res)) return
139
140 return next()
141 }
142 ]
143
144 const addVideoRedundancyValidator = [
145 body('videoId')
146 .customSanitizer(toCompleteUUID)
147 .custom(isIdOrUUIDValid)
148 .withMessage('Should have a valid video id'),
149
150 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
151 logger.debug('Checking addVideoRedundancyValidator parameters', { parameters: req.query })
152
153 if (areValidationErrors(req, res)) return
154
155 if (!await doesVideoExist(req.body.videoId, res, 'only-video')) return
156
157 if (res.locals.onlyVideo.remote === false) {
158 return res.fail({ message: 'Cannot create a redundancy on a local video' })
159 }
160
161 if (res.locals.onlyVideo.isLive) {
162 return res.fail({ message: 'Cannot create a redundancy of a live video' })
163 }
164
165 const alreadyExists = await VideoRedundancyModel.isLocalByVideoUUIDExists(res.locals.onlyVideo.uuid)
166 if (alreadyExists) {
167 return res.fail({
168 status: HttpStatusCode.CONFLICT_409,
169 message: 'This video is already duplicated by your instance.'
170 })
171 }
172
173 return next()
174 }
175 ]
176
177 const removeVideoRedundancyValidator = [
178 param('redundancyId')
179 .custom(isIdValid)
180 .withMessage('Should have a valid redundancy id'),
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 }