diff options
Diffstat (limited to 'server/helpers/custom-validators')
-rw-r--r-- | server/helpers/custom-validators/accounts.ts | 7 | ||||
-rw-r--r-- | server/helpers/custom-validators/video-channels.ts | 5 | ||||
-rw-r--r-- | server/helpers/custom-validators/videos.ts | 113 |
3 files changed, 21 insertions, 104 deletions
diff --git a/server/helpers/custom-validators/accounts.ts b/server/helpers/custom-validators/accounts.ts index 088d5fc8f..83540e545 100644 --- a/server/helpers/custom-validators/accounts.ts +++ b/server/helpers/custom-validators/accounts.ts | |||
@@ -14,12 +14,6 @@ function isAccountNameValid (value: string) { | |||
14 | return isUserUsernameValid(value) | 14 | return isUserUsernameValid(value) |
15 | } | 15 | } |
16 | 16 | ||
17 | function isAccountNameWithHostValid (value: string) { | ||
18 | const [ name, host ] = value.split('@') | ||
19 | |||
20 | return isAccountNameValid(name) && isHostValid(host) | ||
21 | } | ||
22 | |||
23 | function checkVideoAccountExists (id: string, res: express.Response, callback: () => void) { | 17 | function checkVideoAccountExists (id: string, res: express.Response, callback: () => void) { |
24 | let promise: Promise<AccountInstance> | 18 | let promise: Promise<AccountInstance> |
25 | if (validator.isInt(id)) { | 19 | if (validator.isInt(id)) { |
@@ -48,6 +42,5 @@ function checkVideoAccountExists (id: string, res: express.Response, callback: ( | |||
48 | 42 | ||
49 | export { | 43 | export { |
50 | checkVideoAccountExists, | 44 | checkVideoAccountExists, |
51 | isAccountNameWithHostValid, | ||
52 | isAccountNameValid | 45 | isAccountNameValid |
53 | } | 46 | } |
diff --git a/server/helpers/custom-validators/video-channels.ts b/server/helpers/custom-validators/video-channels.ts index 5787c3850..5de01f74b 100644 --- a/server/helpers/custom-validators/video-channels.ts +++ b/server/helpers/custom-validators/video-channels.ts | |||
@@ -24,10 +24,6 @@ function isVideoChannelNameValid (value: string) { | |||
24 | return exists(value) && validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.NAME) | 24 | return exists(value) && validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.NAME) |
25 | } | 25 | } |
26 | 26 | ||
27 | function isVideoChannelUUIDValid (value: string) { | ||
28 | return exists(value) && validator.isUUID('' + value, 4) | ||
29 | } | ||
30 | |||
31 | function checkVideoChannelExists (id: string, res: express.Response, callback: () => void) { | 27 | function checkVideoChannelExists (id: string, res: express.Response, callback: () => void) { |
32 | let promise: Promise<VideoChannelInstance> | 28 | let promise: Promise<VideoChannelInstance> |
33 | if (validator.isInt(id)) { | 29 | if (validator.isInt(id)) { |
@@ -57,7 +53,6 @@ function checkVideoChannelExists (id: string, res: express.Response, callback: ( | |||
57 | export { | 53 | export { |
58 | isVideoChannelDescriptionValid, | 54 | isVideoChannelDescriptionValid, |
59 | isVideoChannelNameValid, | 55 | isVideoChannelNameValid, |
60 | isVideoChannelUUIDValid, | ||
61 | checkVideoChannelExists, | 56 | checkVideoChannelExists, |
62 | isVideoChannelUrlValid | 57 | isVideoChannelUrlValid |
63 | } | 58 | } |
diff --git a/server/helpers/custom-validators/videos.ts b/server/helpers/custom-validators/videos.ts index 07aadadb1..c893d2c7c 100644 --- a/server/helpers/custom-validators/videos.ts +++ b/server/helpers/custom-validators/videos.ts | |||
@@ -1,37 +1,25 @@ | |||
1 | import * as Promise from 'bluebird' | 1 | import * as Bluebird from 'bluebird' |
2 | import * as express from 'express' | 2 | import { Response } from 'express' |
3 | import 'express-validator' | 3 | import 'express-validator' |
4 | import { values } from 'lodash' | 4 | import { values } from 'lodash' |
5 | import 'multer' | 5 | import 'multer' |
6 | import * as validator from 'validator' | 6 | import * as validator from 'validator' |
7 | import { VideoRateType } from '../../../shared' | 7 | import { VideoRateType } from '../../../shared' |
8 | import { logger } from '../../helpers' | 8 | import { CONSTRAINTS_FIELDS, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_RATE_TYPES } from '../../initializers' |
9 | import { | 9 | import { database as db } from '../../initializers/database' |
10 | CONSTRAINTS_FIELDS, | 10 | import { VideoInstance } from '../../models/video/video-interface' |
11 | database as db, | 11 | import { logger } from '../logger' |
12 | VIDEO_CATEGORIES, | ||
13 | VIDEO_LANGUAGES, | ||
14 | VIDEO_LICENCES, | ||
15 | VIDEO_PRIVACIES, | ||
16 | VIDEO_RATE_TYPES | ||
17 | } from '../../initializers' | ||
18 | import { VideoInstance } from '../../models' | ||
19 | import { isActivityPubUrlValid } from './activitypub/misc' | 12 | import { isActivityPubUrlValid } from './activitypub/misc' |
20 | import { exists, isArray } from './misc' | 13 | import { exists, isArray } from './misc' |
14 | import { VIDEO_PRIVACIES } from '../../initializers/constants' | ||
21 | 15 | ||
22 | const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS | 16 | const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS |
23 | const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES | 17 | const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES |
24 | const VIDEO_EVENTS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_EVENTS | ||
25 | 18 | ||
26 | function isVideoCategoryValid (value: number) { | 19 | function isVideoCategoryValid (value: number) { |
27 | return VIDEO_CATEGORIES[value] !== undefined | 20 | return VIDEO_CATEGORIES[value] !== undefined |
28 | } | 21 | } |
29 | 22 | ||
30 | // Maybe we don't know the remote category, but that doesn't matter | ||
31 | function isRemoteVideoCategoryValid (value: string) { | ||
32 | return validator.isInt('' + value) | ||
33 | } | ||
34 | |||
35 | function isVideoUrlValid (value: string) { | 23 | function isVideoUrlValid (value: string) { |
36 | return isActivityPubUrlValid(value) | 24 | return isActivityPubUrlValid(value) |
37 | } | 25 | } |
@@ -40,29 +28,10 @@ function isVideoLicenceValid (value: number) { | |||
40 | return VIDEO_LICENCES[value] !== undefined | 28 | return VIDEO_LICENCES[value] !== undefined |
41 | } | 29 | } |
42 | 30 | ||
43 | function isVideoPrivacyValid (value: string) { | ||
44 | return VIDEO_PRIVACIES[value] !== undefined | ||
45 | } | ||
46 | |||
47 | // Maybe we don't know the remote privacy setting, but that doesn't matter | ||
48 | function isRemoteVideoPrivacyValid (value: string) { | ||
49 | return validator.isInt('' + value) | ||
50 | } | ||
51 | |||
52 | // Maybe we don't know the remote licence, but that doesn't matter | ||
53 | function isRemoteVideoLicenceValid (value: string) { | ||
54 | return validator.isInt('' + value) | ||
55 | } | ||
56 | |||
57 | function isVideoLanguageValid (value: number) { | 31 | function isVideoLanguageValid (value: number) { |
58 | return value === null || VIDEO_LANGUAGES[value] !== undefined | 32 | return value === null || VIDEO_LANGUAGES[value] !== undefined |
59 | } | 33 | } |
60 | 34 | ||
61 | // Maybe we don't know the remote language, but that doesn't matter | ||
62 | function isRemoteVideoLanguageValid (value: string) { | ||
63 | return validator.isInt('' + value) | ||
64 | } | ||
65 | |||
66 | function isVideoNSFWValid (value: any) { | 35 | function isVideoNSFWValid (value: any) { |
67 | return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value)) | 36 | return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value)) |
68 | } | 37 | } |
@@ -93,14 +62,6 @@ function isVideoTagsValid (tags: string[]) { | |||
93 | tags.every(tag => isVideoTagValid(tag)) | 62 | tags.every(tag => isVideoTagValid(tag)) |
94 | } | 63 | } |
95 | 64 | ||
96 | function isVideoThumbnailValid (value: string) { | ||
97 | return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL) | ||
98 | } | ||
99 | |||
100 | function isVideoThumbnailDataValid (value: string) { | ||
101 | return exists(value) && validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL_DATA) | ||
102 | } | ||
103 | |||
104 | function isVideoAbuseReasonValid (value: string) { | 65 | function isVideoAbuseReasonValid (value: string) { |
105 | return exists(value) && validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.REASON) | 66 | return exists(value) && validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.REASON) |
106 | } | 67 | } |
@@ -109,18 +70,6 @@ function isVideoViewsValid (value: string) { | |||
109 | return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS) | 70 | return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS) |
110 | } | 71 | } |
111 | 72 | ||
112 | function isVideoLikesValid (value: string) { | ||
113 | return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.LIKES) | ||
114 | } | ||
115 | |||
116 | function isVideoDislikesValid (value: string) { | ||
117 | return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DISLIKES) | ||
118 | } | ||
119 | |||
120 | function isVideoEventCountValid (value: string) { | ||
121 | return exists(value) && validator.isInt(value + '', VIDEO_EVENTS_CONSTRAINTS_FIELDS.COUNT) | ||
122 | } | ||
123 | |||
124 | function isVideoRatingTypeValid (value: string) { | 73 | function isVideoRatingTypeValid (value: string) { |
125 | return values(VIDEO_RATE_TYPES).indexOf(value as VideoRateType) !== -1 | 74 | return values(VIDEO_RATE_TYPES).indexOf(value as VideoRateType) !== -1 |
126 | } | 75 | } |
@@ -141,24 +90,16 @@ function isVideoFile (files: { [ fieldname: string ]: Express.Multer.File[] } | | |||
141 | return new RegExp('^video/(webm|mp4|ogg)$', 'i').test(file.mimetype) | 90 | return new RegExp('^video/(webm|mp4|ogg)$', 'i').test(file.mimetype) |
142 | } | 91 | } |
143 | 92 | ||
144 | function isVideoFileSizeValid (value: string) { | ||
145 | return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.FILE_SIZE) | ||
146 | } | ||
147 | |||
148 | function isVideoFileResolutionValid (value: string) { | ||
149 | return exists(value) && validator.isInt(value + '') | ||
150 | } | ||
151 | |||
152 | function isVideoFileExtnameValid (value: string) { | ||
153 | return VIDEOS_CONSTRAINTS_FIELDS.EXTNAME.indexOf(value) !== -1 | ||
154 | } | ||
155 | |||
156 | function isVideoFileInfoHashValid (value: string) { | 93 | function isVideoFileInfoHashValid (value: string) { |
157 | return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH) | 94 | return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH) |
158 | } | 95 | } |
159 | 96 | ||
160 | function checkVideoExists (id: string, res: express.Response, callback: () => void) { | 97 | function isVideoPrivacyValid (value: string) { |
161 | let promise: Promise<VideoInstance> | 98 | return VIDEO_PRIVACIES[value] !== undefined |
99 | } | ||
100 | |||
101 | function checkVideoExists (id: string, res: Response, callback: () => void) { | ||
102 | let promise: Bluebird<VideoInstance> | ||
162 | if (validator.isInt(id)) { | 103 | if (validator.isInt(id)) { |
163 | promise = db.Video.loadAndPopulateAccountAndServerAndTags(+id) | 104 | promise = db.Video.loadAndPopulateAccountAndServerAndTags(+id) |
164 | } else { // UUID | 105 | } else { // UUID |
@@ -168,17 +109,17 @@ function checkVideoExists (id: string, res: express.Response, callback: () => vo | |||
168 | promise.then(video => { | 109 | promise.then(video => { |
169 | if (!video) { | 110 | if (!video) { |
170 | return res.status(404) | 111 | return res.status(404) |
171 | .json({ error: 'Video not found' }) | 112 | .json({ error: 'Video not found' }) |
172 | .end() | 113 | .end() |
173 | } | 114 | } |
174 | 115 | ||
175 | res.locals.video = video | 116 | res.locals.video = video |
176 | callback() | 117 | callback() |
177 | }) | 118 | }) |
178 | .catch(err => { | 119 | .catch(err => { |
179 | logger.error('Error in video request validator.', err) | 120 | logger.error('Error in video request validator.', err) |
180 | return res.sendStatus(500) | 121 | return res.sendStatus(500) |
181 | }) | 122 | }) |
182 | } | 123 | } |
183 | 124 | ||
184 | // --------------------------------------------------------------------------- | 125 | // --------------------------------------------------------------------------- |
@@ -193,25 +134,13 @@ export { | |||
193 | isVideoFileInfoHashValid, | 134 | isVideoFileInfoHashValid, |
194 | isVideoNameValid, | 135 | isVideoNameValid, |
195 | isVideoTagsValid, | 136 | isVideoTagsValid, |
196 | isVideoThumbnailValid, | ||
197 | isVideoThumbnailDataValid, | ||
198 | isVideoFileExtnameValid, | ||
199 | isVideoAbuseReasonValid, | 137 | isVideoAbuseReasonValid, |
200 | isVideoFile, | 138 | isVideoFile, |
201 | isVideoViewsValid, | 139 | isVideoViewsValid, |
202 | isVideoLikesValid, | ||
203 | isVideoRatingTypeValid, | 140 | isVideoRatingTypeValid, |
204 | isVideoDislikesValid, | ||
205 | isVideoEventCountValid, | ||
206 | isVideoFileSizeValid, | ||
207 | isVideoPrivacyValid, | ||
208 | isRemoteVideoPrivacyValid, | ||
209 | isVideoDurationValid, | 141 | isVideoDurationValid, |
210 | isVideoFileResolutionValid, | ||
211 | checkVideoExists, | ||
212 | isVideoTagValid, | 142 | isVideoTagValid, |
213 | isRemoteVideoCategoryValid, | ||
214 | isRemoteVideoLicenceValid, | ||
215 | isVideoUrlValid, | 143 | isVideoUrlValid, |
216 | isRemoteVideoLanguageValid | 144 | isVideoPrivacyValid, |
145 | checkVideoExists | ||
217 | } | 146 | } |