1 import { values } from 'lodash'
2 import * as validator from 'validator'
11 VIDEO_FILE_RESOLUTIONS
12 } from '../../initializers'
13 import { isUserUsernameValid } from './users'
14 import { isArray, exists } from './misc'
15 import { VideoRateType } from '../../../shared'
17 const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS
18 const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES
19 const VIDEO_EVENTS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_EVENTS
21 function isVideoIdOrUUIDValid (value: string) {
22 return validator.isInt(value) || isVideoUUIDValid(value)
25 function isVideoAuthorValid (value: string) {
26 return isUserUsernameValid(value)
29 function isVideoDateValid (value: string) {
30 return exists(value) && validator.isISO8601(value)
33 function isVideoCategoryValid (value: number) {
34 return VIDEO_CATEGORIES[value] !== undefined
37 function isVideoLicenceValid (value: number) {
38 return VIDEO_LICENCES[value] !== undefined
41 function isVideoLanguageValid (value: number) {
42 return value === null || VIDEO_LANGUAGES[value] !== undefined
45 function isVideoNSFWValid (value: any) {
46 return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value))
49 function isVideoDescriptionValid (value: string) {
50 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION)
53 function isVideoDurationValid (value: string) {
54 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION)
57 function isVideoNameValid (value: string) {
58 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME)
61 function isVideoTagsValid (tags: string[]) {
62 return isArray(tags) &&
63 validator.isInt(tags.length.toString(), VIDEOS_CONSTRAINTS_FIELDS.TAGS) &&
65 return exists(tag) && validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG)
69 function isVideoThumbnailValid (value: string) {
70 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL)
73 function isVideoThumbnailDataValid (value: string) {
74 return exists(value) && validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL_DATA)
77 function isVideoUUIDValid (value: string) {
78 return exists(value) && validator.isUUID('' + value, 4)
81 function isVideoAbuseReasonValid (value: string) {
82 return exists(value) && validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.REASON)
85 function isVideoAbuseReporterUsernameValid (value: string) {
86 return isUserUsernameValid(value)
89 function isVideoViewsValid (value: string) {
90 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS)
93 function isVideoLikesValid (value: string) {
94 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.LIKES)
97 function isVideoDislikesValid (value: string) {
98 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DISLIKES)
101 function isVideoEventCountValid (value: string) {
102 return exists(value) && validator.isInt(value + '', VIDEO_EVENTS_CONSTRAINTS_FIELDS.COUNT)
105 function isVideoRatingTypeValid (value: string) {
106 return values(VIDEO_RATE_TYPES).indexOf(value as VideoRateType) !== -1
109 function isVideoFile (value: string, files: { [ fieldname: string ]: Express.Multer.File[] }) {
111 if (!files) return false
113 // Should have videofile file
114 const videofile = files.videofile
115 if (!videofile || videofile.length === 0) return false
117 // The file should exist
118 const file = videofile[0]
119 if (!file || !file.originalname) return false
121 return new RegExp('^video/(webm|mp4|ogg)$', 'i').test(file.mimetype)
124 function isVideoFileSizeValid (value: string) {
125 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.FILE_SIZE)
128 function isVideoFileResolutionValid (value: string) {
129 return VIDEO_FILE_RESOLUTIONS[value] !== undefined
132 function isVideoFileExtnameValid (value: string) {
133 return VIDEOS_CONSTRAINTS_FIELDS.EXTNAME.indexOf(value) !== -1
136 function isVideoFileInfoHashValid (value: string) {
137 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH)
140 // ---------------------------------------------------------------------------
143 isVideoIdOrUUIDValid,
146 isVideoCategoryValid,
148 isVideoLanguageValid,
150 isVideoDescriptionValid,
151 isVideoDurationValid,
152 isVideoFileInfoHashValid,
155 isVideoThumbnailValid,
156 isVideoThumbnailDataValid,
157 isVideoFileExtnameValid,
159 isVideoAbuseReasonValid,
160 isVideoAbuseReporterUsernameValid,
164 isVideoRatingTypeValid,
165 isVideoDislikesValid,
166 isVideoEventCountValid,
167 isVideoFileSizeValid,
168 isVideoFileResolutionValid
171 declare module 'express-validator' {
172 export interface Validator {
173 isVideoIdOrUUIDValid,
176 isVideoCategoryValid,
178 isVideoLanguageValid,
180 isVideoDescriptionValid,
181 isVideoDurationValid,
182 isVideoInfoHashValid,
185 isVideoThumbnailValid,
186 isVideoThumbnailDataValid,
189 isVideoAbuseReasonValid,
190 isVideoAbuseReporterUsernameValid,
194 isVideoRatingTypeValid,
195 isVideoDislikesValid,
196 isVideoEventCountValid,
197 isVideoFileSizeValid,
198 isVideoFileResolutionValid