diff options
author | Chocobozzz <me@florianbigard.com> | 2023-07-31 14:34:36 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2023-08-11 15:02:33 +0200 |
commit | 3a4992633ee62d5edfbb484d9c6bcb3cf158489d (patch) | |
tree | e4510b39bdac9c318fdb4b47018d08f15368b8f0 /server/helpers/custom-validators/videos.ts | |
parent | 04d1da5621d25d59bd5fa1543b725c497bf5d9a8 (diff) | |
download | PeerTube-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/helpers/custom-validators/videos.ts')
-rw-r--r-- | server/helpers/custom-validators/videos.ts | 218 |
1 files changed, 0 insertions, 218 deletions
diff --git a/server/helpers/custom-validators/videos.ts b/server/helpers/custom-validators/videos.ts deleted file mode 100644 index 00c6deed4..000000000 --- a/server/helpers/custom-validators/videos.ts +++ /dev/null | |||
@@ -1,218 +0,0 @@ | |||
1 | import { Request, Response, UploadFilesForCheck } from 'express' | ||
2 | import { decode as magnetUriDecode } from 'magnet-uri' | ||
3 | import validator from 'validator' | ||
4 | import { getVideoWithAttributes } from '@server/helpers/video' | ||
5 | import { HttpStatusCode, VideoInclude, VideoPrivacy, VideoRateType } from '@shared/models' | ||
6 | import { | ||
7 | CONSTRAINTS_FIELDS, | ||
8 | MIMETYPES, | ||
9 | VIDEO_CATEGORIES, | ||
10 | VIDEO_LICENCES, | ||
11 | VIDEO_LIVE, | ||
12 | VIDEO_PRIVACIES, | ||
13 | VIDEO_RATE_TYPES, | ||
14 | VIDEO_STATES | ||
15 | } from '../../initializers/constants' | ||
16 | import { exists, isArray, isDateValid, isFileValid } from './misc' | ||
17 | |||
18 | const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS | ||
19 | |||
20 | function isVideoIncludeValid (include: VideoInclude) { | ||
21 | return exists(include) && validator.isInt('' + include) | ||
22 | } | ||
23 | |||
24 | function isVideoCategoryValid (value: any) { | ||
25 | return value === null || VIDEO_CATEGORIES[value] !== undefined | ||
26 | } | ||
27 | |||
28 | function isVideoStateValid (value: any) { | ||
29 | return exists(value) && VIDEO_STATES[value] !== undefined | ||
30 | } | ||
31 | |||
32 | function isVideoLicenceValid (value: any) { | ||
33 | return value === null || VIDEO_LICENCES[value] !== undefined | ||
34 | } | ||
35 | |||
36 | function isVideoLanguageValid (value: any) { | ||
37 | return value === null || | ||
38 | (typeof value === 'string' && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.LANGUAGE)) | ||
39 | } | ||
40 | |||
41 | function isVideoDurationValid (value: string) { | ||
42 | return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION) | ||
43 | } | ||
44 | |||
45 | function isVideoDescriptionValid (value: string) { | ||
46 | return value === null || (exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION)) | ||
47 | } | ||
48 | |||
49 | function isVideoSupportValid (value: string) { | ||
50 | return value === null || (exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.SUPPORT)) | ||
51 | } | ||
52 | |||
53 | function isVideoNameValid (value: string) { | ||
54 | return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME) | ||
55 | } | ||
56 | |||
57 | function isVideoTagValid (tag: string) { | ||
58 | return exists(tag) && validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG) | ||
59 | } | ||
60 | |||
61 | function areVideoTagsValid (tags: string[]) { | ||
62 | return tags === null || ( | ||
63 | isArray(tags) && | ||
64 | validator.isInt(tags.length.toString(), VIDEOS_CONSTRAINTS_FIELDS.TAGS) && | ||
65 | tags.every(tag => isVideoTagValid(tag)) | ||
66 | ) | ||
67 | } | ||
68 | |||
69 | function isVideoViewsValid (value: string) { | ||
70 | return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS) | ||
71 | } | ||
72 | |||
73 | const ratingTypes = new Set(Object.values(VIDEO_RATE_TYPES)) | ||
74 | function isVideoRatingTypeValid (value: string) { | ||
75 | return value === 'none' || ratingTypes.has(value as VideoRateType) | ||
76 | } | ||
77 | |||
78 | function isVideoFileExtnameValid (value: string) { | ||
79 | return exists(value) && (value === VIDEO_LIVE.EXTENSION || MIMETYPES.VIDEO.EXT_MIMETYPE[value] !== undefined) | ||
80 | } | ||
81 | |||
82 | function isVideoFileMimeTypeValid (files: UploadFilesForCheck, field = 'videofile') { | ||
83 | return isFileValid({ | ||
84 | files, | ||
85 | mimeTypeRegex: MIMETYPES.VIDEO.MIMETYPES_REGEX, | ||
86 | field, | ||
87 | maxSize: null | ||
88 | }) | ||
89 | } | ||
90 | |||
91 | const videoImageTypes = CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME | ||
92 | .map(v => v.replace('.', '')) | ||
93 | .join('|') | ||
94 | const videoImageTypesRegex = `image/(${videoImageTypes})` | ||
95 | |||
96 | function isVideoImageValid (files: UploadFilesForCheck, field: string, optional = true) { | ||
97 | return isFileValid({ | ||
98 | files, | ||
99 | mimeTypeRegex: videoImageTypesRegex, | ||
100 | field, | ||
101 | maxSize: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max, | ||
102 | optional | ||
103 | }) | ||
104 | } | ||
105 | |||
106 | function isVideoPrivacyValid (value: number) { | ||
107 | return VIDEO_PRIVACIES[value] !== undefined | ||
108 | } | ||
109 | |||
110 | function isVideoReplayPrivacyValid (value: number) { | ||
111 | return VIDEO_PRIVACIES[value] !== undefined && value !== VideoPrivacy.PASSWORD_PROTECTED | ||
112 | } | ||
113 | |||
114 | function isScheduleVideoUpdatePrivacyValid (value: number) { | ||
115 | return value === VideoPrivacy.UNLISTED || value === VideoPrivacy.PUBLIC || value === VideoPrivacy.INTERNAL | ||
116 | } | ||
117 | |||
118 | function isVideoOriginallyPublishedAtValid (value: string | null) { | ||
119 | return value === null || isDateValid(value) | ||
120 | } | ||
121 | |||
122 | function isVideoFileInfoHashValid (value: string | null | undefined) { | ||
123 | return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH) | ||
124 | } | ||
125 | |||
126 | function isVideoFileResolutionValid (value: string) { | ||
127 | return exists(value) && validator.isInt(value + '') | ||
128 | } | ||
129 | |||
130 | function isVideoFPSResolutionValid (value: string) { | ||
131 | return value === null || validator.isInt(value + '') | ||
132 | } | ||
133 | |||
134 | function isVideoFileSizeValid (value: string) { | ||
135 | return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.FILE_SIZE) | ||
136 | } | ||
137 | |||
138 | function isVideoMagnetUriValid (value: string) { | ||
139 | if (!exists(value)) return false | ||
140 | |||
141 | const parsed = magnetUriDecode(value) | ||
142 | return parsed && isVideoFileInfoHashValid(parsed.infoHash) | ||
143 | } | ||
144 | |||
145 | function isPasswordValid (password: string) { | ||
146 | return password.length >= CONSTRAINTS_FIELDS.VIDEO_PASSWORD.LENGTH.min && | ||
147 | password.length < CONSTRAINTS_FIELDS.VIDEO_PASSWORD.LENGTH.max | ||
148 | } | ||
149 | |||
150 | function isValidPasswordProtectedPrivacy (req: Request, res: Response) { | ||
151 | const fail = (message: string) => { | ||
152 | res.fail({ | ||
153 | status: HttpStatusCode.BAD_REQUEST_400, | ||
154 | message | ||
155 | }) | ||
156 | return false | ||
157 | } | ||
158 | |||
159 | let privacy: VideoPrivacy | ||
160 | const video = getVideoWithAttributes(res) | ||
161 | |||
162 | if (exists(req.body?.privacy)) privacy = req.body.privacy | ||
163 | else if (exists(video?.privacy)) privacy = video.privacy | ||
164 | |||
165 | if (privacy !== VideoPrivacy.PASSWORD_PROTECTED) return true | ||
166 | |||
167 | if (!exists(req.body.videoPasswords) && !exists(req.body.passwords)) return fail('Video passwords are missing.') | ||
168 | |||
169 | const passwords = req.body.videoPasswords || req.body.passwords | ||
170 | |||
171 | if (passwords.length === 0) return fail('At least one video password is required.') | ||
172 | |||
173 | if (new Set(passwords).size !== passwords.length) return fail('Duplicate video passwords are not allowed.') | ||
174 | |||
175 | for (const password of passwords) { | ||
176 | if (typeof password !== 'string') { | ||
177 | return fail('Video password should be a string.') | ||
178 | } | ||
179 | |||
180 | if (!isPasswordValid(password)) { | ||
181 | return fail('Invalid video password. Password length should be at least 2 characters and no more than 100 characters.') | ||
182 | } | ||
183 | } | ||
184 | |||
185 | return true | ||
186 | } | ||
187 | |||
188 | // --------------------------------------------------------------------------- | ||
189 | |||
190 | export { | ||
191 | isVideoCategoryValid, | ||
192 | isVideoLicenceValid, | ||
193 | isVideoLanguageValid, | ||
194 | isVideoDescriptionValid, | ||
195 | isVideoFileInfoHashValid, | ||
196 | isVideoNameValid, | ||
197 | areVideoTagsValid, | ||
198 | isVideoFPSResolutionValid, | ||
199 | isScheduleVideoUpdatePrivacyValid, | ||
200 | isVideoOriginallyPublishedAtValid, | ||
201 | isVideoMagnetUriValid, | ||
202 | isVideoStateValid, | ||
203 | isVideoIncludeValid, | ||
204 | isVideoViewsValid, | ||
205 | isVideoRatingTypeValid, | ||
206 | isVideoFileExtnameValid, | ||
207 | isVideoFileMimeTypeValid, | ||
208 | isVideoDurationValid, | ||
209 | isVideoTagValid, | ||
210 | isVideoPrivacyValid, | ||
211 | isVideoReplayPrivacyValid, | ||
212 | isVideoFileResolutionValid, | ||
213 | isVideoFileSizeValid, | ||
214 | isVideoImageValid, | ||
215 | isVideoSupportValid, | ||
216 | isPasswordValid, | ||
217 | isValidPasswordProtectedPrivacy | ||
218 | } | ||