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'
14 } from '../../initializers'
15 import { VideoModel } from '../../models/video/video'
16 import { exists, isArray } from './misc'
18 const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS
19 const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES
21 function isVideoCategoryValid (value: number) {
22 return value === null || VIDEO_CATEGORIES[value] !== undefined
25 function isVideoLicenceValid (value: number) {
26 return value === null || VIDEO_LICENCES[value] !== undefined
29 function isVideoLanguageValid (value: number) {
30 return value === null || VIDEO_LANGUAGES[value] !== undefined
33 function isVideoDurationValid (value: string) {
34 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION)
37 function isVideoTruncatedDescriptionValid (value: string) {
38 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.TRUNCATED_DESCRIPTION)
41 function isVideoDescriptionValid (value: string) {
42 return value === null || (exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION))
45 function isVideoNameValid (value: string) {
46 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME)
49 function isVideoTagValid (tag: string) {
50 return exists(tag) && validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG)
53 function isVideoTagsValid (tags: string[]) {
54 return isArray(tags) &&
55 validator.isInt(tags.length.toString(), VIDEOS_CONSTRAINTS_FIELDS.TAGS) &&
56 tags.every(tag => isVideoTagValid(tag))
59 function isVideoAbuseReasonValid (value: string) {
60 return exists(value) && validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.REASON)
63 function isVideoViewsValid (value: string) {
64 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS)
67 function isVideoRatingTypeValid (value: string) {
68 return value === 'none' || values(VIDEO_RATE_TYPES).indexOf(value as VideoRateType) !== -1
71 function isVideoFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[]) {
73 if (!files) return false
74 if (isArray(files)) return false
76 // Should have videofile file
77 const videofile = files['videofile']
78 if (!videofile || videofile.length === 0) return false
80 // The file should exist
81 const file = videofile[0]
82 if (!file || !file.originalname) return false
84 return new RegExp('^video/(webm|mp4|ogg)$', 'i').test(file.mimetype)
87 function isVideoPrivacyValid (value: string) {
88 return validator.isInt(value + '') && VIDEO_PRIVACIES[value] !== undefined
91 function isVideoFileInfoHashValid (value: string) {
92 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH)
95 function isVideoFileResolutionValid (value: string) {
96 return exists(value) && validator.isInt(value + '')
99 function isVideoFileSizeValid (value: string) {
100 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.FILE_SIZE)
103 async function isVideoExist (id: string, res: Response) {
104 let video: VideoModel
106 if (validator.isInt(id)) {
107 video = await VideoModel.loadAndPopulateAccountAndServerAndTags(+id)
109 video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(id)
114 .json({ error: 'Video not found' })
120 res.locals.video = video
124 // ---------------------------------------------------------------------------
127 isVideoCategoryValid,
129 isVideoLanguageValid,
130 isVideoTruncatedDescriptionValid,
131 isVideoDescriptionValid,
132 isVideoFileInfoHashValid,
135 isVideoAbuseReasonValid,
138 isVideoRatingTypeValid,
139 isVideoDurationValid,
142 isVideoFileResolutionValid,
143 isVideoFileSizeValid,