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'
17 import { VideoChannelModel } from '../../models/video/video-channel'
19 const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS
20 const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES
22 function isVideoCategoryValid (value: any) {
23 return value === null || VIDEO_CATEGORIES[value] !== undefined
26 function isVideoLicenceValid (value: any) {
27 return value === null || VIDEO_LICENCES[value] !== undefined
30 function isVideoLanguageValid (value: any) {
31 return value === null ||
32 (typeof value === 'string' && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.LANGUAGE))
35 function isVideoDurationValid (value: string) {
36 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION)
39 function isVideoTruncatedDescriptionValid (value: string) {
40 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.TRUNCATED_DESCRIPTION)
43 function isVideoDescriptionValid (value: string) {
44 return value === null || (exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION))
47 function isVideoSupportValid (value: string) {
48 return value === null || (exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.SUPPORT))
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 tags === null || (
62 validator.isInt(tags.length.toString(), VIDEOS_CONSTRAINTS_FIELDS.TAGS) &&
63 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 async function isVideoChannelOfAccountExist (channelId: number, accountId: number, res: Response) {
131 const videoChannel = await VideoChannelModel.loadByIdAndAccount(channelId, accountId)
134 .json({ error: 'Unknown video video channel for this account.' })
140 res.locals.videoChannel = videoChannel
144 // ---------------------------------------------------------------------------
147 isVideoCategoryValid,
149 isVideoLanguageValid,
150 isVideoTruncatedDescriptionValid,
151 isVideoDescriptionValid,
152 isVideoFileInfoHashValid,
155 isVideoAbuseReasonValid,
158 isVideoRatingTypeValid,
159 isVideoDurationValid,
162 isVideoFileResolutionValid,
163 isVideoFileSizeValid,
166 isVideoChannelOfAccountExist,