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