aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators/redundancy.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/middlewares/validators/redundancy.ts')
-rw-r--r--server/middlewares/validators/redundancy.ts87
1 files changed, 57 insertions, 30 deletions
diff --git a/server/middlewares/validators/redundancy.ts b/server/middlewares/validators/redundancy.ts
index c379aebe4..116c8c611 100644
--- a/server/middlewares/validators/redundancy.ts
+++ b/server/middlewares/validators/redundancy.ts
@@ -1,17 +1,25 @@
1import * as express from 'express' 1import * as express from 'express'
2import { body, param, query } from 'express-validator' 2import { body, param, query } from 'express-validator'
3import { exists, isBooleanValid, isIdOrUUIDValid, isIdValid, toBooleanOrNull, toIntOrNull } from '../../helpers/custom-validators/misc' 3import { isVideoRedundancyTarget } from '@server/helpers/custom-validators/video-redundancies'
4import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
5import {
6 exists,
7 isBooleanValid,
8 isIdOrUUIDValid,
9 isIdValid,
10 toBooleanOrNull,
11 toCompleteUUID,
12 toIntOrNull
13} from '../../helpers/custom-validators/misc'
14import { isHostValid } from '../../helpers/custom-validators/servers'
4import { logger } from '../../helpers/logger' 15import { logger } from '../../helpers/logger'
5import { areValidationErrors } from './utils'
6import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy' 16import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy'
7import { isHostValid } from '../../helpers/custom-validators/servers'
8import { ServerModel } from '../../models/server/server' 17import { ServerModel } from '../../models/server/server'
9import { doesVideoExist } from '../../helpers/middlewares' 18import { areValidationErrors, doesVideoExist, isValidVideoIdParam } from './shared'
10import { isVideoRedundancyTarget } from '@server/helpers/custom-validators/video-redundancies'
11import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
12 19
13const videoFileRedundancyGetValidator = [ 20const videoFileRedundancyGetValidator = [
14 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'), 21 isValidVideoIdParam('videoId'),
22
15 param('resolution') 23 param('resolution')
16 .customSanitizer(toIntOrNull) 24 .customSanitizer(toIntOrNull)
17 .custom(exists).withMessage('Should have a valid resolution'), 25 .custom(exists).withMessage('Should have a valid resolution'),
@@ -35,11 +43,21 @@ const videoFileRedundancyGetValidator = [
35 return f.resolution === paramResolution && (!req.params.fps || paramFPS) 43 return f.resolution === paramResolution && (!req.params.fps || paramFPS)
36 }) 44 })
37 45
38 if (!videoFile) return res.status(HttpStatusCode.NOT_FOUND_404).json({ error: 'Video file not found.' }) 46 if (!videoFile) {
47 return res.fail({
48 status: HttpStatusCode.NOT_FOUND_404,
49 message: 'Video file not found.'
50 })
51 }
39 res.locals.videoFile = videoFile 52 res.locals.videoFile = videoFile
40 53
41 const videoRedundancy = await VideoRedundancyModel.loadLocalByFileId(videoFile.id) 54 const videoRedundancy = await VideoRedundancyModel.loadLocalByFileId(videoFile.id)
42 if (!videoRedundancy) return res.status(HttpStatusCode.NOT_FOUND_404).json({ error: 'Video redundancy not found.' }) 55 if (!videoRedundancy) {
56 return res.fail({
57 status: HttpStatusCode.NOT_FOUND_404,
58 message: 'Video redundancy not found.'
59 })
60 }
43 res.locals.videoRedundancy = videoRedundancy 61 res.locals.videoRedundancy = videoRedundancy
44 62
45 return next() 63 return next()
@@ -47,9 +65,8 @@ const videoFileRedundancyGetValidator = [
47] 65]
48 66
49const videoPlaylistRedundancyGetValidator = [ 67const videoPlaylistRedundancyGetValidator = [
50 param('videoId') 68 isValidVideoIdParam('videoId'),
51 .custom(isIdOrUUIDValid) 69
52 .not().isEmpty().withMessage('Should have a valid video id'),
53 param('streamingPlaylistType') 70 param('streamingPlaylistType')
54 .customSanitizer(toIntOrNull) 71 .customSanitizer(toIntOrNull)
55 .custom(exists).withMessage('Should have a valid streaming playlist type'), 72 .custom(exists).withMessage('Should have a valid streaming playlist type'),
@@ -65,11 +82,21 @@ const videoPlaylistRedundancyGetValidator = [
65 const paramPlaylistType = req.params.streamingPlaylistType as unknown as number // We casted to int above 82 const paramPlaylistType = req.params.streamingPlaylistType as unknown as number // We casted to int above
66 const videoStreamingPlaylist = video.VideoStreamingPlaylists.find(p => p.type === paramPlaylistType) 83 const videoStreamingPlaylist = video.VideoStreamingPlaylists.find(p => p.type === paramPlaylistType)
67 84
68 if (!videoStreamingPlaylist) return res.status(HttpStatusCode.NOT_FOUND_404).json({ error: 'Video playlist not found.' }) 85 if (!videoStreamingPlaylist) {
86 return res.fail({
87 status: HttpStatusCode.NOT_FOUND_404,
88 message: 'Video playlist not found.'
89 })
90 }
69 res.locals.videoStreamingPlaylist = videoStreamingPlaylist 91 res.locals.videoStreamingPlaylist = videoStreamingPlaylist
70 92
71 const videoRedundancy = await VideoRedundancyModel.loadLocalByStreamingPlaylistId(videoStreamingPlaylist.id) 93 const videoRedundancy = await VideoRedundancyModel.loadLocalByStreamingPlaylistId(videoStreamingPlaylist.id)
72 if (!videoRedundancy) return res.status(HttpStatusCode.NOT_FOUND_404).json({ error: 'Video redundancy not found.' }) 94 if (!videoRedundancy) {
95 return res.fail({
96 status: HttpStatusCode.NOT_FOUND_404,
97 message: 'Video redundancy not found.'
98 })
99 }
73 res.locals.videoRedundancy = videoRedundancy 100 res.locals.videoRedundancy = videoRedundancy
74 101
75 return next() 102 return next()
@@ -90,12 +117,10 @@ const updateServerRedundancyValidator = [
90 const server = await ServerModel.loadByHost(req.params.host) 117 const server = await ServerModel.loadByHost(req.params.host)
91 118
92 if (!server) { 119 if (!server) {
93 return res 120 return res.fail({
94 .status(HttpStatusCode.NOT_FOUND_404) 121 status: HttpStatusCode.NOT_FOUND_404,
95 .json({ 122 message: `Server ${req.params.host} not found.`
96 error: `Server ${req.params.host} not found.` 123 })
97 })
98 .end()
99 } 124 }
100 125
101 res.locals.server = server 126 res.locals.server = server
@@ -118,7 +143,8 @@ const listVideoRedundanciesValidator = [
118 143
119const addVideoRedundancyValidator = [ 144const addVideoRedundancyValidator = [
120 body('videoId') 145 body('videoId')
121 .custom(isIdValid) 146 .customSanitizer(toCompleteUUID)
147 .custom(isIdOrUUIDValid)
122 .withMessage('Should have a valid video id'), 148 .withMessage('Should have a valid video id'),
123 149
124 async (req: express.Request, res: express.Response, next: express.NextFunction) => { 150 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
@@ -129,19 +155,19 @@ const addVideoRedundancyValidator = [
129 if (!await doesVideoExist(req.body.videoId, res, 'only-video')) return 155 if (!await doesVideoExist(req.body.videoId, res, 'only-video')) return
130 156
131 if (res.locals.onlyVideo.remote === false) { 157 if (res.locals.onlyVideo.remote === false) {
132 return res.status(HttpStatusCode.BAD_REQUEST_400) 158 return res.fail({ message: 'Cannot create a redundancy on a local video' })
133 .json({ error: 'Cannot create a redundancy on a local video' })
134 } 159 }
135 160
136 if (res.locals.onlyVideo.isLive) { 161 if (res.locals.onlyVideo.isLive) {
137 return res.status(HttpStatusCode.BAD_REQUEST_400) 162 return res.fail({ message: 'Cannot create a redundancy of a live video' })
138 .json({ error: 'Cannot create a redundancy of a live video' })
139 } 163 }
140 164
141 const alreadyExists = await VideoRedundancyModel.isLocalByVideoUUIDExists(res.locals.onlyVideo.uuid) 165 const alreadyExists = await VideoRedundancyModel.isLocalByVideoUUIDExists(res.locals.onlyVideo.uuid)
142 if (alreadyExists) { 166 if (alreadyExists) {
143 return res.status(HttpStatusCode.CONFLICT_409) 167 return res.fail({
144 .json({ error: 'This video is already duplicated by your instance.' }) 168 status: HttpStatusCode.CONFLICT_409,
169 message: 'This video is already duplicated by your instance.'
170 })
145 } 171 }
146 172
147 return next() 173 return next()
@@ -160,9 +186,10 @@ const removeVideoRedundancyValidator = [
160 186
161 const redundancy = await VideoRedundancyModel.loadByIdWithVideo(parseInt(req.params.redundancyId, 10)) 187 const redundancy = await VideoRedundancyModel.loadByIdWithVideo(parseInt(req.params.redundancyId, 10))
162 if (!redundancy) { 188 if (!redundancy) {
163 return res.status(HttpStatusCode.NOT_FOUND_404) 189 return res.fail({
164 .json({ error: 'Video redundancy not found' }) 190 status: HttpStatusCode.NOT_FOUND_404,
165 .end() 191 message: 'Video redundancy not found'
192 })
166 } 193 }
167 194
168 res.locals.videoRedundancy = redundancy 195 res.locals.videoRedundancy = redundancy