aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/custom-validators/videos.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/helpers/custom-validators/videos.ts')
-rw-r--r--server/helpers/custom-validators/videos.ts153
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 @@
1import { values } from 'lodash'
2import expressValidator = require('express-validator')
3// TODO: use .validator when express-validator typing will have validator field
4const validator = expressValidator['validator']
5
6import {
7 CONSTRAINTS_FIELDS,
8 VIDEO_CATEGORIES,
9 VIDEO_LICENCES,
10 VIDEO_LANGUAGES,
11 VIDEO_RATE_TYPES
12} from '../../initializers'
13import { isUserUsernameValid } from './users'
14import { isArray } from './misc'
15
16const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS
17const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES
18const VIDEO_EVENTS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_EVENTS
19
20function isVideoAuthorValid (value) {
21 return isUserUsernameValid(value)
22}
23
24function isVideoDateValid (value) {
25 return validator.isDate(value)
26}
27
28function isVideoCategoryValid (value) {
29 return VIDEO_CATEGORIES[value] !== undefined
30}
31
32function isVideoLicenceValid (value) {
33 return VIDEO_LICENCES[value] !== undefined
34}
35
36function isVideoLanguageValid (value) {
37 return value === null || VIDEO_LANGUAGES[value] !== undefined
38}
39
40function isVideoNSFWValid (value) {
41 return validator.isBoolean(value)
42}
43
44function isVideoDescriptionValid (value) {
45 return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION)
46}
47
48function isVideoDurationValid (value) {
49 return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION)
50}
51
52function isVideoExtnameValid (value) {
53 return VIDEOS_CONSTRAINTS_FIELDS.EXTNAME.indexOf(value) !== -1
54}
55
56function isVideoInfoHashValid (value) {
57 return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH)
58}
59
60function isVideoNameValid (value) {
61 return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME)
62}
63
64function 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
72function isVideoThumbnailValid (value) {
73 return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL)
74}
75
76function isVideoThumbnailDataValid (value) {
77 return validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL_DATA)
78}
79
80function isVideoRemoteIdValid (value) {
81 return validator.isUUID(value, 4)
82}
83
84function isVideoAbuseReasonValid (value) {
85 return validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.REASON)
86}
87
88function isVideoAbuseReporterUsernameValid (value) {
89 return isUserUsernameValid(value)
90}
91
92function isVideoViewsValid (value) {
93 return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS)
94}
95
96function isVideoLikesValid (value) {
97 return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.LIKES)
98}
99
100function isVideoDislikesValid (value) {
101 return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DISLIKES)
102}
103
104function isVideoEventCountValid (value) {
105 return validator.isInt(value + '', VIDEO_EVENTS_CONSTRAINTS_FIELDS.COUNT)
106}
107
108function isVideoRatingTypeValid (value) {
109 return values(VIDEO_RATE_TYPES).indexOf(value) !== -1
110}
111
112function 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
129export {
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}