'use strict'; // HTTP status codes by name const codes = require('../routes/http-codes'); // library to check image dimensions from file buffer const sizeOf = require('buffer-image-size'); /** * check if id has valid UUID format * * @param {object} req * @param {function} res * @param {function} next * @return {boolean} */ const idValidator = (req, res, next) => { const reqId = req.params.id; if (!reqId.match(/^[0-9a-fA-F]{24}$/)) { const err = new Error('Invalid request id format'); err.status = codes.notfound; return next(err); } next(); }; const imageDimensionValidator = (imageFileBuf, maxWidth, maxHeight) => { const dimensions = sizeOf(imageFileBuf); if (dimensions.width > maxWidth || dimensions.height > maxHeight) { let err = new Error(`Image exceeds maximum dimensions of ${maxWidth}px width and ${maxHeight}px height`); err.status = codes.wrongrequest; return err; } }; exports.idValidator = idValidator; exports.imageDimensionValidator = imageDimensionValidator;