aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/custom-validators
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-11-23 17:53:38 +0100
committerChocobozzz <florian.bigard@gmail.com>2017-11-27 19:40:53 +0100
commit8d468a16fd33ec2660c3c59b3f7def53eb0cc4a1 (patch)
tree078708f8316ed6294088b159f79e861664fca953 /server/helpers/custom-validators
parent39445ead45aaaea801ec09991b8dd2464f722e47 (diff)
downloadPeerTube-8d468a16fd33ec2660c3c59b3f7def53eb0cc4a1.tar.gz
PeerTube-8d468a16fd33ec2660c3c59b3f7def53eb0cc4a1.tar.zst
PeerTube-8d468a16fd33ec2660c3c59b3f7def53eb0cc4a1.zip
Cleanup helpers
Diffstat (limited to 'server/helpers/custom-validators')
-rw-r--r--server/helpers/custom-validators/accounts.ts7
-rw-r--r--server/helpers/custom-validators/video-channels.ts5
-rw-r--r--server/helpers/custom-validators/videos.ts113
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
17function isAccountNameWithHostValid (value: string) {
18 const [ name, host ] = value.split('@')
19
20 return isAccountNameValid(name) && isHostValid(host)
21}
22
23function checkVideoAccountExists (id: string, res: express.Response, callback: () => void) { 17function 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
49export { 43export {
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
27function isVideoChannelUUIDValid (value: string) {
28 return exists(value) && validator.isUUID('' + value, 4)
29}
30
31function checkVideoChannelExists (id: string, res: express.Response, callback: () => void) { 27function 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: (
57export { 53export {
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 @@
1import * as Promise from 'bluebird' 1import * as Bluebird from 'bluebird'
2import * as express from 'express' 2import { Response } from 'express'
3import 'express-validator' 3import 'express-validator'
4import { values } from 'lodash' 4import { values } from 'lodash'
5import 'multer' 5import 'multer'
6import * as validator from 'validator' 6import * as validator from 'validator'
7import { VideoRateType } from '../../../shared' 7import { VideoRateType } from '../../../shared'
8import { logger } from '../../helpers' 8import { CONSTRAINTS_FIELDS, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_RATE_TYPES } from '../../initializers'
9import { 9import { database as db } from '../../initializers/database'
10 CONSTRAINTS_FIELDS, 10import { VideoInstance } from '../../models/video/video-interface'
11 database as db, 11import { logger } from '../logger'
12 VIDEO_CATEGORIES,
13 VIDEO_LANGUAGES,
14 VIDEO_LICENCES,
15 VIDEO_PRIVACIES,
16 VIDEO_RATE_TYPES
17} from '../../initializers'
18import { VideoInstance } from '../../models'
19import { isActivityPubUrlValid } from './activitypub/misc' 12import { isActivityPubUrlValid } from './activitypub/misc'
20import { exists, isArray } from './misc' 13import { exists, isArray } from './misc'
14import { VIDEO_PRIVACIES } from '../../initializers/constants'
21 15
22const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS 16const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS
23const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES 17const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES
24const VIDEO_EVENTS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_EVENTS
25 18
26function isVideoCategoryValid (value: number) { 19function 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
31function isRemoteVideoCategoryValid (value: string) {
32 return validator.isInt('' + value)
33}
34
35function isVideoUrlValid (value: string) { 23function 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
43function 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
48function isRemoteVideoPrivacyValid (value: string) {
49 return validator.isInt('' + value)
50}
51
52// Maybe we don't know the remote licence, but that doesn't matter
53function isRemoteVideoLicenceValid (value: string) {
54 return validator.isInt('' + value)
55}
56
57function isVideoLanguageValid (value: number) { 31function 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
62function isRemoteVideoLanguageValid (value: string) {
63 return validator.isInt('' + value)
64}
65
66function isVideoNSFWValid (value: any) { 35function 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
96function isVideoThumbnailValid (value: string) {
97 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL)
98}
99
100function isVideoThumbnailDataValid (value: string) {
101 return exists(value) && validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL_DATA)
102}
103
104function isVideoAbuseReasonValid (value: string) { 65function 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
112function isVideoLikesValid (value: string) {
113 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.LIKES)
114}
115
116function isVideoDislikesValid (value: string) {
117 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DISLIKES)
118}
119
120function isVideoEventCountValid (value: string) {
121 return exists(value) && validator.isInt(value + '', VIDEO_EVENTS_CONSTRAINTS_FIELDS.COUNT)
122}
123
124function isVideoRatingTypeValid (value: string) { 73function 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
144function isVideoFileSizeValid (value: string) {
145 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.FILE_SIZE)
146}
147
148function isVideoFileResolutionValid (value: string) {
149 return exists(value) && validator.isInt(value + '')
150}
151
152function isVideoFileExtnameValid (value: string) {
153 return VIDEOS_CONSTRAINTS_FIELDS.EXTNAME.indexOf(value) !== -1
154}
155
156function isVideoFileInfoHashValid (value: string) { 93function 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
160function checkVideoExists (id: string, res: express.Response, callback: () => void) { 97function isVideoPrivacyValid (value: string) {
161 let promise: Promise<VideoInstance> 98 return VIDEO_PRIVACIES[value] !== undefined
99}
100
101function 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}