1 import * as Bluebird from 'bluebird'
2 import { Response } from 'express'
3 import 'express-validator'
4 import { values } from 'lodash'
6 import * as validator from 'validator'
7 import { VideoRateType } from '../../../shared'
8 import { CONSTRAINTS_FIELDS, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_RATE_TYPES } from '../../initializers'
9 import { database as db } from '../../initializers/database'
10 import { VideoInstance } from '../../models/video/video-interface'
11 import { logger } from '../logger'
12 import { isActivityPubUrlValid } from './activitypub/misc'
13 import { exists, isArray } from './misc'
14 import { VIDEO_PRIVACIES } from '../../initializers/constants'
16 const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS
17 const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES
19 function isVideoCategoryValid (value: number) {
20 return VIDEO_CATEGORIES[value] !== undefined
23 function isVideoUrlValid (value: string) {
24 return isActivityPubUrlValid(value)
27 function isVideoLicenceValid (value: number) {
28 return VIDEO_LICENCES[value] !== undefined
31 function isVideoLanguageValid (value: number) {
32 return value === null || VIDEO_LANGUAGES[value] !== undefined
35 function isVideoNSFWValid (value: any) {
36 return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value))
39 function isVideoDurationValid (value: string) {
40 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION)
43 function isVideoTruncatedDescriptionValid (value: string) {
44 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.TRUNCATED_DESCRIPTION)
47 function isVideoDescriptionValid (value: string) {
48 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION)
51 function isVideoNameValid (value: string) {
52 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME)
55 function isVideoTagValid (tag: string) {
56 return exists(tag) && validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG)
59 function isVideoTagsValid (tags: string[]) {
60 return isArray(tags) &&
61 validator.isInt(tags.length.toString(), VIDEOS_CONSTRAINTS_FIELDS.TAGS) &&
62 tags.every(tag => isVideoTagValid(tag))
65 function isVideoAbuseReasonValid (value: string) {
66 return exists(value) && validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.REASON)
69 function isVideoViewsValid (value: string) {
70 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS)
73 function isVideoRatingTypeValid (value: string) {
74 return values(VIDEO_RATE_TYPES).indexOf(value as VideoRateType) !== -1
77 function isVideoFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[]) {
79 if (!files) return false
80 if (isArray(files)) return false
82 // Should have videofile file
83 const videofile = files['videofile']
84 if (!videofile || videofile.length === 0) return false
86 // The file should exist
87 const file = videofile[0]
88 if (!file || !file.originalname) return false
90 return new RegExp('^video/(webm|mp4|ogg)$', 'i').test(file.mimetype)
93 function isVideoPrivacyValid (value: string) {
94 return VIDEO_PRIVACIES[value] !== undefined
97 function isVideoFileInfoHashValid (value: string) {
98 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH)
101 function isVideoFileResolutionValid (value: string) {
102 return exists(value) && validator.isInt(value + '')
105 function isVideoFileSizeValid (value: string) {
106 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.FILE_SIZE)
109 function checkVideoExists (id: string, res: Response, callback: () => void) {
110 let promise: Bluebird<VideoInstance>
111 if (validator.isInt(id)) {
112 promise = db.Video.loadAndPopulateAccountAndServerAndTags(+id)
114 promise = db.Video.loadByUUIDAndPopulateAccountAndServerAndTags(id)
117 promise.then(video => {
119 return res.status(404)
120 .json({ error: 'Video not found' })
124 res.locals.video = video
128 logger.error('Error in video request validator.', err)
129 return res.sendStatus(500)
133 async function isVideoExistsPromise (id: string, res: Response) {
134 let video: VideoInstance
136 if (validator.isInt(id)) {
137 video = await db.Video.loadAndPopulateAccountAndServerAndTags(+id)
139 video = await db.Video.loadByUUIDAndPopulateAccountAndServerAndTags(id)
144 .json({ error: 'Video not found' })
150 res.locals.video = video
154 // ---------------------------------------------------------------------------
157 isVideoCategoryValid,
159 isVideoLanguageValid,
161 isVideoTruncatedDescriptionValid,
162 isVideoDescriptionValid,
163 isVideoFileInfoHashValid,
166 isVideoAbuseReasonValid,
169 isVideoRatingTypeValid,
170 isVideoDurationValid,
174 isVideoFileResolutionValid,
175 isVideoFileSizeValid,