X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmiddlewares%2Fvalidators%2Fusers.ts;h=7de3e442ccec31cbabe8ccc295adfc45982eabc7;hb=01de67b9a4fcdf01102ccc3cb7dc24beebf6c7ea;hp=920176d07af046f8ff69441a15ffc05e6d5f05f6;hpb=3fd3ab2d34d512b160a5e6084d7609be7b4f4452;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/middlewares/validators/users.ts b/server/middlewares/validators/users.ts index 920176d07..7de3e442c 100644 --- a/server/middlewares/validators/users.ts +++ b/server/middlewares/validators/users.ts @@ -1,18 +1,18 @@ import * as express from 'express' import 'express-validator' import { body, param } from 'express-validator/check' -import { isSignupAllowed, logger } from '../../helpers' import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc' import { - isUserDisplayNSFWValid, - isUserPasswordValid, - isUserRoleValid, - isUserUsernameValid, + isAvatarFile, isUserAutoPlayVideoValid, isUserDisplayNSFWValid, isUserPasswordValid, isUserRoleValid, isUserUsernameValid, isUserVideoQuotaValid } from '../../helpers/custom-validators/users' import { isVideoExist } from '../../helpers/custom-validators/videos' +import { logger } from '../../helpers/logger' +import { isSignupAllowed } from '../../helpers/utils' +import { CONSTRAINTS_FIELDS } from '../../initializers' import { UserModel } from '../../models/account/user' import { areValidationErrors } from './utils' +import Multer = require('multer') const usersAddValidator = [ body('username').custom(isUserUsernameValid).withMessage('Should have a valid username (lowercase alphanumeric characters)'), @@ -86,6 +86,7 @@ const usersUpdateMeValidator = [ body('password').optional().custom(isUserPasswordValid).withMessage('Should have a valid password'), body('email').optional().isEmail().withMessage('Should have a valid email attribute'), body('displayNSFW').optional().custom(isUserDisplayNSFWValid).withMessage('Should have a valid display Not Safe For Work attribute'), + body('autoPlayVideo').optional().custom(isUserAutoPlayVideoValid).withMessage('Should have a valid automatically plays video attribute'), (req: express.Request, res: express.Response, next: express.NextFunction) => { // TODO: Add old password verification @@ -97,6 +98,29 @@ const usersUpdateMeValidator = [ } ] +const usersUpdateMyAvatarValidator = [ + body('avatarfile').custom((value, { req }) => isAvatarFile(req.files)).withMessage( + 'This file is not supported. Please, make sure it is of the following type : ' + + CONSTRAINTS_FIELDS.ACTORS.AVATAR.EXTNAME.join(', ') + ), + + (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking usersUpdateMyAvatarValidator parameters', { parameters: req.body }) + + if (areValidationErrors(req, res)) return + + const imageFile = req.files['avatarfile'][0] as Express.Multer.File + if (imageFile.size > CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max) { + res.status(400) + .send({ error: `The size of the avatar is too big (>${CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max}).` }) + .end() + return + } + + return next() + } +] + const usersGetValidator = [ param('id').isInt().not().isEmpty().withMessage('Should have a valid id'), @@ -146,7 +170,8 @@ export { usersUpdateMeValidator, usersVideoRatingValidator, ensureUserRegistrationAllowed, - usersGetValidator + usersGetValidator, + usersUpdateMyAvatarValidator } // ---------------------------------------------------------------------------