aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators/redundancy.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2023-07-31 14:34:36 +0200
committerChocobozzz <me@florianbigard.com>2023-08-11 15:02:33 +0200
commit3a4992633ee62d5edfbb484d9c6bcb3cf158489d (patch)
treee4510b39bdac9c318fdb4b47018d08f15368b8f0 /server/middlewares/validators/redundancy.ts
parent04d1da5621d25d59bd5fa1543b725c497bf5d9a8 (diff)
downloadPeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.gz
PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.zst
PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.zip
Migrate server to ESM
Sorry for the very big commit that may lead to git log issues and merge conflicts, but it's a major step forward: * Server can be faster at startup because imports() are async and we can easily lazy import big modules * Angular doesn't seem to support ES import (with .js extension), so we had to correctly organize peertube into a monorepo: * Use yarn workspace feature * Use typescript reference projects for dependencies * Shared projects have been moved into "packages", each one is now a node module (with a dedicated package.json/tsconfig.json) * server/tools have been moved into apps/ and is now a dedicated app bundled and published on NPM so users don't have to build peertube cli tools manually * server/tests have been moved into packages/ so we don't compile them every time we want to run the server * Use isolatedModule option: * Had to move from const enum to const (https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums) * Had to explictely specify "type" imports when used in decorators * Prefer tsx (that uses esbuild under the hood) instead of ts-node to load typescript files (tests with mocha or scripts): * To reduce test complexity as esbuild doesn't support decorator metadata, we only test server files that do not import server models * We still build tests files into js files for a faster CI * Remove unmaintained peertube CLI import script * Removed some barrels to speed up execution (less imports)
Diffstat (limited to 'server/middlewares/validators/redundancy.ts')
-rw-r--r--server/middlewares/validators/redundancy.ts198
1 files changed, 0 insertions, 198 deletions
diff --git a/server/middlewares/validators/redundancy.ts b/server/middlewares/validators/redundancy.ts
deleted file mode 100644
index c80f9b728..000000000
--- a/server/middlewares/validators/redundancy.ts
+++ /dev/null
@@ -1,198 +0,0 @@
1import express from 'express'
2import { body, param, query } from 'express-validator'
3import { isVideoRedundancyTarget } from '@server/helpers/custom-validators/video-redundancies'
4import { forceNumber } from '@shared/core-utils'
5import { HttpStatusCode } from '../../../shared/models/http/http-error-codes'
6import {
7 exists,
8 isBooleanValid,
9 isIdOrUUIDValid,
10 isIdValid,
11 toBooleanOrNull,
12 toCompleteUUID,
13 toIntOrNull
14} from '../../helpers/custom-validators/misc'
15import { isHostValid } from '../../helpers/custom-validators/servers'
16import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy'
17import { ServerModel } from '../../models/server/server'
18import { areValidationErrors, doesVideoExist, isValidVideoIdParam } from './shared'
19
20const videoFileRedundancyGetValidator = [
21 isValidVideoIdParam('videoId'),
22
23 param('resolution')
24 .customSanitizer(toIntOrNull)
25 .custom(exists),
26 param('fps')
27 .optional()
28 .customSanitizer(toIntOrNull)
29 .custom(exists),
30
31 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
32 if (areValidationErrors(req, res)) return
33 if (!await doesVideoExist(req.params.videoId, res)) return
34
35 const video = res.locals.videoAll
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
40 const videoFile = video.VideoFiles.find(f => {
41 return f.resolution === paramResolution && (!req.params.fps || paramFPS)
42 })
43
44 if (!videoFile) {
45 return res.fail({
46 status: HttpStatusCode.NOT_FOUND_404,
47 message: 'Video file not found.'
48 })
49 }
50 res.locals.videoFile = videoFile
51
52 const videoRedundancy = await VideoRedundancyModel.loadLocalByFileId(videoFile.id)
53 if (!videoRedundancy) {
54 return res.fail({
55 status: HttpStatusCode.NOT_FOUND_404,
56 message: 'Video redundancy not found.'
57 })
58 }
59 res.locals.videoRedundancy = videoRedundancy
60
61 return next()
62 }
63]
64
65const videoPlaylistRedundancyGetValidator = [
66 isValidVideoIdParam('videoId'),
67
68 param('streamingPlaylistType')
69 .customSanitizer(toIntOrNull)
70 .custom(exists),
71
72 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
73 if (areValidationErrors(req, res)) return
74 if (!await doesVideoExist(req.params.videoId, res)) return
75
76 const video = res.locals.videoAll
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)
80
81 if (!videoStreamingPlaylist) {
82 return res.fail({
83 status: HttpStatusCode.NOT_FOUND_404,
84 message: 'Video playlist not found.'
85 })
86 }
87 res.locals.videoStreamingPlaylist = videoStreamingPlaylist
88
89 const videoRedundancy = await VideoRedundancyModel.loadLocalByStreamingPlaylistId(videoStreamingPlaylist.id)
90 if (!videoRedundancy) {
91 return res.fail({
92 status: HttpStatusCode.NOT_FOUND_404,
93 message: 'Video redundancy not found.'
94 })
95 }
96 res.locals.videoRedundancy = videoRedundancy
97
98 return next()
99 }
100]
101
102const updateServerRedundancyValidator = [
103 param('host')
104 .custom(isHostValid),
105
106 body('redundancyAllowed')
107 .customSanitizer(toBooleanOrNull)
108 .custom(isBooleanValid).withMessage('Should have a valid redundancyAllowed boolean'),
109
110 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
111 if (areValidationErrors(req, res)) return
112
113 const server = await ServerModel.loadByHost(req.params.host)
114
115 if (!server) {
116 return res.fail({
117 status: HttpStatusCode.NOT_FOUND_404,
118 message: `Server ${req.params.host} not found.`
119 })
120 }
121
122 res.locals.server = server
123 return next()
124 }
125]
126
127const listVideoRedundanciesValidator = [
128 query('target')
129 .custom(isVideoRedundancyTarget),
130
131 (req: express.Request, res: express.Response, next: express.NextFunction) => {
132 if (areValidationErrors(req, res)) return
133
134 return next()
135 }
136]
137
138const addVideoRedundancyValidator = [
139 body('videoId')
140 .customSanitizer(toCompleteUUID)
141 .custom(isIdOrUUIDValid),
142
143 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
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) {
149 return res.fail({ message: 'Cannot create a redundancy on a local video' })
150 }
151
152 if (res.locals.onlyVideo.isLive) {
153 return res.fail({ message: 'Cannot create a redundancy of a live video' })
154 }
155
156 const alreadyExists = await VideoRedundancyModel.isLocalByVideoUUIDExists(res.locals.onlyVideo.uuid)
157 if (alreadyExists) {
158 return res.fail({
159 status: HttpStatusCode.CONFLICT_409,
160 message: 'This video is already duplicated by your instance.'
161 })
162 }
163
164 return next()
165 }
166]
167
168const removeVideoRedundancyValidator = [
169 param('redundancyId')
170 .custom(isIdValid),
171
172 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
173 if (areValidationErrors(req, res)) return
174
175 const redundancy = await VideoRedundancyModel.loadByIdWithVideo(forceNumber(req.params.redundancyId))
176 if (!redundancy) {
177 return res.fail({
178 status: HttpStatusCode.NOT_FOUND_404,
179 message: 'Video redundancy not found'
180 })
181 }
182
183 res.locals.videoRedundancy = redundancy
184
185 return next()
186 }
187]
188
189// ---------------------------------------------------------------------------
190
191export {
192 videoFileRedundancyGetValidator,
193 videoPlaylistRedundancyGetValidator,
194 updateServerRedundancyValidator,
195 listVideoRedundanciesValidator,
196 addVideoRedundancyValidator,
197 removeVideoRedundancyValidator
198}