]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/redundancy.ts
Reorganize plugin models
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / redundancy.ts
1 import * as express from 'express'
2 import { body, param, query } from 'express-validator'
3 import { exists, isBooleanValid, isIdOrUUIDValid, isIdValid, toBooleanOrNull, toIntOrNull } from '../../helpers/custom-validators/misc'
4 import { logger } from '../../helpers/logger'
5 import { areValidationErrors } from './utils'
6 import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy'
7 import { isHostValid } from '../../helpers/custom-validators/servers'
8 import { ServerModel } from '../../models/server/server'
9 import { doesVideoExist } from '../../helpers/middlewares'
10 import { isVideoRedundancyTarget } from '@server/helpers/custom-validators/video-redundancies'
11 import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
12
13 const videoFileRedundancyGetValidator = [
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) => {
24 logger.debug('Checking videoFileRedundancyGetValidator parameters', { parameters: req.params })
25
26 if (areValidationErrors(req, res)) return
27 if (!await doesVideoExist(req.params.videoId, res)) return
28
29 const video = res.locals.videoAll
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
34 const videoFile = video.VideoFiles.find(f => {
35 return f.resolution === paramResolution && (!req.params.fps || paramFPS)
36 })
37
38 if (!videoFile) return res.status(HttpStatusCode.NOT_FOUND_404).json({ error: 'Video file not found.' })
39 res.locals.videoFile = videoFile
40
41 const videoRedundancy = await VideoRedundancyModel.loadLocalByFileId(videoFile.id)
42 if (!videoRedundancy) return res.status(HttpStatusCode.NOT_FOUND_404).json({ error: 'Video redundancy not found.' })
43 res.locals.videoRedundancy = videoRedundancy
44
45 return next()
46 }
47 ]
48
49 const videoPlaylistRedundancyGetValidator = [
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'),
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
61 if (!await doesVideoExist(req.params.videoId, res)) return
62
63 const video = res.locals.videoAll
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)
67
68 if (!videoStreamingPlaylist) return res.status(HttpStatusCode.NOT_FOUND_404).json({ error: 'Video playlist not found.' })
69 res.locals.videoStreamingPlaylist = videoStreamingPlaylist
70
71 const videoRedundancy = await VideoRedundancyModel.loadLocalByStreamingPlaylistId(videoStreamingPlaylist.id)
72 if (!videoRedundancy) return res.status(HttpStatusCode.NOT_FOUND_404).json({ error: 'Video redundancy not found.' })
73 res.locals.videoRedundancy = videoRedundancy
74
75 return next()
76 }
77 ]
78
79 const updateServerRedundancyValidator = [
80 param('host').custom(isHostValid).withMessage('Should have a valid host'),
81 body('redundancyAllowed')
82 .customSanitizer(toBooleanOrNull)
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
94 .status(HttpStatusCode.NOT_FOUND_404)
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
106 const listVideoRedundanciesValidator = [
107 query('target')
108 .custom(isVideoRedundancyTarget).withMessage('Should have a valid video redundancies target'),
109
110 (req: express.Request, res: express.Response, next: express.NextFunction) => {
111 logger.debug('Checking listVideoRedundanciesValidator parameters', { parameters: req.query })
112
113 if (areValidationErrors(req, res)) return
114
115 return next()
116 }
117 ]
118
119 const 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) {
132 return res.status(HttpStatusCode.BAD_REQUEST_400)
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' })
139 }
140
141 const alreadyExists = await VideoRedundancyModel.isLocalByVideoUUIDExists(res.locals.onlyVideo.uuid)
142 if (alreadyExists) {
143 return res.status(HttpStatusCode.CONFLICT_409)
144 .json({ error: 'This video is already duplicated by your instance.' })
145 }
146
147 return next()
148 }
149 ]
150
151 const 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) {
163 return res.status(HttpStatusCode.NOT_FOUND_404)
164 .json({ error: 'Video redundancy not found' })
165 .end()
166 }
167
168 res.locals.videoRedundancy = redundancy
169
170 return next()
171 }
172 ]
173
174 // ---------------------------------------------------------------------------
175
176 export {
177 videoFileRedundancyGetValidator,
178 videoPlaylistRedundancyGetValidator,
179 updateServerRedundancyValidator,
180 listVideoRedundanciesValidator,
181 addVideoRedundancyValidator,
182 removeVideoRedundancyValidator
183 }