checking dimensions of the images

This commit is contained in:
Arjun Patel
2019-04-09 03:27:28 -07:00
parent 3539f7aef9
commit 750dbbc96e
4 changed files with 30 additions and 3 deletions
+19 -2
View File
@@ -7,6 +7,7 @@ const fs = require('fs');
const fileType = require('file-type');
const bluebird = require('bluebird');
const multiparty = require('multiparty');
var sizeOf = require('image-size');
// configure the keys for accessing AWS
AWS.config.update({
@@ -35,6 +36,8 @@ const uploadFile = (buffer, name, type) => {
};
exports.uploadProfilePic = async (req, res, next) => {
const charity_id = req.user.id;
const form = new multiparty.Form();
form.parse(req, async (error, fields, files) => {
@@ -42,19 +45,33 @@ exports.uploadProfilePic = async (req, res, next) => {
try {
const path = files.file[0].path;
const buffer = fs.readFileSync(path);
const dimensions = sizeOf(buffer);
const ratio = dimensions.height / parseFloat(dimensions.width);
if (
dimensions.height > 800 ||
dimensions.width > 800 ||
dimensions.height < 100 ||
dimensions.width < 100 ||
ratio > 1.5 ||
ratio < 0.5
)
return next(errorMaker(400, 'Invalid image dimensions or type'));
const type = fileType(buffer);
const timestamp = Date.now().toString();
const fileName = `bucketFolder/${timestamp}-lg`;
const fileName = `charityProfilePic/${timestamp}-lg`;
const data = await uploadFile(buffer, fileName, type);
await sequelize.query(
`
UPDATE organizations
SET profile_pic_url = :profilePicUrl
WHERE id = :charity_id
`,
{
type: db.Sequelize.QueryTypes.UPDATE,
replacements: { profilePicUrl: data.Location }
replacements: { profilePicUrl: data.Location, charity_id }
}
);
+5 -1
View File
@@ -16,7 +16,11 @@ const {
router.post('/', organization.createOrganization);
// Charity upload profile pic
router.post('/profilepic', image.uploadProfilePic);
router.post(
'/profilepic',
checkAuth(roles.ORGANIZATION),
image.uploadProfilePic
);
// GET Activate org's stripe connected account
router.get('/stripe/connect', stripe.activateStripeAccount);
+5
View File
@@ -840,6 +840,11 @@
"resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.1.8.tgz",
"integrity": "sha1-vjPUCsEO8ZJnAfbwii2G+/0a0+Q="
},
"image-size": {
"version": "0.7.3",
"resolved": "https://registry.npmjs.org/image-size/-/image-size-0.7.3.tgz",
"integrity": "sha512-CgCZhKUtnwgCV/wh28LGDWdIWhbqq64DAL0c6kjtAtqrKbMr/EZ0yAUXQXdPBsGtQxWQdK6P3itDVfDJ7tjlvA=="
},
"inflection": {
"version": "1.12.0",
"resolved": "https://registry.npmjs.org/inflection/-/inflection-1.12.0.tgz",
+1
View File
@@ -31,6 +31,7 @@
"express": "^4.16.4",
"file-type": "^10.10.0",
"fs": "0.0.1-security",
"image-size": "^0.7.3",
"jsonwebtoken": "^8.4.0",
"morgan": "^1.9.1",
"multiparty": "^4.2.1",