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 isVideoAuthorValid (value: string) {
21 return isUserUsernameValid(value)
24 function isVideoDateValid (value: string) {
25 return exists(value) && validator.isISO8601(value)
28 function isVideoCategoryValid (value: number) {
29 return VIDEO_CATEGORIES[value] !== undefined
32 function isVideoLicenceValid (value: number) {
33 return VIDEO_LICENCES[value] !== undefined
36 function isVideoLanguageValid (value: number) {
37 return value === null || VIDEO_LANGUAGES[value] !== undefined
40 function isVideoNSFWValid (value: any) {
41 return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value))
44 function isVideoDescriptionValid (value: string) {
45 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION)
48 function isVideoDurationValid (value: string) {
49 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION)
52 function isVideoExtnameValid (value: string) {
53 return VIDEOS_CONSTRAINTS_FIELDS.EXTNAME.indexOf(value) !== -1
56 function isVideoInfoHashValid (value: string) {
57 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH)
60 function isVideoNameValid (value: string) {
61 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME)
64 function isVideoTagsValid (tags: string[]) {
65 return isArray(tags) &&
66 validator.isInt(tags.length.toString(), VIDEOS_CONSTRAINTS_FIELDS.TAGS) &&
67 tags.every(function (tag) {
68 return exists(tag) && validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG)
72 function isVideoThumbnailValid (value: string) {
73 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL)
76 function isVideoThumbnailDataValid (value: string) {
77 return exists(value) && validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL_DATA)
80 function isVideoRemoteIdValid (value: string) {
81 return exists(value) && validator.isUUID(value, 4)
84 function isVideoAbuseReasonValid (value: string) {
85 return exists(value) && validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.REASON)
88 function isVideoAbuseReporterUsernameValid (value: string) {
89 return isUserUsernameValid(value)
92 function isVideoViewsValid (value: string) {
93 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS)
96 function isVideoLikesValid (value: string) {
97 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.LIKES)
100 function isVideoDislikesValid (value: string) {
101 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DISLIKES)
104 function isVideoEventCountValid (value: string) {
105 return exists(value) && validator.isInt(value + '', VIDEO_EVENTS_CONSTRAINTS_FIELDS.COUNT)
108 function isVideoRatingTypeValid (value: string) {
109 return values(VIDEO_RATE_TYPES).indexOf(value as VideoRateType) !== -1
112 function isVideoFile (value: string, files: { [ fieldname: string ]: Express.Multer.File[] }) {
114 if (!files) return false
116 // Should have videofile file
117 const videofile = files.videofile
118 if (!videofile || videofile.length === 0) return false
120 // The file should exist
121 const file = videofile[0]
122 if (!file || !file.originalname) return false
124 return new RegExp('^video/(webm|mp4|ogg)$', 'i').test(file.mimetype)
127 // ---------------------------------------------------------------------------
132 isVideoCategoryValid,
134 isVideoLanguageValid,
136 isVideoDescriptionValid,
137 isVideoDurationValid,
138 isVideoInfoHashValid,
141 isVideoThumbnailValid,
142 isVideoThumbnailDataValid,
144 isVideoRemoteIdValid,
145 isVideoAbuseReasonValid,
146 isVideoAbuseReporterUsernameValid,
150 isVideoRatingTypeValid,
151 isVideoDislikesValid,
152 isVideoEventCountValid
156 namespace ExpressValidator {
157 export interface Validator {
160 isVideoCategoryValid,
162 isVideoLanguageValid,
164 isVideoDescriptionValid,
165 isVideoDurationValid,
166 isVideoInfoHashValid,
169 isVideoThumbnailValid,
170 isVideoThumbnailDataValid,
172 isVideoRemoteIdValid,
173 isVideoAbuseReasonValid,
174 isVideoAbuseReporterUsernameValid,
178 isVideoRatingTypeValid,
179 isVideoDislikesValid,
180 isVideoEventCountValid