1 import { Response } from 'express'
2 import 'express-validator'
3 import { values } from 'lodash'
5 import * as validator from 'validator'
6 import { UserRight, VideoPrivacy, VideoRateType } from '../../../shared'
15 } from '../../initializers'
16 import { VideoModel } from '../../models/video/video'
17 import { exists, isArray, isFileValid } from './misc'
18 import { VideoChannelModel } from '../../models/video/video-channel'
19 import { UserModel } from '../../models/account/user'
21 const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS
22 const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES
24 function isVideoCategoryValid (value: any) {
25 return value === null || VIDEO_CATEGORIES[ value ] !== undefined
28 function isVideoStateValid (value: any) {
29 return exists(value) && VIDEO_STATES[ value ] !== undefined
32 function isVideoLicenceValid (value: any) {
33 return value === null || VIDEO_LICENCES[ value ] !== undefined
36 function isVideoLanguageValid (value: any) {
37 return value === null ||
38 (typeof value === 'string' && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.LANGUAGE))
41 function isVideoDurationValid (value: string) {
42 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION)
45 function isVideoTruncatedDescriptionValid (value: string) {
46 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.TRUNCATED_DESCRIPTION)
49 function isVideoDescriptionValid (value: string) {
50 return value === null || (exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION))
53 function isVideoSupportValid (value: string) {
54 return value === null || (exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.SUPPORT))
57 function isVideoNameValid (value: string) {
58 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME)
61 function isVideoTagValid (tag: string) {
62 return exists(tag) && validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG)
65 function isVideoTagsValid (tags: string[]) {
66 return tags === null || (
68 validator.isInt(tags.length.toString(), VIDEOS_CONSTRAINTS_FIELDS.TAGS) &&
69 tags.every(tag => isVideoTagValid(tag))
73 function isVideoAbuseReasonValid (value: string) {
74 return exists(value) && validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.REASON)
77 function isVideoViewsValid (value: string) {
78 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS)
81 function isVideoRatingTypeValid (value: string) {
82 return value === 'none' || values(VIDEO_RATE_TYPES).indexOf(value as VideoRateType) !== -1
85 const videoFileTypes = Object.keys(VIDEO_MIMETYPE_EXT).map(m => `(${m})`)
86 const videoFileTypesRegex = videoFileTypes.join('|')
88 function isVideoFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[]) {
89 return isFileValid(files, videoFileTypesRegex, 'videofile', null)
92 const videoImageTypes = CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME
93 .map(v => v.replace('.', ''))
95 const videoImageTypesRegex = `image/(${videoImageTypes})`
97 function isVideoImage (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[], field: string) {
98 return isFileValid(files, videoImageTypesRegex, field, CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max, true)
101 function isVideoPrivacyValid (value: number) {
102 return validator.isInt(value + '') && VIDEO_PRIVACIES[ value ] !== undefined
105 function isScheduleVideoUpdatePrivacyValid (value: number) {
106 return validator.isInt(value + '') &&
108 value === VideoPrivacy.UNLISTED ||
109 value === VideoPrivacy.PUBLIC
113 function isVideoFileInfoHashValid (value: string) {
114 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH)
117 function isVideoFileResolutionValid (value: string) {
118 return exists(value) && validator.isInt(value + '')
121 function isVideoFPSResolutionValid (value: string) {
122 return value === null || validator.isInt(value + '')
125 function isVideoFileSizeValid (value: string) {
126 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.FILE_SIZE)
129 function checkUserCanManageVideo (user: UserModel, video: VideoModel, right: UserRight, res: Response) {
130 // Retrieve the user who did the request
131 if (video.isOwned() === false) {
133 .json({ error: 'Cannot manage a video of another server.' })
138 // Check if the user can delete the video
139 // The user can delete it if he has the right
140 // Or if s/he is the video's account
141 const account = video.VideoChannel.Account
142 if (user.hasRight(right) === false && account.userId !== user.id) {
144 .json({ error: 'Cannot manage a video of another user.' })
152 async function isVideoExist (id: string, res: Response) {
153 let video: VideoModel
155 if (validator.isInt(id)) {
156 video = await VideoModel.loadAndPopulateAccountAndServerAndTags(+id)
158 video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(id)
163 .json({ error: 'Video not found' })
169 res.locals.video = video
173 async function isVideoChannelOfAccountExist (channelId: number, user: UserModel, res: Response) {
174 if (user.hasRight(UserRight.UPDATE_ANY_VIDEO) === true) {
175 const videoChannel = await VideoChannelModel.loadAndPopulateAccount(channelId)
178 .json({ error: 'Unknown video video channel on this instance.' })
184 res.locals.videoChannel = videoChannel
188 const videoChannel = await VideoChannelModel.loadByIdAndAccount(channelId, user.Account.id)
191 .json({ error: 'Unknown video video channel for this account.' })
197 res.locals.videoChannel = videoChannel
201 // ---------------------------------------------------------------------------
204 isVideoCategoryValid,
205 checkUserCanManageVideo,
207 isVideoLanguageValid,
208 isVideoTruncatedDescriptionValid,
209 isVideoDescriptionValid,
210 isVideoFileInfoHashValid,
213 isVideoFPSResolutionValid,
214 isScheduleVideoUpdatePrivacyValid,
215 isVideoAbuseReasonValid,
219 isVideoRatingTypeValid,
220 isVideoDurationValid,
223 isVideoFileResolutionValid,
224 isVideoFileSizeValid,
227 isVideoChannelOfAccountExist,