1 import { values } from 'lodash'
2 import * as validator from 'validator'
3 import * as Promise from 'bluebird'
4 import * as express from 'express'
5 import 'express-validator'
15 } from '../../initializers'
16 import { isUserUsernameValid } from './users'
17 import { isArray, exists } from './misc'
18 import { VideoInstance } from '../../models'
19 import { logger } from '../../helpers'
20 import { VideoRateType } from '../../../shared'
22 const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS
23 const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES
24 const VIDEO_EVENTS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_EVENTS
26 function isVideoCategoryValid (value: number) {
27 return VIDEO_CATEGORIES[value] !== undefined
30 // Maybe we don't know the remote category, but that doesn't matter
31 function isRemoteVideoCategoryValid (value: string) {
32 return validator.isInt('' + value)
35 function isVideoLicenceValid (value: number) {
36 return VIDEO_LICENCES[value] !== undefined
39 // Maybe we don't know the remote licence, but that doesn't matter
40 function isRemoteVideoLicenceValid (value: string) {
41 return validator.isInt('' + value)
44 function isVideoLanguageValid (value: number) {
45 return value === null || VIDEO_LANGUAGES[value] !== undefined
48 // Maybe we don't know the remote language, but that doesn't matter
49 function isRemoteVideoLanguageValid (value: string) {
50 return validator.isInt('' + value)
53 function isVideoNSFWValid (value: any) {
54 return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value))
57 function isVideoDescriptionValid (value: string) {
58 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION)
61 function isVideoDurationValid (value: string) {
62 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION)
65 function isVideoNameValid (value: string) {
66 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME)
69 function isVideoTagsValid (tags: string[]) {
70 return isArray(tags) &&
71 validator.isInt(tags.length.toString(), VIDEOS_CONSTRAINTS_FIELDS.TAGS) &&
73 return exists(tag) && validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG)
77 function isVideoThumbnailValid (value: string) {
78 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL)
81 function isVideoThumbnailDataValid (value: string) {
82 return exists(value) && validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL_DATA)
85 function isVideoAbuseReasonValid (value: string) {
86 return exists(value) && validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.REASON)
89 function isVideoAbuseReporterUsernameValid (value: string) {
90 return isUserUsernameValid(value)
93 function isVideoViewsValid (value: string) {
94 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS)
97 function isVideoLikesValid (value: string) {
98 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.LIKES)
101 function isVideoDislikesValid (value: string) {
102 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DISLIKES)
105 function isVideoEventCountValid (value: string) {
106 return exists(value) && validator.isInt(value + '', VIDEO_EVENTS_CONSTRAINTS_FIELDS.COUNT)
109 function isVideoRatingTypeValid (value: string) {
110 return values(VIDEO_RATE_TYPES).indexOf(value as VideoRateType) !== -1
113 function isVideoFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[]) {
115 if (!files) return false
116 if (isArray(files)) return false
118 // Should have videofile file
119 const videofile = files['videofile']
120 if (!videofile || videofile.length === 0) return false
122 // The file should exist
123 const file = videofile[0]
124 if (!file || !file.originalname) return false
126 return new RegExp('^video/(webm|mp4|ogg)$', 'i').test(file.mimetype)
129 function isVideoFileSizeValid (value: string) {
130 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.FILE_SIZE)
133 function isVideoFileResolutionValid (value: string) {
134 return exists(value) && validator.isInt(value + '')
137 function isVideoFileExtnameValid (value: string) {
138 return VIDEOS_CONSTRAINTS_FIELDS.EXTNAME.indexOf(value) !== -1
141 function isVideoFileInfoHashValid (value: string) {
142 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH)
145 function checkVideoExists (id: string, res: express.Response, callback: () => void) {
146 let promise: Promise<VideoInstance>
147 if (validator.isInt(id)) {
148 promise = db.Video.loadAndPopulateAuthorAndPodAndTags(+id)
150 promise = db.Video.loadByUUIDAndPopulateAuthorAndPodAndTags(id)
153 promise.then(video => {
155 return res.status(404)
156 .json({ error: 'Video not found' })
160 res.locals.video = video
164 logger.error('Error in video request validator.', err)
165 return res.sendStatus(500)
169 // ---------------------------------------------------------------------------
172 isVideoCategoryValid,
174 isVideoLanguageValid,
176 isVideoDescriptionValid,
177 isVideoDurationValid,
178 isVideoFileInfoHashValid,
181 isVideoThumbnailValid,
182 isVideoThumbnailDataValid,
183 isVideoFileExtnameValid,
184 isVideoAbuseReasonValid,
185 isVideoAbuseReporterUsernameValid,
189 isVideoRatingTypeValid,
190 isVideoDislikesValid,
191 isVideoEventCountValid,
192 isVideoFileSizeValid,
193 isVideoFileResolutionValid,
195 isRemoteVideoCategoryValid,
196 isRemoteVideoLicenceValid,
197 isRemoteVideoLanguageValid