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