From 2baea0c77cc765f7cbca9c9a2f4272268892a35c Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 14 Jun 2018 18:06:56 +0200 Subject: Add ability for uploaders to schedule video update --- server/middlewares/validators/videos.ts | 46 ++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 4 deletions(-) (limited to 'server/middlewares') diff --git a/server/middlewares/validators/videos.ts b/server/middlewares/validators/videos.ts index e181aebdb..9fe5a253b 100644 --- a/server/middlewares/validators/videos.ts +++ b/server/middlewares/validators/videos.ts @@ -2,8 +2,17 @@ import * as express from 'express' import 'express-validator' import { body, param, query } from 'express-validator/check' import { UserRight, VideoPrivacy } from '../../../shared' -import { isBooleanValid, isIdOrUUIDValid, isIdValid, isUUIDValid, toIntOrNull, toValueOrNull } from '../../helpers/custom-validators/misc' import { + isBooleanValid, + isDateValid, + isIdOrUUIDValid, + isIdValid, + isUUIDValid, + toIntOrNull, + toValueOrNull +} from '../../helpers/custom-validators/misc' +import { + isScheduleVideoUpdatePrivacyValid, isVideoAbuseReasonValid, isVideoCategoryValid, isVideoChannelOfAccountExist, @@ -84,14 +93,21 @@ const videosAddValidator = [ .custom(isVideoPrivacyValid).withMessage('Should have correct video privacy'), body('channelId') .toInt() - .custom(isIdValid) - .withMessage('Should have correct video channel id'), + .custom(isIdValid).withMessage('Should have correct video channel id'), + body('scheduleUpdate.updateAt') + .optional() + .custom(isDateValid).withMessage('Should have a valid schedule update date'), + body('scheduleUpdate.privacy') + .optional() + .toInt() + .custom(isScheduleVideoUpdatePrivacyValid).withMessage('Should have correct schedule update privacy'), async (req: express.Request, res: express.Response, next: express.NextFunction) => { logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files }) if (areValidationErrors(req, res)) return if (areErrorsInVideoImageFiles(req, res)) return + if (areErrorsInScheduleUpdate(req, res)) return const videoFile: Express.Multer.File = req.files['videofile'][0] const user = res.locals.oauth.token.User @@ -183,12 +199,20 @@ const videosUpdateValidator = [ .optional() .toInt() .custom(isIdValid).withMessage('Should have correct video channel id'), + body('scheduleUpdate.updateAt') + .optional() + .custom(isDateValid).withMessage('Should have a valid schedule update date'), + body('scheduleUpdate.privacy') + .optional() + .toInt() + .custom(isScheduleVideoUpdatePrivacyValid).withMessage('Should have correct schedule update privacy'), async (req: express.Request, res: express.Response, next: express.NextFunction) => { logger.debug('Checking videosUpdate parameters', { parameters: req.body }) if (areValidationErrors(req, res)) return if (areErrorsInVideoImageFiles(req, res)) return + if (areErrorsInScheduleUpdate(req, res)) return if (!await isVideoExist(req.params.id, res)) return const video = res.locals.video @@ -371,7 +395,7 @@ function areErrorsInVideoImageFiles (req: express.Request, res: express.Response const imageFile = req.files[ imageField ][ 0 ] as Express.Multer.File if (imageFile.size > CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max) { res.status(400) - .send({ error: `The size of the ${imageField} is too big (>${CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max}).` }) + .json({ error: `The size of the ${imageField} is too big (>${CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max}).` }) .end() return true } @@ -379,3 +403,17 @@ function areErrorsInVideoImageFiles (req: express.Request, res: express.Response return false } + +function areErrorsInScheduleUpdate (req: express.Request, res: express.Response) { + if (req.body.scheduleUpdate) { + if (!req.body.scheduleUpdate.updateAt) { + res.status(400) + .json({ error: 'Schedule update at is mandatory.' }) + .end() + + return true + } + } + + return false +} -- cgit v1.2.3