diff options
Diffstat (limited to 'server/helpers/custom-validators/videos.js')
-rw-r--r-- | server/helpers/custom-validators/videos.js | 107 |
1 files changed, 107 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..a507ff686 --- /dev/null +++ b/server/helpers/custom-validators/videos.js | |||
@@ -0,0 +1,107 @@ | |||
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, | ||
12 | isVideoAuthorValid, | ||
13 | isVideoDateValid, | ||
14 | isVideoDescriptionValid, | ||
15 | isVideoDurationValid, | ||
16 | isVideoMagnetUriValid, | ||
17 | isVideoNameValid, | ||
18 | isVideoPodUrlValid, | ||
19 | isVideoTagsValid, | ||
20 | isVideoThumbnailValid, | ||
21 | isVideoThumbnail64Valid | ||
22 | } | ||
23 | |||
24 | function isEachRemoteVideosValid (requests) { | ||
25 | return miscValidators.isArray(requests) && | ||
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 | isVideoMagnetUriValid(video.magnetUri) && | ||
35 | isVideoNameValid(video.name) && | ||
36 | isVideoPodUrlValid(video.podUrl) && | ||
37 | isVideoTagsValid(video.tags) && | ||
38 | isVideoThumbnail64Valid(video.thumbnailBase64) | ||
39 | ) || | ||
40 | ( | ||
41 | isRequestTypeRemoveValid(request.type) && | ||
42 | isVideoNameValid(video.name) && | ||
43 | isVideoMagnetUriValid(video.magnetUri) | ||
44 | ) | ||
45 | }) | ||
46 | } | ||
47 | |||
48 | function isVideoAuthorValid (value) { | ||
49 | return usersValidators.isUserUsernameValid(value) | ||
50 | } | ||
51 | |||
52 | function isVideoDateValid (value) { | ||
53 | return validator.isDate(value) | ||
54 | } | ||
55 | |||
56 | function isVideoDescriptionValid (value) { | ||
57 | return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION) | ||
58 | } | ||
59 | |||
60 | function isVideoDurationValid (value) { | ||
61 | return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION) | ||
62 | } | ||
63 | |||
64 | function isVideoMagnetUriValid (value) { | ||
65 | return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.MAGNET_URI) | ||
66 | } | ||
67 | |||
68 | function isVideoNameValid (value) { | ||
69 | return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME) | ||
70 | } | ||
71 | |||
72 | function isVideoPodUrlValid (value) { | ||
73 | // TODO: set options (TLD...) | ||
74 | return validator.isURL(value) | ||
75 | } | ||
76 | |||
77 | function isVideoTagsValid (tags) { | ||
78 | return miscValidators.isArray(tags) && | ||
79 | validator.isInt(tags.length, VIDEOS_CONSTRAINTS_FIELDS.TAGS) && | ||
80 | tags.every(function (tag) { | ||
81 | return validator.isAlphanumeric(tag) && | ||
82 | validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG) | ||
83 | }) | ||
84 | } | ||
85 | |||
86 | function isVideoThumbnailValid (value) { | ||
87 | return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL) | ||
88 | } | ||
89 | |||
90 | function isVideoThumbnail64Valid (value) { | ||
91 | return validator.isBase64(value) && | ||
92 | validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL64) | ||
93 | } | ||
94 | |||
95 | // --------------------------------------------------------------------------- | ||
96 | |||
97 | module.exports = videosValidators | ||
98 | |||
99 | // --------------------------------------------------------------------------- | ||
100 | |||
101 | function isRequestTypeAddValid (value) { | ||
102 | return value === 'add' | ||
103 | } | ||
104 | |||
105 | function isRequestTypeRemoveValid (value) { | ||
106 | return value === 'remove' | ||
107 | } | ||