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