1 import { values } from 'lodash'
2 import * as validator from 'validator'
11 } from '../../initializers'
12 import { isUserUsernameValid } from './users'
13 import { isArray, exists } from './misc'
14 import { VideoRateType } from '../../../shared'
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
20 function isVideoIdOrUUIDValid (value: string) {
21 return validator.isInt(value) || isVideoUUIDValid(value)
24 function isVideoAuthorValid (value: string) {
25 return isUserUsernameValid(value)
28 function isVideoDateValid (value: string) {
29 return exists(value) && validator.isISO8601(value)
32 function isVideoCategoryValid (value: number) {
33 return VIDEO_CATEGORIES[value] !== undefined
36 function isVideoLicenceValid (value: number) {
37 return VIDEO_LICENCES[value] !== undefined
40 function isVideoLanguageValid (value: number) {
41 return value === null || VIDEO_LANGUAGES[value] !== undefined
44 function isVideoNSFWValid (value: any) {
45 return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value))
48 function isVideoDescriptionValid (value: string) {
49 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION)
52 function isVideoDurationValid (value: string) {
53 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION)
56 function isVideoExtnameValid (value: string) {
57 return VIDEOS_CONSTRAINTS_FIELDS.EXTNAME.indexOf(value) !== -1
60 function isVideoInfoHashValid (value: string) {
61 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH)
64 function isVideoNameValid (value: string) {
65 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME)
68 function isVideoTagsValid (tags: string[]) {
69 return isArray(tags) &&
70 validator.isInt(tags.length.toString(), VIDEOS_CONSTRAINTS_FIELDS.TAGS) &&
71 tags.every(function (tag) {
72 return exists(tag) && validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG)
76 function isVideoThumbnailValid (value: string) {
77 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL)
80 function isVideoThumbnailDataValid (value: string) {
81 return exists(value) && validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL_DATA)
84 function isVideoUUIDValid (value: string) {
85 return exists(value) && validator.isUUID('' + value, 4)
88 function isVideoAbuseReasonValid (value: string) {
89 return exists(value) && validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.REASON)
92 function isVideoAbuseReporterUsernameValid (value: string) {
93 return isUserUsernameValid(value)
96 function isVideoViewsValid (value: string) {
97 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS)
100 function isVideoLikesValid (value: string) {
101 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.LIKES)
104 function isVideoDislikesValid (value: string) {
105 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DISLIKES)
108 function isVideoEventCountValid (value: string) {
109 return exists(value) && validator.isInt(value + '', VIDEO_EVENTS_CONSTRAINTS_FIELDS.COUNT)
112 function isVideoRatingTypeValid (value: string) {
113 return values(VIDEO_RATE_TYPES).indexOf(value as VideoRateType) !== -1
116 function isVideoFile (value: string, files: { [ fieldname: string ]: Express.Multer.File[] }) {
118 if (!files) return false
120 // Should have videofile file
121 const videofile = files.videofile
122 if (!videofile || videofile.length === 0) return false
124 // The file should exist
125 const file = videofile[0]
126 if (!file || !file.originalname) return false
128 return new RegExp('^video/(webm|mp4|ogg)$', 'i').test(file.mimetype)
131 // ---------------------------------------------------------------------------
134 isVideoIdOrUUIDValid,
137 isVideoCategoryValid,
139 isVideoLanguageValid,
141 isVideoDescriptionValid,
142 isVideoDurationValid,
143 isVideoInfoHashValid,
146 isVideoThumbnailValid,
147 isVideoThumbnailDataValid,
150 isVideoAbuseReasonValid,
151 isVideoAbuseReporterUsernameValid,
155 isVideoRatingTypeValid,
156 isVideoDislikesValid,
157 isVideoEventCountValid
161 namespace ExpressValidator {
162 export interface Validator {
163 isVideoIdOrUUIDValid,
166 isVideoCategoryValid,
168 isVideoLanguageValid,
170 isVideoDescriptionValid,
171 isVideoDurationValid,
172 isVideoInfoHashValid,
175 isVideoThumbnailValid,
176 isVideoThumbnailDataValid,
179 isVideoAbuseReasonValid,
180 isVideoAbuseReporterUsernameValid,
184 isVideoRatingTypeValid,
185 isVideoDislikesValid,
186 isVideoEventCountValid