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'
11 VIDEO_LICENCES, VIDEO_MIMETYPE_EXT,
14 } from '../../initializers'
15 import { VideoModel } from '../../models/video/video'
16 import { exists, isArray, isFileValid } 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 areVideoLanguagesValid (value: number[]) {
30 return value === null || (isArray(value) && value.every(v => isVideoLanguageValid(v)))
33 function isVideoLanguageValid (value: number) {
34 return VIDEO_LANGUAGES[value] !== undefined
37 function isVideoDurationValid (value: string) {
38 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION)
41 function isVideoTruncatedDescriptionValid (value: string) {
42 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.TRUNCATED_DESCRIPTION)
45 function isVideoDescriptionValid (value: string) {
46 return value === null || (exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION))
49 function isVideoSupportValid (value: string) {
50 return value === null || (exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.SUPPORT))
53 function isVideoNameValid (value: string) {
54 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME)
57 function isVideoTagValid (tag: string) {
58 return exists(tag) && validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG)
61 function isVideoTagsValid (tags: string[]) {
62 return isArray(tags) &&
63 validator.isInt(tags.length.toString(), VIDEOS_CONSTRAINTS_FIELDS.TAGS) &&
64 tags.every(tag => isVideoTagValid(tag))
67 function isVideoAbuseReasonValid (value: string) {
68 return exists(value) && validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.REASON)
71 function isVideoViewsValid (value: string) {
72 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS)
75 function isVideoRatingTypeValid (value: string) {
76 return value === 'none' || values(VIDEO_RATE_TYPES).indexOf(value as VideoRateType) !== -1
79 const videoFileTypes = Object.keys(VIDEO_MIMETYPE_EXT).map(m => `(${m})`)
80 const videoFileTypesRegex = videoFileTypes.join('|')
81 function isVideoFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[]) {
82 return isFileValid(files, videoFileTypesRegex, 'videofile')
85 const videoImageTypes = CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME
86 .map(v => v.replace('.', ''))
88 const videoImageTypesRegex = `image/(${videoImageTypes})`
89 function isVideoImage (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[], field: string) {
90 return isFileValid(files, videoImageTypesRegex, field, true)
93 function isVideoPrivacyValid (value: string) {
94 return validator.isInt(value + '') && 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 async function isVideoExist (id: string, res: Response) {
110 let video: VideoModel
112 if (validator.isInt(id)) {
113 video = await VideoModel.loadAndPopulateAccountAndServerAndTags(+id)
115 video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(id)
120 .json({ error: 'Video not found' })
126 res.locals.video = video
130 // ---------------------------------------------------------------------------
133 isVideoCategoryValid,
135 isVideoLanguageValid,
136 isVideoTruncatedDescriptionValid,
137 isVideoDescriptionValid,
138 isVideoFileInfoHashValid,
140 areVideoLanguagesValid,
142 isVideoAbuseReasonValid,
145 isVideoRatingTypeValid,
146 isVideoDurationValid,
149 isVideoFileResolutionValid,
150 isVideoFileSizeValid,