2018-03-12 09:26:44 +01:00
|
|
|
'use strict';
|
2018-02-04 16:36:42 +01:00
|
|
|
|
|
|
|
// HTTP status codes by name
|
|
|
|
const codes = require('../routes/http-codes');
|
|
|
|
|
2019-02-03 11:49:48 +01:00
|
|
|
// library to check image dimensions from file buffer
|
2019-02-03 12:16:22 +01:00
|
|
|
const sizeOf = require('buffer-image-size');
|
2019-02-03 11:49:48 +01:00
|
|
|
|
2018-02-04 16:36:42 +01:00
|
|
|
/**
|
|
|
|
* check if id has valid UUID format
|
2018-03-12 10:39:56 +01:00
|
|
|
*
|
|
|
|
* @param {object} req
|
|
|
|
* @param {function} res
|
|
|
|
* @param {function} next
|
|
|
|
* @return {boolean}
|
2018-02-04 16:36:42 +01:00
|
|
|
*/
|
|
|
|
const idValidator = (req, res, next) => {
|
|
|
|
const reqId = req.params.id;
|
|
|
|
|
|
|
|
if (!reqId.match(/^[0-9a-fA-F]{24}$/)) {
|
2018-03-12 09:26:44 +01:00
|
|
|
const err = new Error('Invalid request id format');
|
2018-02-04 16:36:42 +01:00
|
|
|
err.status = codes.notfound;
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
};
|
|
|
|
|
2019-02-03 11:49:48 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-02-04 16:36:42 +01:00
|
|
|
exports.idValidator = idValidator;
|
2019-02-03 11:49:48 +01:00
|
|
|
exports.imageDimensionValidator = imageDimensionValidator;
|