1 import { Response } from 'express'
2 import 'express-validator'
3 import { values } from 'lodash'
5 import * as validator from 'validator'
6 import { VideoRateType } from '../../../shared'
7 import { CONSTRAINTS_FIELDS, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_RATE_TYPES } from '../../initializers'
8 import { VIDEO_PRIVACIES } from '../../initializers/constants'
9 import { database as db } from '../../initializers/database'
10 import { VideoInstance } from '../../models/video/video-interface'
11 import { exists, isArray } from './misc'
13 const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS
14 const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES
16 function isVideoCategoryValid (value: number) {
17 return VIDEO_CATEGORIES[value] !== undefined
20 function isVideoLicenceValid (value: number) {
21 return VIDEO_LICENCES[value] !== undefined
24 function isVideoLanguageValid (value: number) {
25 return value === null || VIDEO_LANGUAGES[value] !== undefined
28 function isVideoNSFWValid (value: any) {
29 return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value))
32 function isVideoDurationValid (value: string) {
33 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION)
36 function isVideoTruncatedDescriptionValid (value: string) {
37 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.TRUNCATED_DESCRIPTION)
40 function isVideoDescriptionValid (value: string) {
41 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION)
44 function isVideoNameValid (value: string) {
45 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME)
48 function isVideoTagValid (tag: string) {
49 return exists(tag) && validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG)
52 function isVideoTagsValid (tags: string[]) {
53 return isArray(tags) &&
54 validator.isInt(tags.length.toString(), VIDEOS_CONSTRAINTS_FIELDS.TAGS) &&
55 tags.every(tag => isVideoTagValid(tag))
58 function isVideoAbuseReasonValid (value: string) {
59 return exists(value) && validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.REASON)
62 function isVideoViewsValid (value: string) {
63 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS)
66 function isVideoRatingTypeValid (value: string) {
67 return values(VIDEO_RATE_TYPES).indexOf(value as VideoRateType) !== -1
70 function isVideoFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[]) {
72 if (!files) return false
73 if (isArray(files)) return false
75 // Should have videofile file
76 const videofile = files['videofile']
77 if (!videofile || videofile.length === 0) return false
79 // The file should exist
80 const file = videofile[0]
81 if (!file || !file.originalname) return false
83 return new RegExp('^video/(webm|mp4|ogg)$', 'i').test(file.mimetype)
86 function isVideoPrivacyValid (value: string) {
87 return VIDEO_PRIVACIES[value] !== undefined
90 function isVideoFileInfoHashValid (value: string) {
91 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH)
94 function isVideoFileResolutionValid (value: string) {
95 return exists(value) && validator.isInt(value + '')
98 function isVideoFileSizeValid (value: string) {
99 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.FILE_SIZE)
102 async function isVideoExist (id: string, res: Response) {
103 let video: VideoInstance
105 if (validator.isInt(id)) {
106 video = await db.Video.loadAndPopulateAccountAndServerAndTags(+id)
108 video = await db.Video.loadByUUIDAndPopulateAccountAndServerAndTags(id)
113 .json({ error: 'Video not found' })
119 res.locals.video = video
123 // ---------------------------------------------------------------------------
126 isVideoCategoryValid,
128 isVideoLanguageValid,
130 isVideoTruncatedDescriptionValid,
131 isVideoDescriptionValid,
132 isVideoFileInfoHashValid,
135 isVideoAbuseReasonValid,
138 isVideoRatingTypeValid,
139 isVideoDurationValid,
142 isVideoFileResolutionValid,
143 isVideoFileSizeValid,