aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators/videos.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/middlewares/validators/videos.ts')
-rw-r--r--server/middlewares/validators/videos.ts51
1 files changed, 49 insertions, 2 deletions
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'
4import { UserRight, VideoPrivacy } from '../../../shared' 4import { UserRight, VideoPrivacy } from '../../../shared'
5import { isBooleanValid, isIdOrUUIDValid, isIdValid, isUUIDValid } from '../../helpers/custom-validators/misc' 5import { isBooleanValid, isIdOrUUIDValid, isIdValid, isUUIDValid } from '../../helpers/custom-validators/misc'
6import { 6import {
7 isVideoAbuseReasonValid, isVideoCategoryValid, isVideoDescriptionValid, isVideoExist, isVideoFile, isVideoLanguageValid, 7 isVideoAbuseReasonValid,
8 isVideoLicenceValid, isVideoNameValid, isVideoPrivacyValid, isVideoRatingTypeValid, isVideoTagsValid 8 isVideoCategoryValid,
9 isVideoDescriptionValid,
10 isVideoExist,
11 isVideoFile,
12 isVideoImage,
13 isVideoLanguageValid,
14 isVideoLicenceValid,
15 isVideoNameValid,
16 isVideoPrivacyValid,
17 isVideoRatingTypeValid,
18 isVideoTagsValid
9} from '../../helpers/custom-validators/videos' 19} from '../../helpers/custom-validators/videos'
10import { getDurationFromVideoFile } from '../../helpers/ffmpeg-utils' 20import { getDurationFromVideoFile } from '../../helpers/ffmpeg-utils'
11import { logger } from '../../helpers/logger' 21import { logger } from '../../helpers/logger'
@@ -22,6 +32,14 @@ const videosAddValidator = [
22 'This file is not supported. Please, make sure it is of the following type : ' 32 'This file is not supported. Please, make sure it is of the following type : '
23 + CONSTRAINTS_FIELDS.VIDEOS.EXTNAME.join(', ') 33 + CONSTRAINTS_FIELDS.VIDEOS.EXTNAME.join(', ')
24 ), 34 ),
35 body('thumbnailfile').custom((value, { req }) => isVideoImage(req.files, 'thumbnailfile')).withMessage(
36 'This thumbnail file is not supported. Please, make sure it is of the following type : '
37 + CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME.join(', ')
38 ),
39 body('previewfile').custom((value, { req }) => isVideoImage(req.files, 'previewfile')).withMessage(
40 'This preview file is not supported. Please, make sure it is of the following type : '
41 + CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME.join(', ')
42 ),
25 body('name').custom(isVideoNameValid).withMessage('Should have a valid name'), 43 body('name').custom(isVideoNameValid).withMessage('Should have a valid name'),
26 body('category').optional().custom(isVideoCategoryValid).withMessage('Should have a valid category'), 44 body('category').optional().custom(isVideoCategoryValid).withMessage('Should have a valid category'),
27 body('licence').optional().custom(isVideoLicenceValid).withMessage('Should have a valid licence'), 45 body('licence').optional().custom(isVideoLicenceValid).withMessage('Should have a valid licence'),
@@ -37,6 +55,7 @@ const videosAddValidator = [
37 logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files }) 55 logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files })
38 56
39 if (areValidationErrors(req, res)) return 57 if (areValidationErrors(req, res)) return
58 if (areErrorsInVideoImageFiles(req, res)) return
40 59
41 const videoFile: Express.Multer.File = req.files['videofile'][0] 60 const videoFile: Express.Multer.File = req.files['videofile'][0]
42 const user = res.locals.oauth.token.User 61 const user = res.locals.oauth.token.User
@@ -82,6 +101,14 @@ const videosAddValidator = [
82 101
83const videosUpdateValidator = [ 102const videosUpdateValidator = [
84 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), 103 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
104 body('thumbnailfile').custom((value, { req }) => isVideoImage(req.files, 'thumbnailfile')).withMessage(
105 'This thumbnail file is not supported. Please, make sure it is of the following type : '
106 + CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME.join(', ')
107 ),
108 body('previewfile').custom((value, { req }) => isVideoImage(req.files, 'previewfile')).withMessage(
109 'This preview file is not supported. Please, make sure it is of the following type : '
110 + CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME.join(', ')
111 ),
85 body('name').optional().custom(isVideoNameValid).withMessage('Should have a valid name'), 112 body('name').optional().custom(isVideoNameValid).withMessage('Should have a valid name'),
86 body('category').optional().custom(isVideoCategoryValid).withMessage('Should have a valid category'), 113 body('category').optional().custom(isVideoCategoryValid).withMessage('Should have a valid category'),
87 body('licence').optional().custom(isVideoLicenceValid).withMessage('Should have a valid licence'), 114 body('licence').optional().custom(isVideoLicenceValid).withMessage('Should have a valid licence'),
@@ -96,6 +123,7 @@ const videosUpdateValidator = [
96 logger.debug('Checking videosUpdate parameters', { parameters: req.body }) 123 logger.debug('Checking videosUpdate parameters', { parameters: req.body })
97 124
98 if (areValidationErrors(req, res)) return 125 if (areValidationErrors(req, res)) return
126 if (areErrorsInVideoImageFiles(req, res)) return
99 if (!await isVideoExist(req.params.id, res)) return 127 if (!await isVideoExist(req.params.id, res)) return
100 128
101 const video = res.locals.video 129 const video = res.locals.video
@@ -274,3 +302,22 @@ function checkUserCanDeleteVideo (user: UserModel, video: VideoModel, res: expre
274 302
275 return true 303 return true
276} 304}
305
306function areErrorsInVideoImageFiles (req: express.Request, res: express.Response) {
307 // Files are optional
308 if (!req.files) return false
309
310 for (const imageField of [ 'thumbnail', 'preview' ]) {
311 if (!req.files[ imageField ]) continue
312
313 const imageFile = req.files[ imageField ][ 0 ] as Express.Multer.File
314 if (imageFile.size > CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max) {
315 res.status(400)
316 .send({ error: `The size of the ${imageField} is too big (>${CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max}).` })
317 .end()
318 return true
319 }
320 }
321
322 return false
323}