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