2018-06-29 04:29:23 -05:00
|
|
|
import * as express from 'express'
|
2019-07-25 09:23:44 -05:00
|
|
|
import { body } from 'express-validator'
|
2018-06-29 04:29:23 -05:00
|
|
|
import { isAvatarFile } from '../../helpers/custom-validators/users'
|
|
|
|
import { areValidationErrors } from './utils'
|
2019-04-11 07:26:41 -05:00
|
|
|
import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
|
2018-06-29 04:29:23 -05:00
|
|
|
import { logger } from '../../helpers/logger'
|
2018-08-14 08:28:30 -05:00
|
|
|
import { cleanUpReqFiles } from '../../helpers/express-utils'
|
2018-06-29 04:29:23 -05:00
|
|
|
|
2021-04-06 10:01:35 -05:00
|
|
|
const updateActorImageValidatorFactory = (fieldname: string) => ([
|
|
|
|
body(fieldname).custom((value, { req }) => isAvatarFile(req.files)).withMessage(
|
2020-01-31 09:56:52 -06:00
|
|
|
'This file is not supported or too large. Please, make sure it is of the following type : ' +
|
2021-04-06 10:01:35 -05:00
|
|
|
CONSTRAINTS_FIELDS.ACTORS.IMAGE.EXTNAME.join(', ')
|
2018-06-29 04:29:23 -05:00
|
|
|
),
|
|
|
|
|
|
|
|
(req: express.Request, res: express.Response, next: express.NextFunction) => {
|
2021-04-06 10:01:35 -05:00
|
|
|
logger.debug('Checking updateActorImageValidator parameters', { files: req.files })
|
2018-06-29 04:29:23 -05:00
|
|
|
|
2018-07-31 08:09:34 -05:00
|
|
|
if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
|
2018-06-29 04:29:23 -05:00
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
2021-04-06 10:01:35 -05:00
|
|
|
])
|
|
|
|
|
|
|
|
const updateAvatarValidator = updateActorImageValidatorFactory('avatarfile')
|
|
|
|
const updateBannerValidator = updateActorImageValidatorFactory('bannerfile')
|
2018-06-29 04:29:23 -05:00
|
|
|
|
|
|
|
export {
|
2021-04-06 10:01:35 -05:00
|
|
|
updateAvatarValidator,
|
|
|
|
updateBannerValidator
|
2018-06-29 04:29:23 -05:00
|
|
|
}
|