diff options
Diffstat (limited to 'server/helpers/custom-validators/videos.ts')
-rw-r--r-- | server/helpers/custom-validators/videos.ts | 153 |
1 files changed, 153 insertions, 0 deletions
diff --git a/server/helpers/custom-validators/videos.ts b/server/helpers/custom-validators/videos.ts new file mode 100644 index 000000000..2b2370be4 --- /dev/null +++ b/server/helpers/custom-validators/videos.ts | |||
@@ -0,0 +1,153 @@ | |||
1 | import { values } from 'lodash' | ||
2 | import expressValidator = require('express-validator') | ||
3 | // TODO: use .validator when express-validator typing will have validator field | ||
4 | const validator = expressValidator['validator'] | ||
5 | |||
6 | import { | ||
7 | CONSTRAINTS_FIELDS, | ||
8 | VIDEO_CATEGORIES, | ||
9 | VIDEO_LICENCES, | ||
10 | VIDEO_LANGUAGES, | ||
11 | VIDEO_RATE_TYPES | ||
12 | } from '../../initializers' | ||
13 | import { isUserUsernameValid } from './users' | ||
14 | import { isArray } from './misc' | ||
15 | |||
16 | const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS | ||
17 | const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES | ||
18 | const VIDEO_EVENTS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_EVENTS | ||
19 | |||
20 | function isVideoAuthorValid (value) { | ||
21 | return isUserUsernameValid(value) | ||
22 | } | ||
23 | |||
24 | function isVideoDateValid (value) { | ||
25 | return validator.isDate(value) | ||
26 | } | ||
27 | |||
28 | function isVideoCategoryValid (value) { | ||
29 | return VIDEO_CATEGORIES[value] !== undefined | ||
30 | } | ||
31 | |||
32 | function isVideoLicenceValid (value) { | ||
33 | return VIDEO_LICENCES[value] !== undefined | ||
34 | } | ||
35 | |||
36 | function isVideoLanguageValid (value) { | ||
37 | return value === null || VIDEO_LANGUAGES[value] !== undefined | ||
38 | } | ||
39 | |||
40 | function isVideoNSFWValid (value) { | ||
41 | return validator.isBoolean(value) | ||
42 | } | ||
43 | |||
44 | function isVideoDescriptionValid (value) { | ||
45 | return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION) | ||
46 | } | ||
47 | |||
48 | function isVideoDurationValid (value) { | ||
49 | return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION) | ||
50 | } | ||
51 | |||
52 | function isVideoExtnameValid (value) { | ||
53 | return VIDEOS_CONSTRAINTS_FIELDS.EXTNAME.indexOf(value) !== -1 | ||
54 | } | ||
55 | |||
56 | function isVideoInfoHashValid (value) { | ||
57 | return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH) | ||
58 | } | ||
59 | |||
60 | function isVideoNameValid (value) { | ||
61 | return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME) | ||
62 | } | ||
63 | |||
64 | function isVideoTagsValid (tags) { | ||
65 | return isArray(tags) && | ||
66 | validator.isInt(tags.length, VIDEOS_CONSTRAINTS_FIELDS.TAGS) && | ||
67 | tags.every(function (tag) { | ||
68 | return validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG) | ||
69 | }) | ||
70 | } | ||
71 | |||
72 | function isVideoThumbnailValid (value) { | ||
73 | return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL) | ||
74 | } | ||
75 | |||
76 | function isVideoThumbnailDataValid (value) { | ||
77 | return validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL_DATA) | ||
78 | } | ||
79 | |||
80 | function isVideoRemoteIdValid (value) { | ||
81 | return validator.isUUID(value, 4) | ||
82 | } | ||
83 | |||
84 | function isVideoAbuseReasonValid (value) { | ||
85 | return validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.REASON) | ||
86 | } | ||
87 | |||
88 | function isVideoAbuseReporterUsernameValid (value) { | ||
89 | return isUserUsernameValid(value) | ||
90 | } | ||
91 | |||
92 | function isVideoViewsValid (value) { | ||
93 | return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS) | ||
94 | } | ||
95 | |||
96 | function isVideoLikesValid (value) { | ||
97 | return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.LIKES) | ||
98 | } | ||
99 | |||
100 | function isVideoDislikesValid (value) { | ||
101 | return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DISLIKES) | ||
102 | } | ||
103 | |||
104 | function isVideoEventCountValid (value) { | ||
105 | return validator.isInt(value + '', VIDEO_EVENTS_CONSTRAINTS_FIELDS.COUNT) | ||
106 | } | ||
107 | |||
108 | function isVideoRatingTypeValid (value) { | ||
109 | return values(VIDEO_RATE_TYPES).indexOf(value) !== -1 | ||
110 | } | ||
111 | |||
112 | function isVideoFile (value, files) { | ||
113 | // Should have files | ||
114 | if (!files) return false | ||
115 | |||
116 | // Should have videofile file | ||
117 | const videofile = files.videofile | ||
118 | if (!videofile || videofile.length === 0) return false | ||
119 | |||
120 | // The file should exist | ||
121 | const file = videofile[0] | ||
122 | if (!file || !file.originalname) return false | ||
123 | |||
124 | return new RegExp('^video/(webm|mp4|ogg)$', 'i').test(file.mimetype) | ||
125 | } | ||
126 | |||
127 | // --------------------------------------------------------------------------- | ||
128 | |||
129 | export { | ||
130 | isVideoAuthorValid, | ||
131 | isVideoDateValid, | ||
132 | isVideoCategoryValid, | ||
133 | isVideoLicenceValid, | ||
134 | isVideoLanguageValid, | ||
135 | isVideoNSFWValid, | ||
136 | isVideoDescriptionValid, | ||
137 | isVideoDurationValid, | ||
138 | isVideoInfoHashValid, | ||
139 | isVideoNameValid, | ||
140 | isVideoTagsValid, | ||
141 | isVideoThumbnailValid, | ||
142 | isVideoThumbnailDataValid, | ||
143 | isVideoExtnameValid, | ||
144 | isVideoRemoteIdValid, | ||
145 | isVideoAbuseReasonValid, | ||
146 | isVideoAbuseReporterUsernameValid, | ||
147 | isVideoFile, | ||
148 | isVideoViewsValid, | ||
149 | isVideoLikesValid, | ||
150 | isVideoRatingTypeValid, | ||
151 | isVideoDislikesValid, | ||
152 | isVideoEventCountValid | ||
153 | } | ||