1 import { values } from 'lodash'
2 import * as validator from 'validator'
10 } from '../../initializers'
11 import { isUserUsernameValid } from './users'
12 import { isArray, exists } from './misc'
14 const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS
15 const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES
16 const VIDEO_EVENTS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_EVENTS
18 function isVideoAuthorValid (value: string) {
19 return isUserUsernameValid(value)
22 function isVideoDateValid (value: string) {
23 return exists(value) && validator.isISO8601(value)
26 function isVideoCategoryValid (value: number) {
27 return VIDEO_CATEGORIES[value] !== undefined
30 function isVideoLicenceValid (value: number) {
31 return VIDEO_LICENCES[value] !== undefined
34 function isVideoLanguageValid (value: number) {
35 return value === null || VIDEO_LANGUAGES[value] !== undefined
38 function isVideoNSFWValid (value: any) {
39 return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value))
42 function isVideoDescriptionValid (value: string) {
43 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION)
46 function isVideoDurationValid (value: string) {
47 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION)
50 function isVideoExtnameValid (value: string) {
51 return VIDEOS_CONSTRAINTS_FIELDS.EXTNAME.indexOf(value) !== -1
54 function isVideoInfoHashValid (value: string) {
55 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH)
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) &&
65 tags.every(function (tag) {
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 isVideoRemoteIdValid (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) !== -1
110 function isVideoFile (value: string, files: { [ fieldname: string ]: Express.Multer.File[] }) {
112 if (!files) return false
114 // Should have videofile file
115 const videofile = files.videofile
116 if (!videofile || videofile.length === 0) return false
118 // The file should exist
119 const file = videofile[0]
120 if (!file || !file.originalname) return false
122 return new RegExp('^video/(webm|mp4|ogg)$', 'i').test(file.mimetype)
125 // ---------------------------------------------------------------------------
130 isVideoCategoryValid,
132 isVideoLanguageValid,
134 isVideoDescriptionValid,
135 isVideoDurationValid,
136 isVideoInfoHashValid,
139 isVideoThumbnailValid,
140 isVideoThumbnailDataValid,
142 isVideoRemoteIdValid,
143 isVideoAbuseReasonValid,
144 isVideoAbuseReporterUsernameValid,
148 isVideoRatingTypeValid,
149 isVideoDislikesValid,
150 isVideoEventCountValid
154 namespace ExpressValidator {
155 export interface Validator {
158 isVideoCategoryValid,
160 isVideoLanguageValid,
162 isVideoDescriptionValid,
163 isVideoDurationValid,
164 isVideoInfoHashValid,
167 isVideoThumbnailValid,
168 isVideoThumbnailDataValid,
170 isVideoRemoteIdValid,
171 isVideoAbuseReasonValid,
172 isVideoAbuseReporterUsernameValid,
176 isVideoRatingTypeValid,
177 isVideoDislikesValid,
178 isVideoEventCountValid