aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators/avatar.ts
blob: f7eb367bd6d5ce6bffe7b3d82aa3b8f4f7cafd2e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import * as express from 'express'
import { body } from 'express-validator'
import { isAvatarFile } from '../../helpers/custom-validators/users'
import { areValidationErrors } from './utils'
import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
import { logger } from '../../helpers/logger'
import { cleanUpReqFiles } from '../../helpers/express-utils'

const updateActorImageValidatorFactory = (fieldname: string) => ([
  body(fieldname).custom((value, { req }) => isAvatarFile(req.files)).withMessage(
    'This file is not supported or too large. Please, make sure it is of the following type : ' +
    CONSTRAINTS_FIELDS.ACTORS.IMAGE.EXTNAME.join(', ')
  ),

  (req: express.Request, res: express.Response, next: express.NextFunction) => {
    logger.debug('Checking updateActorImageValidator parameters', { files: req.files })

    if (areValidationErrors(req, res)) return cleanUpReqFiles(req)

    return next()
  }
])

const updateAvatarValidator = updateActorImageValidatorFactory('avatarfile')
const updateBannerValidator = updateActorImageValidatorFactory('bannerfile')

export {
  updateAvatarValidator,
  updateBannerValidator
}