X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmiddlewares%2Fvalidators%2Fusers.ts;h=d22a745b42a34cce17d151b5c6012651e7c79589;hb=e8e122002d5a6a2bedcf3d66d35657c9b9e1ebaf;hp=ac7435b7d3c647157f8490f04c0159d8ec689012;hpb=a2431b7dcbc72c05101dcdbe631ff84a823aeb51;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/middlewares/validators/users.ts b/server/middlewares/validators/users.ts index ac7435b7d..d22a745b4 100644 --- a/server/middlewares/validators/users.ts +++ b/server/middlewares/validators/users.ts @@ -1,18 +1,16 @@ import * as express from 'express' import 'express-validator' import { body, param } from 'express-validator/check' +import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc' import { - isIdOrUUIDValid, - isSignupAllowed, - isUserDisplayNSFWValid, - isUserPasswordValid, - isUserRoleValid, - isUserUsernameValid, - isUserVideoQuotaValid, - logger -} from '../../helpers' + isAvatarFile, isUserAutoPlayVideoValid, isUserDisplayNSFWValid, isUserPasswordValid, isUserRoleValid, isUserUsernameValid, + isUserVideoQuotaValid +} from '../../helpers/custom-validators/users' import { isVideoExist } from '../../helpers/custom-validators/videos' -import { database as db } from '../../initializers/database' +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' const usersAddValidator = [ @@ -87,6 +85,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 @@ -98,6 +97,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', { files: req.files }) + + 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'), @@ -147,13 +169,14 @@ export { usersUpdateMeValidator, usersVideoRatingValidator, ensureUserRegistrationAllowed, - usersGetValidator + usersGetValidator, + usersUpdateMyAvatarValidator } // --------------------------------------------------------------------------- async function checkUserIdExist (id: number, res: express.Response) { - const user = await db.User.loadById(id) + const user = await UserModel.loadById(id) if (!user) { res.status(404) @@ -168,7 +191,7 @@ async function checkUserIdExist (id: number, res: express.Response) { } async function checkUserNameOrEmailDoesNotAlreadyExist (username: string, email: string, res: express.Response) { - const user = await db.User.loadByUsernameOrEmail(username, email) + const user = await UserModel.loadByUsernameOrEmail(username, email) if (user) { res.status(409)