diff options
Diffstat (limited to 'server/helpers/custom-validators/videos.js')
-rw-r--r-- | server/helpers/custom-validators/videos.js | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/server/helpers/custom-validators/videos.js b/server/helpers/custom-validators/videos.js new file mode 100644 index 000000000..39a19cbd7 --- /dev/null +++ b/server/helpers/custom-validators/videos.js | |||
@@ -0,0 +1,106 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const validator = require('express-validator').validator | ||
4 | |||
5 | const constants = require('../../initializers/constants') | ||
6 | const usersValidators = require('./users') | ||
7 | const miscValidators = require('./misc') | ||
8 | const VIDEOS_CONSTRAINTS_FIELDS = constants.CONSTRAINTS_FIELDS.VIDEOS | ||
9 | |||
10 | const videosValidators = { | ||
11 | isEachRemoteVideosValid: isEachRemoteVideosValid, | ||
12 | isVideoAuthorValid: isVideoAuthorValid, | ||
13 | isVideoDateValid: isVideoDateValid, | ||
14 | isVideoDescriptionValid: isVideoDescriptionValid, | ||
15 | isVideoDurationValid: isVideoDurationValid, | ||
16 | isVideoMagnetUriValid: isVideoMagnetUriValid, | ||
17 | isVideoNameValid: isVideoNameValid, | ||
18 | isVideoPodUrlValid: isVideoPodUrlValid, | ||
19 | isVideoTagsValid: isVideoTagsValid, | ||
20 | isVideoThumbnailValid: isVideoThumbnailValid, | ||
21 | isVideoThumbnail64Valid: isVideoThumbnail64Valid | ||
22 | } | ||
23 | |||
24 | function isEachRemoteVideosValid (requests) { | ||
25 | return requests.every(function (request) { | ||
26 | const video = request.data | ||
27 | return ( | ||
28 | isRequestTypeAddValid(request.type) && | ||
29 | isVideoAuthorValid(video.author) && | ||
30 | isVideoDateValid(video.createdDate) && | ||
31 | isVideoDescriptionValid(video.description) && | ||
32 | isVideoDurationValid(video.duration) && | ||
33 | isVideoMagnetUriValid(video.magnetUri) && | ||
34 | isVideoNameValid(video.name) && | ||
35 | isVideoPodUrlValid(video.podUrl) && | ||
36 | isVideoTagsValid(video.tags) && | ||
37 | isVideoThumbnail64Valid(video.thumbnailBase64) | ||
38 | ) || | ||
39 | ( | ||
40 | isRequestTypeRemoveValid(request.type) && | ||
41 | isVideoNameValid(video.name) && | ||
42 | isVideoMagnetUriValid(video.magnetUri) | ||
43 | ) | ||
44 | }) | ||
45 | } | ||
46 | |||
47 | function isVideoAuthorValid (value) { | ||
48 | return usersValidators.isUserUsernameValid(usersValidators) | ||
49 | } | ||
50 | |||
51 | function isVideoDateValid (value) { | ||
52 | return validator.isDate(value) | ||
53 | } | ||
54 | |||
55 | function isVideoDescriptionValid (value) { | ||
56 | return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION) | ||
57 | } | ||
58 | |||
59 | function isVideoDurationValid (value) { | ||
60 | return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION) | ||
61 | } | ||
62 | |||
63 | function isVideoMagnetUriValid (value) { | ||
64 | return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.MAGNET_URI) | ||
65 | } | ||
66 | |||
67 | function isVideoNameValid (value) { | ||
68 | return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME) | ||
69 | } | ||
70 | |||
71 | function isVideoPodUrlValid (value) { | ||
72 | // TODO: set options (TLD...) | ||
73 | return validator.isURL(value) | ||
74 | } | ||
75 | |||
76 | function isVideoTagsValid (tags) { | ||
77 | return miscValidators.isArray(tags) && | ||
78 | validator.isInt(tags.length, VIDEOS_CONSTRAINTS_FIELDS.TAGS) && | ||
79 | tags.every(function (tag) { | ||
80 | return validator.isAlphanumeric(tag) && | ||
81 | validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG) | ||
82 | }) | ||
83 | } | ||
84 | |||
85 | function isVideoThumbnailValid (value) { | ||
86 | return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL) | ||
87 | } | ||
88 | |||
89 | function isVideoThumbnail64Valid (value) { | ||
90 | return validator.isBase64(value) && | ||
91 | validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL64) | ||
92 | } | ||
93 | |||
94 | // --------------------------------------------------------------------------- | ||
95 | |||
96 | module.exports = videosValidators | ||
97 | |||
98 | // --------------------------------------------------------------------------- | ||
99 | |||
100 | function isRequestTypeAddValid (value) { | ||
101 | return value === 'add' | ||
102 | } | ||
103 | |||
104 | function isRequestTypeRemoveValid (value) { | ||
105 | return value === 'remove' | ||
106 | } | ||