diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-11-23 17:53:38 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-11-27 19:40:53 +0100 |
commit | 8d468a16fd33ec2660c3c59b3f7def53eb0cc4a1 (patch) | |
tree | 078708f8316ed6294088b159f79e861664fca953 /server/helpers/custom-validators/videos.ts | |
parent | 39445ead45aaaea801ec09991b8dd2464f722e47 (diff) | |
download | PeerTube-8d468a16fd33ec2660c3c59b3f7def53eb0cc4a1.tar.gz PeerTube-8d468a16fd33ec2660c3c59b3f7def53eb0cc4a1.tar.zst PeerTube-8d468a16fd33ec2660c3c59b3f7def53eb0cc4a1.zip |
Cleanup helpers
Diffstat (limited to 'server/helpers/custom-validators/videos.ts')
-rw-r--r-- | server/helpers/custom-validators/videos.ts | 113 |
1 files changed, 21 insertions, 92 deletions
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 | } |