From ac81d1a06d57b9ae86663831e7f5edcef57b0fa4 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 13 Feb 2018 18:17:05 +0100 Subject: Add ability to set video thumbnail/preview --- server/middlewares/validators/videos.ts | 51 +++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) (limited to 'server/middlewares/validators') diff --git a/server/middlewares/validators/videos.ts b/server/middlewares/validators/videos.ts index a365ed217..6d4fb907b 100644 --- a/server/middlewares/validators/videos.ts +++ b/server/middlewares/validators/videos.ts @@ -4,8 +4,18 @@ import { body, param, query } from 'express-validator/check' import { UserRight, VideoPrivacy } from '../../../shared' import { isBooleanValid, isIdOrUUIDValid, isIdValid, isUUIDValid } from '../../helpers/custom-validators/misc' import { - isVideoAbuseReasonValid, isVideoCategoryValid, isVideoDescriptionValid, isVideoExist, isVideoFile, isVideoLanguageValid, - isVideoLicenceValid, isVideoNameValid, isVideoPrivacyValid, isVideoRatingTypeValid, isVideoTagsValid + isVideoAbuseReasonValid, + isVideoCategoryValid, + isVideoDescriptionValid, + isVideoExist, + isVideoFile, + isVideoImage, + isVideoLanguageValid, + isVideoLicenceValid, + isVideoNameValid, + isVideoPrivacyValid, + isVideoRatingTypeValid, + isVideoTagsValid } from '../../helpers/custom-validators/videos' import { getDurationFromVideoFile } from '../../helpers/ffmpeg-utils' import { logger } from '../../helpers/logger' @@ -22,6 +32,14 @@ const videosAddValidator = [ 'This file is not supported. Please, make sure it is of the following type : ' + CONSTRAINTS_FIELDS.VIDEOS.EXTNAME.join(', ') ), + body('thumbnailfile').custom((value, { req }) => isVideoImage(req.files, 'thumbnailfile')).withMessage( + 'This thumbnail file is not supported. Please, make sure it is of the following type : ' + + CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME.join(', ') + ), + body('previewfile').custom((value, { req }) => isVideoImage(req.files, 'previewfile')).withMessage( + 'This preview file is not supported. Please, make sure it is of the following type : ' + + CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME.join(', ') + ), body('name').custom(isVideoNameValid).withMessage('Should have a valid name'), body('category').optional().custom(isVideoCategoryValid).withMessage('Should have a valid category'), body('licence').optional().custom(isVideoLicenceValid).withMessage('Should have a valid licence'), @@ -37,6 +55,7 @@ const videosAddValidator = [ logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files }) if (areValidationErrors(req, res)) return + if (areErrorsInVideoImageFiles(req, res)) return const videoFile: Express.Multer.File = req.files['videofile'][0] const user = res.locals.oauth.token.User @@ -82,6 +101,14 @@ const videosAddValidator = [ const videosUpdateValidator = [ param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), + body('thumbnailfile').custom((value, { req }) => isVideoImage(req.files, 'thumbnailfile')).withMessage( + 'This thumbnail file is not supported. Please, make sure it is of the following type : ' + + CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME.join(', ') + ), + body('previewfile').custom((value, { req }) => isVideoImage(req.files, 'previewfile')).withMessage( + 'This preview file is not supported. Please, make sure it is of the following type : ' + + CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME.join(', ') + ), body('name').optional().custom(isVideoNameValid).withMessage('Should have a valid name'), body('category').optional().custom(isVideoCategoryValid).withMessage('Should have a valid category'), body('licence').optional().custom(isVideoLicenceValid).withMessage('Should have a valid licence'), @@ -96,6 +123,7 @@ const videosUpdateValidator = [ logger.debug('Checking videosUpdate parameters', { parameters: req.body }) if (areValidationErrors(req, res)) return + if (areErrorsInVideoImageFiles(req, res)) return if (!await isVideoExist(req.params.id, res)) return const video = res.locals.video @@ -274,3 +302,22 @@ function checkUserCanDeleteVideo (user: UserModel, video: VideoModel, res: expre return true } + +function areErrorsInVideoImageFiles (req: express.Request, res: express.Response) { + // Files are optional + if (!req.files) return false + + for (const imageField of [ 'thumbnail', 'preview' ]) { + if (!req.files[ imageField ]) continue + + 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}).` }) + .end() + return true + } + } + + return false +} -- cgit v1.2.3