1 import { values } from 'lodash'
2 import * as validator from 'validator'
3 import 'express-validator'
12 VIDEO_FILE_RESOLUTIONS
13 } from '../../initializers'
14 import { isUserUsernameValid } from './users'
15 import { isArray, exists } from './misc'
16 import { VideoRateType } from '../../../shared'
18 const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS
19 const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES
20 const VIDEO_EVENTS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_EVENTS
22 function isVideoIdOrUUIDValid (value: string) {
23 return validator.isInt(value) || isVideoUUIDValid(value)
26 function isVideoAuthorValid (value: string) {
27 return isUserUsernameValid(value)
30 function isVideoDateValid (value: string) {
31 return exists(value) && validator.isISO8601(value)
34 function isVideoCategoryValid (value: number) {
35 return VIDEO_CATEGORIES[value] !== undefined
38 function isVideoLicenceValid (value: number) {
39 return VIDEO_LICENCES[value] !== undefined
42 function isVideoLanguageValid (value: number) {
43 return value === null || VIDEO_LANGUAGES[value] !== undefined
46 function isVideoNSFWValid (value: any) {
47 return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value))
50 function isVideoDescriptionValid (value: string) {
51 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION)
54 function isVideoDurationValid (value: string) {
55 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION)
58 function isVideoNameValid (value: string) {
59 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME)
62 function isVideoTagsValid (tags: string[]) {
63 return isArray(tags) &&
64 validator.isInt(tags.length.toString(), VIDEOS_CONSTRAINTS_FIELDS.TAGS) &&
66 return exists(tag) && validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG)
70 function isVideoThumbnailValid (value: string) {
71 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL)
74 function isVideoThumbnailDataValid (value: string) {
75 return exists(value) && validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL_DATA)
78 function isVideoUUIDValid (value: string) {
79 return exists(value) && validator.isUUID('' + value, 4)
82 function isVideoAbuseReasonValid (value: string) {
83 return exists(value) && validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.REASON)
86 function isVideoAbuseReporterUsernameValid (value: string) {
87 return isUserUsernameValid(value)
90 function isVideoViewsValid (value: string) {
91 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS)
94 function isVideoLikesValid (value: string) {
95 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.LIKES)
98 function isVideoDislikesValid (value: string) {
99 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DISLIKES)
102 function isVideoEventCountValid (value: string) {
103 return exists(value) && validator.isInt(value + '', VIDEO_EVENTS_CONSTRAINTS_FIELDS.COUNT)
106 function isVideoRatingTypeValid (value: string) {
107 return values(VIDEO_RATE_TYPES).indexOf(value as VideoRateType) !== -1
110 function isVideoFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[]) {
112 if (!files) return false
113 if (isArray(files)) return false
115 // Should have videofile file
116 const videofile = files['videofile']
117 if (!videofile || videofile.length === 0) return false
119 // The file should exist
120 const file = videofile[0]
121 if (!file || !file.originalname) return false
123 return new RegExp('^video/(webm|mp4|ogg)$', 'i').test(file.mimetype)
126 function isVideoFileSizeValid (value: string) {
127 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.FILE_SIZE)
130 function isVideoFileResolutionValid (value: string) {
131 return VIDEO_FILE_RESOLUTIONS[value] !== undefined
134 function isVideoFileExtnameValid (value: string) {
135 return VIDEOS_CONSTRAINTS_FIELDS.EXTNAME.indexOf(value) !== -1
138 function isVideoFileInfoHashValid (value: string) {
139 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH)
142 // ---------------------------------------------------------------------------
145 isVideoIdOrUUIDValid,
148 isVideoCategoryValid,
150 isVideoLanguageValid,
152 isVideoDescriptionValid,
153 isVideoDurationValid,
154 isVideoFileInfoHashValid,
157 isVideoThumbnailValid,
158 isVideoThumbnailDataValid,
159 isVideoFileExtnameValid,
161 isVideoAbuseReasonValid,
162 isVideoAbuseReporterUsernameValid,
166 isVideoRatingTypeValid,
167 isVideoDislikesValid,
168 isVideoEventCountValid,
169 isVideoFileSizeValid,
170 isVideoFileResolutionValid