1 import { Response } from 'express'
2 import 'express-validator'
3 import { values } from 'lodash'
5 import * as validator from 'validator'
6 import { UserRight, 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'
18 import { UserModel } from '../../models/account/user'
20 const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS
21 const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES
23 function isVideoCategoryValid (value: any) {
24 return value === null || VIDEO_CATEGORIES[value] !== undefined
27 function isVideoLicenceValid (value: any) {
28 return value === null || VIDEO_LICENCES[value] !== undefined
31 function isVideoLanguageValid (value: any) {
32 return value === null ||
33 (typeof value === 'string' && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.LANGUAGE))
36 function isVideoDurationValid (value: string) {
37 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION)
40 function isVideoTruncatedDescriptionValid (value: string) {
41 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.TRUNCATED_DESCRIPTION)
44 function isVideoDescriptionValid (value: string) {
45 return value === null || (exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION))
48 function isVideoSupportValid (value: string) {
49 return value === null || (exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.SUPPORT))
52 function isVideoNameValid (value: string) {
53 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME)
56 function isVideoTagValid (tag: string) {
57 return exists(tag) && validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG)
60 function isVideoTagsValid (tags: string[]) {
61 return tags === null || (
63 validator.isInt(tags.length.toString(), VIDEOS_CONSTRAINTS_FIELDS.TAGS) &&
64 tags.every(tag => isVideoTagValid(tag))
68 function isVideoAbuseReasonValid (value: string) {
69 return exists(value) && validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.REASON)
72 function isVideoViewsValid (value: string) {
73 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS)
76 function isVideoRatingTypeValid (value: string) {
77 return value === 'none' || values(VIDEO_RATE_TYPES).indexOf(value as VideoRateType) !== -1
80 const videoFileTypes = Object.keys(VIDEO_MIMETYPE_EXT).map(m => `(${m})`)
81 const videoFileTypesRegex = videoFileTypes.join('|')
82 function isVideoFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[]) {
83 return isFileValid(files, videoFileTypesRegex, 'videofile')
86 const videoImageTypes = CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME
87 .map(v => v.replace('.', ''))
89 const videoImageTypesRegex = `image/(${videoImageTypes})`
90 function isVideoImage (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[], field: string) {
91 return isFileValid(files, videoImageTypesRegex, field, true)
94 function isVideoPrivacyValid (value: string) {
95 return validator.isInt(value + '') && VIDEO_PRIVACIES[value] !== undefined
98 function isVideoFileInfoHashValid (value: string) {
99 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH)
102 function isVideoFileResolutionValid (value: string) {
103 return exists(value) && validator.isInt(value + '')
106 function isVideoFileSizeValid (value: string) {
107 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.FILE_SIZE)
110 async function isVideoExist (id: string, res: Response) {
111 let video: VideoModel
113 if (validator.isInt(id)) {
114 video = await VideoModel.loadAndPopulateAccountAndServerAndTags(+id)
116 video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(id)
121 .json({ error: 'Video not found' })
127 res.locals.video = video
131 async function isVideoChannelOfAccountExist (channelId: number, user: UserModel, res: Response) {
132 if (user.hasRight(UserRight.UPDATE_ANY_VIDEO) === true) {
133 const videoChannel = await VideoChannelModel.loadAndPopulateAccount(channelId)
136 .json({ error: 'Unknown video video channel on this instance.' })
142 res.locals.videoChannel = videoChannel
146 const videoChannel = await VideoChannelModel.loadByIdAndAccount(channelId, user.Account.id)
149 .json({ error: 'Unknown video video channel for this account.' })
155 res.locals.videoChannel = videoChannel
159 // ---------------------------------------------------------------------------
162 isVideoCategoryValid,
164 isVideoLanguageValid,
165 isVideoTruncatedDescriptionValid,
166 isVideoDescriptionValid,
167 isVideoFileInfoHashValid,
170 isVideoAbuseReasonValid,
173 isVideoRatingTypeValid,
174 isVideoDurationValid,
177 isVideoFileResolutionValid,
178 isVideoFileSizeValid,
181 isVideoChannelOfAccountExist,