]>
Commit | Line | Data |
---|---|---|
65fcc311 | 1 | import { values } from 'lodash' |
4d4e5cd4 | 2 | import * as validator from 'validator' |
1840c2f7 | 3 | import 'multer' |
65fcc311 C |
4 | |
5 | import { | |
6 | CONSTRAINTS_FIELDS, | |
7 | VIDEO_CATEGORIES, | |
8 | VIDEO_LICENCES, | |
9 | VIDEO_LANGUAGES, | |
10 | VIDEO_RATE_TYPES | |
11 | } from '../../initializers' | |
12 | import { isUserUsernameValid } from './users' | |
69818c93 | 13 | import { isArray, exists } from './misc' |
ee9e7b61 | 14 | import { VideoRateType } from '../../../shared' |
65fcc311 C |
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 | |
0b697522 | 19 | |
0a6658fd C |
20 | function isVideoIdOrUUIDValid (value: string) { |
21 | return validator.isInt(value) || isVideoUUIDValid(value) | |
22 | } | |
23 | ||
69818c93 | 24 | function isVideoAuthorValid (value: string) { |
65fcc311 | 25 | return isUserUsernameValid(value) |
be587647 C |
26 | } |
27 | ||
69818c93 C |
28 | function isVideoDateValid (value: string) { |
29 | return exists(value) && validator.isISO8601(value) | |
be587647 C |
30 | } |
31 | ||
69818c93 | 32 | function isVideoCategoryValid (value: number) { |
65fcc311 | 33 | return VIDEO_CATEGORIES[value] !== undefined |
6e07c3de C |
34 | } |
35 | ||
69818c93 | 36 | function isVideoLicenceValid (value: number) { |
65fcc311 | 37 | return VIDEO_LICENCES[value] !== undefined |
6f0c39e2 C |
38 | } |
39 | ||
69818c93 | 40 | function isVideoLanguageValid (value: number) { |
65fcc311 | 41 | return value === null || VIDEO_LANGUAGES[value] !== undefined |
3092476e C |
42 | } |
43 | ||
69818c93 C |
44 | function isVideoNSFWValid (value: any) { |
45 | return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value)) | |
31b59b47 C |
46 | } |
47 | ||
69818c93 C |
48 | function isVideoDescriptionValid (value: string) { |
49 | return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION) | |
be587647 C |
50 | } |
51 | ||
69818c93 C |
52 | function isVideoDurationValid (value: string) { |
53 | return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION) | |
be587647 C |
54 | } |
55 | ||
69818c93 | 56 | function isVideoExtnameValid (value: string) { |
feb4bdfd C |
57 | return VIDEOS_CONSTRAINTS_FIELDS.EXTNAME.indexOf(value) !== -1 |
58 | } | |
59 | ||
69818c93 C |
60 | function isVideoInfoHashValid (value: string) { |
61 | return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH) | |
be587647 C |
62 | } |
63 | ||
69818c93 C |
64 | function isVideoNameValid (value: string) { |
65 | return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME) | |
be587647 C |
66 | } |
67 | ||
69818c93 | 68 | function isVideoTagsValid (tags: string[]) { |
65fcc311 | 69 | return isArray(tags) && |
69818c93 | 70 | validator.isInt(tags.length.toString(), VIDEOS_CONSTRAINTS_FIELDS.TAGS) && |
be587647 | 71 | tags.every(function (tag) { |
69818c93 | 72 | return exists(tag) && validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG) |
be587647 C |
73 | }) |
74 | } | |
75 | ||
69818c93 C |
76 | function isVideoThumbnailValid (value: string) { |
77 | return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL) | |
aaf61f38 C |
78 | } |
79 | ||
69818c93 C |
80 | function isVideoThumbnailDataValid (value: string) { |
81 | return exists(value) && validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL_DATA) | |
be587647 C |
82 | } |
83 | ||
0a6658fd C |
84 | function isVideoUUIDValid (value: string) { |
85 | return exists(value) && validator.isUUID('' + value, 4) | |
558d7c23 C |
86 | } |
87 | ||
69818c93 C |
88 | function isVideoAbuseReasonValid (value: string) { |
89 | return exists(value) && validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.REASON) | |
e4c55619 C |
90 | } |
91 | ||
69818c93 | 92 | function isVideoAbuseReporterUsernameValid (value: string) { |
65fcc311 | 93 | return isUserUsernameValid(value) |
3d118fb5 C |
94 | } |
95 | ||
69818c93 C |
96 | function isVideoViewsValid (value: string) { |
97 | return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS) | |
9e167724 C |
98 | } |
99 | ||
69818c93 C |
100 | function isVideoLikesValid (value: string) { |
101 | return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.LIKES) | |
9e167724 C |
102 | } |
103 | ||
69818c93 C |
104 | function isVideoDislikesValid (value: string) { |
105 | return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DISLIKES) | |
e4c87ec2 C |
106 | } |
107 | ||
69818c93 C |
108 | function isVideoEventCountValid (value: string) { |
109 | return exists(value) && validator.isInt(value + '', VIDEO_EVENTS_CONSTRAINTS_FIELDS.COUNT) | |
9e167724 C |
110 | } |
111 | ||
69818c93 | 112 | function isVideoRatingTypeValid (value: string) { |
ee9e7b61 | 113 | return values(VIDEO_RATE_TYPES).indexOf(value as VideoRateType) !== -1 |
d38b8281 C |
114 | } |
115 | ||
69818c93 | 116 | function isVideoFile (value: string, files: { [ fieldname: string ]: Express.Multer.File[] }) { |
f6f7dfee C |
117 | // Should have files |
118 | if (!files) return false | |
119 | ||
120 | // Should have videofile file | |
121 | const videofile = files.videofile | |
122 | if (!videofile || videofile.length === 0) return false | |
123 | ||
124 | // The file should exist | |
125 | const file = videofile[0] | |
126 | if (!file || !file.originalname) return false | |
127 | ||
128 | return new RegExp('^video/(webm|mp4|ogg)$', 'i').test(file.mimetype) | |
129 | } | |
130 | ||
55fa55a9 C |
131 | // --------------------------------------------------------------------------- |
132 | ||
65fcc311 | 133 | export { |
0a6658fd | 134 | isVideoIdOrUUIDValid, |
65fcc311 C |
135 | isVideoAuthorValid, |
136 | isVideoDateValid, | |
137 | isVideoCategoryValid, | |
138 | isVideoLicenceValid, | |
139 | isVideoLanguageValid, | |
140 | isVideoNSFWValid, | |
141 | isVideoDescriptionValid, | |
142 | isVideoDurationValid, | |
143 | isVideoInfoHashValid, | |
144 | isVideoNameValid, | |
145 | isVideoTagsValid, | |
146 | isVideoThumbnailValid, | |
147 | isVideoThumbnailDataValid, | |
148 | isVideoExtnameValid, | |
0a6658fd | 149 | isVideoUUIDValid, |
65fcc311 C |
150 | isVideoAbuseReasonValid, |
151 | isVideoAbuseReporterUsernameValid, | |
152 | isVideoFile, | |
153 | isVideoViewsValid, | |
154 | isVideoLikesValid, | |
155 | isVideoRatingTypeValid, | |
156 | isVideoDislikesValid, | |
157 | isVideoEventCountValid | |
158 | } | |
69818c93 C |
159 | |
160 | declare global { | |
161 | namespace ExpressValidator { | |
162 | export interface Validator { | |
0a6658fd | 163 | isVideoIdOrUUIDValid, |
69818c93 C |
164 | isVideoAuthorValid, |
165 | isVideoDateValid, | |
166 | isVideoCategoryValid, | |
167 | isVideoLicenceValid, | |
168 | isVideoLanguageValid, | |
169 | isVideoNSFWValid, | |
170 | isVideoDescriptionValid, | |
171 | isVideoDurationValid, | |
172 | isVideoInfoHashValid, | |
173 | isVideoNameValid, | |
174 | isVideoTagsValid, | |
175 | isVideoThumbnailValid, | |
176 | isVideoThumbnailDataValid, | |
177 | isVideoExtnameValid, | |
0a6658fd | 178 | isVideoUUIDValid, |
69818c93 C |
179 | isVideoAbuseReasonValid, |
180 | isVideoAbuseReporterUsernameValid, | |
181 | isVideoFile, | |
182 | isVideoViewsValid, | |
183 | isVideoLikesValid, | |
184 | isVideoRatingTypeValid, | |
185 | isVideoDislikesValid, | |
186 | isVideoEventCountValid | |
187 | } | |
188 | } | |
189 | } |