1 import * as express from 'express'
2 import 'express-validator'
3 import { body, param, query, ValidationChain } from 'express-validator/check'
4 import { UserRight, VideoPrivacy } from '../../../shared'
13 } from '../../helpers/custom-validators/misc'
15 checkUserCanManageVideo,
16 isScheduleVideoUpdatePrivacyValid,
17 isVideoAbuseReasonValid,
19 isVideoChannelOfAccountExist,
20 isVideoDescriptionValid,
28 isVideoRatingTypeValid,
31 } from '../../helpers/custom-validators/videos'
32 import { getDurationFromVideoFile } from '../../helpers/ffmpeg-utils'
33 import { logger } from '../../helpers/logger'
34 import { CONSTRAINTS_FIELDS } from '../../initializers'
35 import { VideoShareModel } from '../../models/video/video-share'
36 import { authenticate } from '../oauth'
37 import { areValidationErrors } from './utils'
39 const videosAddValidator = getCommonVideoAttributes().concat([
41 .custom((value, { req }) => isVideoFile(req.files)).withMessage(
42 'This file is not supported or too large. Please, make sure it is of the following type: '
43 + CONSTRAINTS_FIELDS.VIDEOS.EXTNAME.join(', ')
45 body('name').custom(isVideoNameValid).withMessage('Should have a valid name'),
48 .custom(isIdValid).withMessage('Should have correct video channel id'),
50 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
51 logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files })
53 if (areValidationErrors(req, res)) return
54 if (areErrorsInScheduleUpdate(req, res)) return
56 const videoFile: Express.Multer.File = req.files['videofile'][0]
57 const user = res.locals.oauth.token.User
59 if (!await isVideoChannelOfAccountExist(req.body.channelId, user, res)) return
61 const isAble = await user.isAbleToUploadVideo(videoFile)
62 if (isAble === false) {
64 .json({ error: 'The user video quota is exceeded with this video.' })
73 duration = await getDurationFromVideoFile(videoFile.path)
75 logger.error('Invalid input file in videosAddValidator.', { err })
77 .json({ error: 'Invalid input file.' })
83 videoFile['duration'] = duration
89 const videosUpdateValidator = getCommonVideoAttributes().concat([
90 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
93 .custom(isVideoNameValid).withMessage('Should have a valid name'),
97 .custom(isIdValid).withMessage('Should have correct video channel id'),
99 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
100 logger.debug('Checking videosUpdate parameters', { parameters: req.body })
102 if (areValidationErrors(req, res)) return
103 if (areErrorsInScheduleUpdate(req, res)) return
104 if (!await isVideoExist(req.params.id, res)) return
106 const video = res.locals.video
108 // Check if the user who did the request is able to update the video
109 const user = res.locals.oauth.token.User
110 if (!checkUserCanManageVideo(user, res.locals.video, UserRight.UPDATE_ANY_VIDEO, res)) return
112 if (video.privacy !== VideoPrivacy.PRIVATE && req.body.privacy === VideoPrivacy.PRIVATE) {
113 return res.status(409)
114 .json({ error: 'Cannot set "private" a video that was not private.' })
118 if (req.body.channelId && !await isVideoChannelOfAccountExist(req.body.channelId, user, res)) return
124 const videosGetValidator = [
125 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
127 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
128 logger.debug('Checking videosGet parameters', { parameters: req.params })
130 if (areValidationErrors(req, res)) return
131 if (!await isVideoExist(req.params.id, res)) return
133 const video = res.locals.video
135 // Video is public, anyone can access it
136 if (video.privacy === VideoPrivacy.PUBLIC) return next()
138 // Video is unlisted, check we used the uuid to fetch it
139 if (video.privacy === VideoPrivacy.UNLISTED) {
140 if (isUUIDValid(req.params.id)) return next()
142 // Don't leak this unlisted video
143 return res.status(404).end()
146 // Video is private, check the user
147 authenticate(req, res, () => {
148 if (video.VideoChannel.Account.userId !== res.locals.oauth.token.User.id) {
149 return res.status(403)
150 .json({ error: 'Cannot get this private video of another user' })
159 const videosRemoveValidator = [
160 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
162 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
163 logger.debug('Checking videosRemove parameters', { parameters: req.params })
165 if (areValidationErrors(req, res)) return
166 if (!await isVideoExist(req.params.id, res)) return
168 // Check if the user who did the request is able to delete the video
169 if (!checkUserCanManageVideo(res.locals.oauth.token.User, res.locals.video, UserRight.REMOVE_ANY_VIDEO, res)) return
175 const videosSearchValidator = [
176 query('search').not().isEmpty().withMessage('Should have a valid search'),
178 (req: express.Request, res: express.Response, next: express.NextFunction) => {
179 logger.debug('Checking videosSearch parameters', { parameters: req.params })
181 if (areValidationErrors(req, res)) return
187 const videoAbuseReportValidator = [
188 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
189 body('reason').custom(isVideoAbuseReasonValid).withMessage('Should have a valid reason'),
191 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
192 logger.debug('Checking videoAbuseReport parameters', { parameters: req.body })
194 if (areValidationErrors(req, res)) return
195 if (!await isVideoExist(req.params.id, res)) return
201 const videoRateValidator = [
202 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
203 body('rating').custom(isVideoRatingTypeValid).withMessage('Should have a valid rate type'),
205 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
206 logger.debug('Checking videoRate parameters', { parameters: req.body })
208 if (areValidationErrors(req, res)) return
209 if (!await isVideoExist(req.params.id, res)) return
215 const videosShareValidator = [
216 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
217 param('accountId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid account id'),
219 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
220 logger.debug('Checking videoShare parameters', { parameters: req.params })
222 if (areValidationErrors(req, res)) return
223 if (!await isVideoExist(req.params.id, res)) return
225 const share = await VideoShareModel.load(req.params.accountId, res.locals.video.id, undefined)
227 return res.status(404)
231 res.locals.videoShare = share
236 // ---------------------------------------------------------------------------
240 videosUpdateValidator,
242 videosRemoveValidator,
243 videosSearchValidator,
244 videosShareValidator,
246 videoAbuseReportValidator,
251 // ---------------------------------------------------------------------------
253 function areErrorsInScheduleUpdate (req: express.Request, res: express.Response) {
254 if (req.body.scheduleUpdate) {
255 if (!req.body.scheduleUpdate.updateAt) {
257 .json({ error: 'Schedule update at is mandatory.' })
267 function getCommonVideoAttributes () {
269 body('thumbnailfile')
270 .custom((value, { req }) => isVideoImage(req.files, 'thumbnailfile')).withMessage(
271 'This thumbnail file is not supported or too large. Please, make sure it is of the following type: '
272 + CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME.join(', ')
275 .custom((value, { req }) => isVideoImage(req.files, 'previewfile')).withMessage(
276 'This preview file is not supported or too large. Please, make sure it is of the following type: '
277 + CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME.join(', ')
282 .customSanitizer(toIntOrNull)
283 .custom(isVideoCategoryValid).withMessage('Should have a valid category'),
286 .customSanitizer(toIntOrNull)
287 .custom(isVideoLicenceValid).withMessage('Should have a valid licence'),
290 .customSanitizer(toValueOrNull)
291 .custom(isVideoLanguageValid).withMessage('Should have a valid language'),
295 .custom(isBooleanValid).withMessage('Should have a valid NSFW attribute'),
296 body('waitTranscoding')
299 .custom(isBooleanValid).withMessage('Should have a valid wait transcoding attribute'),
303 .custom(isVideoPrivacyValid).withMessage('Should have correct video privacy'),
306 .customSanitizer(toValueOrNull)
307 .custom(isVideoDescriptionValid).withMessage('Should have a valid description'),
310 .customSanitizer(toValueOrNull)
311 .custom(isVideoSupportValid).withMessage('Should have a valid support text'),
314 .customSanitizer(toValueOrNull)
315 .custom(isVideoTagsValid).withMessage('Should have correct tags'),
316 body('commentsEnabled')
319 .custom(isBooleanValid).withMessage('Should have comments enabled boolean'),
321 body('scheduleUpdate')
323 .customSanitizer(toValueOrNull),
324 body('scheduleUpdate.updateAt')
326 .custom(isDateValid).withMessage('Should have a valid schedule update date'),
327 body('scheduleUpdate.privacy')
330 .custom(isScheduleVideoUpdatePrivacyValid).withMessage('Should have correct schedule update privacy')
331 ] as (ValidationChain | express.Handler)[]