diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-01-12 15:20:03 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-01-12 15:20:03 +0100 |
commit | 99fe265a5fc077cb66c322e7f3d191ff7110aea0 (patch) | |
tree | c9e04ccfcc5496d2300d7c26db5833e494b4cdad /server/helpers/custom-validators | |
parent | fcc5f77b95d330bfcb439c172b7fcc58f3162e4d (diff) | |
parent | 91cc839af88730ba55f84997c56b85ea100070a7 (diff) | |
download | PeerTube-99fe265a5fc077cb66c322e7f3d191ff7110aea0.tar.gz PeerTube-99fe265a5fc077cb66c322e7f3d191ff7110aea0.tar.zst PeerTube-99fe265a5fc077cb66c322e7f3d191ff7110aea0.zip |
Merge branch 'postgresql'
Diffstat (limited to 'server/helpers/custom-validators')
-rw-r--r-- | server/helpers/custom-validators/index.js | 2 | ||||
-rw-r--r-- | server/helpers/custom-validators/pods.js | 9 | ||||
-rw-r--r-- | server/helpers/custom-validators/remote/index.js | 11 | ||||
-rw-r--r-- | server/helpers/custom-validators/remote/videos.js | 73 | ||||
-rw-r--r-- | server/helpers/custom-validators/videos.js | 71 |
5 files changed, 116 insertions, 50 deletions
diff --git a/server/helpers/custom-validators/index.js b/server/helpers/custom-validators/index.js index 96b5b20b9..9383e0304 100644 --- a/server/helpers/custom-validators/index.js +++ b/server/helpers/custom-validators/index.js | |||
@@ -2,12 +2,14 @@ | |||
2 | 2 | ||
3 | const miscValidators = require('./misc') | 3 | const miscValidators = require('./misc') |
4 | const podsValidators = require('./pods') | 4 | const podsValidators = require('./pods') |
5 | const remoteValidators = require('./remote') | ||
5 | const usersValidators = require('./users') | 6 | const usersValidators = require('./users') |
6 | const videosValidators = require('./videos') | 7 | const videosValidators = require('./videos') |
7 | 8 | ||
8 | const validators = { | 9 | const validators = { |
9 | misc: miscValidators, | 10 | misc: miscValidators, |
10 | pods: podsValidators, | 11 | pods: podsValidators, |
12 | remote: remoteValidators, | ||
11 | users: usersValidators, | 13 | users: usersValidators, |
12 | videos: videosValidators | 14 | videos: videosValidators |
13 | } | 15 | } |
diff --git a/server/helpers/custom-validators/pods.js b/server/helpers/custom-validators/pods.js index 0154a2424..8bb3733ff 100644 --- a/server/helpers/custom-validators/pods.js +++ b/server/helpers/custom-validators/pods.js | |||
@@ -5,14 +5,19 @@ const validator = require('express-validator').validator | |||
5 | const miscValidators = require('./misc') | 5 | const miscValidators = require('./misc') |
6 | 6 | ||
7 | const podsValidators = { | 7 | const podsValidators = { |
8 | isEachUniqueHostValid | 8 | isEachUniqueHostValid, |
9 | isHostValid | ||
10 | } | ||
11 | |||
12 | function isHostValid (host) { | ||
13 | return validator.isURL(host) && host.split('://').length === 1 | ||
9 | } | 14 | } |
10 | 15 | ||
11 | function isEachUniqueHostValid (hosts) { | 16 | function isEachUniqueHostValid (hosts) { |
12 | return miscValidators.isArray(hosts) && | 17 | return miscValidators.isArray(hosts) && |
13 | hosts.length !== 0 && | 18 | hosts.length !== 0 && |
14 | hosts.every(function (host) { | 19 | hosts.every(function (host) { |
15 | return validator.isURL(host) && host.split('://').length === 1 && hosts.indexOf(host) === hosts.lastIndexOf(host) | 20 | return isHostValid(host) && hosts.indexOf(host) === hosts.lastIndexOf(host) |
16 | }) | 21 | }) |
17 | } | 22 | } |
18 | 23 | ||
diff --git a/server/helpers/custom-validators/remote/index.js b/server/helpers/custom-validators/remote/index.js new file mode 100644 index 000000000..1939a95f4 --- /dev/null +++ b/server/helpers/custom-validators/remote/index.js | |||
@@ -0,0 +1,11 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const remoteVideosValidators = require('./videos') | ||
4 | |||
5 | const validators = { | ||
6 | videos: remoteVideosValidators | ||
7 | } | ||
8 | |||
9 | // --------------------------------------------------------------------------- | ||
10 | |||
11 | module.exports = validators | ||
diff --git a/server/helpers/custom-validators/remote/videos.js b/server/helpers/custom-validators/remote/videos.js new file mode 100644 index 000000000..7c27b9dbb --- /dev/null +++ b/server/helpers/custom-validators/remote/videos.js | |||
@@ -0,0 +1,73 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const videosValidators = require('../videos') | ||
4 | const miscValidators = require('../misc') | ||
5 | |||
6 | const remoteVideosValidators = { | ||
7 | isEachRemoteRequestVideosValid | ||
8 | } | ||
9 | |||
10 | function isEachRemoteRequestVideosValid (requests) { | ||
11 | return miscValidators.isArray(requests) && | ||
12 | requests.every(function (request) { | ||
13 | const video = request.data | ||
14 | return ( | ||
15 | isRequestTypeAddValid(request.type) && | ||
16 | videosValidators.isVideoAuthorValid(video.author) && | ||
17 | videosValidators.isVideoDateValid(video.createdAt) && | ||
18 | videosValidators.isVideoDateValid(video.updatedAt) && | ||
19 | videosValidators.isVideoDescriptionValid(video.description) && | ||
20 | videosValidators.isVideoDurationValid(video.duration) && | ||
21 | videosValidators.isVideoInfoHashValid(video.infoHash) && | ||
22 | videosValidators.isVideoNameValid(video.name) && | ||
23 | videosValidators.isVideoTagsValid(video.tags) && | ||
24 | videosValidators.isVideoThumbnailDataValid(video.thumbnailData) && | ||
25 | videosValidators.isVideoRemoteIdValid(video.remoteId) && | ||
26 | videosValidators.isVideoExtnameValid(video.extname) | ||
27 | ) || | ||
28 | ( | ||
29 | isRequestTypeUpdateValid(request.type) && | ||
30 | videosValidators.isVideoDateValid(video.createdAt) && | ||
31 | videosValidators.isVideoDateValid(video.updatedAt) && | ||
32 | videosValidators.isVideoDescriptionValid(video.description) && | ||
33 | videosValidators.isVideoDurationValid(video.duration) && | ||
34 | videosValidators.isVideoInfoHashValid(video.infoHash) && | ||
35 | videosValidators.isVideoNameValid(video.name) && | ||
36 | videosValidators.isVideoTagsValid(video.tags) && | ||
37 | videosValidators.isVideoRemoteIdValid(video.remoteId) && | ||
38 | videosValidators.isVideoExtnameValid(video.extname) | ||
39 | ) || | ||
40 | ( | ||
41 | isRequestTypeRemoveValid(request.type) && | ||
42 | videosValidators.isVideoRemoteIdValid(video.remoteId) | ||
43 | ) || | ||
44 | ( | ||
45 | isRequestTypeReportAbuseValid(request.type) && | ||
46 | videosValidators.isVideoRemoteIdValid(request.data.videoRemoteId) && | ||
47 | videosValidators.isVideoAbuseReasonValid(request.data.reportReason) && | ||
48 | videosValidators.isVideoAbuseReporterUsernameValid(request.data.reporterUsername) | ||
49 | ) | ||
50 | }) | ||
51 | } | ||
52 | |||
53 | // --------------------------------------------------------------------------- | ||
54 | |||
55 | module.exports = remoteVideosValidators | ||
56 | |||
57 | // --------------------------------------------------------------------------- | ||
58 | |||
59 | function isRequestTypeAddValid (value) { | ||
60 | return value === 'add' | ||
61 | } | ||
62 | |||
63 | function isRequestTypeUpdateValid (value) { | ||
64 | return value === 'update' | ||
65 | } | ||
66 | |||
67 | function isRequestTypeRemoveValid (value) { | ||
68 | return value === 'remove' | ||
69 | } | ||
70 | |||
71 | function isRequestTypeReportAbuseValid (value) { | ||
72 | return value === 'report-abuse' | ||
73 | } | ||
diff --git a/server/helpers/custom-validators/videos.js b/server/helpers/custom-validators/videos.js index 1a7753265..7f727854d 100644 --- a/server/helpers/custom-validators/videos.js +++ b/server/helpers/custom-validators/videos.js | |||
@@ -6,43 +6,22 @@ const constants = require('../../initializers/constants') | |||
6 | const usersValidators = require('./users') | 6 | const usersValidators = require('./users') |
7 | const miscValidators = require('./misc') | 7 | const miscValidators = require('./misc') |
8 | const VIDEOS_CONSTRAINTS_FIELDS = constants.CONSTRAINTS_FIELDS.VIDEOS | 8 | const VIDEOS_CONSTRAINTS_FIELDS = constants.CONSTRAINTS_FIELDS.VIDEOS |
9 | const VIDEO_ABUSES_CONSTRAINTS_FIELDS = constants.CONSTRAINTS_FIELDS.VIDEO_ABUSES | ||
9 | 10 | ||
10 | const videosValidators = { | 11 | const videosValidators = { |
11 | isEachRemoteVideosValid, | ||
12 | isVideoAuthorValid, | 12 | isVideoAuthorValid, |
13 | isVideoDateValid, | 13 | isVideoDateValid, |
14 | isVideoDescriptionValid, | 14 | isVideoDescriptionValid, |
15 | isVideoDurationValid, | 15 | isVideoDurationValid, |
16 | isVideoMagnetValid, | 16 | isVideoInfoHashValid, |
17 | isVideoNameValid, | 17 | isVideoNameValid, |
18 | isVideoPodHostValid, | ||
19 | isVideoTagsValid, | 18 | isVideoTagsValid, |
20 | isVideoThumbnailValid, | 19 | isVideoThumbnailValid, |
21 | isVideoThumbnail64Valid | 20 | isVideoThumbnailDataValid, |
22 | } | 21 | isVideoExtnameValid, |
23 | 22 | isVideoRemoteIdValid, | |
24 | function isEachRemoteVideosValid (requests) { | 23 | isVideoAbuseReasonValid, |
25 | return miscValidators.isArray(requests) && | 24 | isVideoAbuseReporterUsernameValid |
26 | requests.every(function (request) { | ||
27 | const video = request.data | ||
28 | return ( | ||
29 | isRequestTypeAddValid(request.type) && | ||
30 | isVideoAuthorValid(video.author) && | ||
31 | isVideoDateValid(video.createdDate) && | ||
32 | isVideoDescriptionValid(video.description) && | ||
33 | isVideoDurationValid(video.duration) && | ||
34 | isVideoMagnetValid(video.magnet) && | ||
35 | isVideoNameValid(video.name) && | ||
36 | isVideoTagsValid(video.tags) && | ||
37 | isVideoThumbnail64Valid(video.thumbnailBase64) && | ||
38 | isVideoRemoteIdValid(video.remoteId) | ||
39 | ) || | ||
40 | ( | ||
41 | isRequestTypeRemoveValid(request.type) && | ||
42 | isVideoNameValid(video.name) && | ||
43 | isVideoRemoteIdValid(video.remoteId) | ||
44 | ) | ||
45 | }) | ||
46 | } | 25 | } |
47 | 26 | ||
48 | function isVideoAuthorValid (value) { | 27 | function isVideoAuthorValid (value) { |
@@ -61,17 +40,16 @@ function isVideoDurationValid (value) { | |||
61 | return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION) | 40 | return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION) |
62 | } | 41 | } |
63 | 42 | ||
64 | function isVideoMagnetValid (value) { | 43 | function isVideoExtnameValid (value) { |
65 | return validator.isLength(value.infoHash, VIDEOS_CONSTRAINTS_FIELDS.MAGNET.INFO_HASH) | 44 | return VIDEOS_CONSTRAINTS_FIELDS.EXTNAME.indexOf(value) !== -1 |
66 | } | 45 | } |
67 | 46 | ||
68 | function isVideoNameValid (value) { | 47 | function isVideoInfoHashValid (value) { |
69 | return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME) | 48 | return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH) |
70 | } | 49 | } |
71 | 50 | ||
72 | function isVideoPodHostValid (value) { | 51 | function isVideoNameValid (value) { |
73 | // TODO: set options (TLD...) | 52 | return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME) |
74 | return validator.isURL(value) | ||
75 | } | 53 | } |
76 | 54 | ||
77 | function isVideoTagsValid (tags) { | 55 | function isVideoTagsValid (tags) { |
@@ -87,25 +65,22 @@ function isVideoThumbnailValid (value) { | |||
87 | return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL) | 65 | return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL) |
88 | } | 66 | } |
89 | 67 | ||
90 | function isVideoThumbnail64Valid (value) { | 68 | function isVideoThumbnailDataValid (value) { |
91 | return validator.isBase64(value) && | 69 | return validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL_DATA) |
92 | validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL64) | ||
93 | } | 70 | } |
94 | 71 | ||
95 | function isVideoRemoteIdValid (value) { | 72 | function isVideoRemoteIdValid (value) { |
96 | return validator.isMongoId(value) | 73 | return validator.isUUID(value, 4) |
97 | } | 74 | } |
98 | 75 | ||
99 | // --------------------------------------------------------------------------- | 76 | function isVideoAbuseReasonValid (value) { |
77 | return validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.REASON) | ||
78 | } | ||
100 | 79 | ||
101 | module.exports = videosValidators | 80 | function isVideoAbuseReporterUsernameValid (value) { |
81 | return usersValidators.isUserUsernameValid(value) | ||
82 | } | ||
102 | 83 | ||
103 | // --------------------------------------------------------------------------- | 84 | // --------------------------------------------------------------------------- |
104 | 85 | ||
105 | function isRequestTypeAddValid (value) { | 86 | module.exports = videosValidators |
106 | return value === 'add' | ||
107 | } | ||
108 | |||
109 | function isRequestTypeRemoveValid (value) { | ||
110 | return value === 'remove' | ||
111 | } | ||