checking dimensions of the images
This commit is contained in:
@@ -7,6 +7,7 @@ const fs = require('fs');
|
|||||||
const fileType = require('file-type');
|
const fileType = require('file-type');
|
||||||
const bluebird = require('bluebird');
|
const bluebird = require('bluebird');
|
||||||
const multiparty = require('multiparty');
|
const multiparty = require('multiparty');
|
||||||
|
var sizeOf = require('image-size');
|
||||||
|
|
||||||
// configure the keys for accessing AWS
|
// configure the keys for accessing AWS
|
||||||
AWS.config.update({
|
AWS.config.update({
|
||||||
@@ -35,6 +36,8 @@ const uploadFile = (buffer, name, type) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
exports.uploadProfilePic = async (req, res, next) => {
|
exports.uploadProfilePic = async (req, res, next) => {
|
||||||
|
const charity_id = req.user.id;
|
||||||
|
|
||||||
const form = new multiparty.Form();
|
const form = new multiparty.Form();
|
||||||
|
|
||||||
form.parse(req, async (error, fields, files) => {
|
form.parse(req, async (error, fields, files) => {
|
||||||
@@ -42,19 +45,33 @@ exports.uploadProfilePic = async (req, res, next) => {
|
|||||||
try {
|
try {
|
||||||
const path = files.file[0].path;
|
const path = files.file[0].path;
|
||||||
const buffer = fs.readFileSync(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 type = fileType(buffer);
|
||||||
const timestamp = Date.now().toString();
|
const timestamp = Date.now().toString();
|
||||||
const fileName = `bucketFolder/${timestamp}-lg`;
|
const fileName = `charityProfilePic/${timestamp}-lg`;
|
||||||
const data = await uploadFile(buffer, fileName, type);
|
const data = await uploadFile(buffer, fileName, type);
|
||||||
|
|
||||||
await sequelize.query(
|
await sequelize.query(
|
||||||
`
|
`
|
||||||
UPDATE organizations
|
UPDATE organizations
|
||||||
SET profile_pic_url = :profilePicUrl
|
SET profile_pic_url = :profilePicUrl
|
||||||
|
WHERE id = :charity_id
|
||||||
`,
|
`,
|
||||||
{
|
{
|
||||||
type: db.Sequelize.QueryTypes.UPDATE,
|
type: db.Sequelize.QueryTypes.UPDATE,
|
||||||
replacements: { profilePicUrl: data.Location }
|
replacements: { profilePicUrl: data.Location, charity_id }
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,11 @@ const {
|
|||||||
router.post('/', organization.createOrganization);
|
router.post('/', organization.createOrganization);
|
||||||
|
|
||||||
// Charity upload profile pic
|
// Charity upload profile pic
|
||||||
router.post('/profilepic', image.uploadProfilePic);
|
router.post(
|
||||||
|
'/profilepic',
|
||||||
|
checkAuth(roles.ORGANIZATION),
|
||||||
|
image.uploadProfilePic
|
||||||
|
);
|
||||||
|
|
||||||
// GET Activate org's stripe connected account
|
// GET Activate org's stripe connected account
|
||||||
router.get('/stripe/connect', stripe.activateStripeAccount);
|
router.get('/stripe/connect', stripe.activateStripeAccount);
|
||||||
|
|||||||
Generated
+5
@@ -840,6 +840,11 @@
|
|||||||
"resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.1.8.tgz",
|
"resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.1.8.tgz",
|
||||||
"integrity": "sha1-vjPUCsEO8ZJnAfbwii2G+/0a0+Q="
|
"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": {
|
"inflection": {
|
||||||
"version": "1.12.0",
|
"version": "1.12.0",
|
||||||
"resolved": "https://registry.npmjs.org/inflection/-/inflection-1.12.0.tgz",
|
"resolved": "https://registry.npmjs.org/inflection/-/inflection-1.12.0.tgz",
|
||||||
|
|||||||
@@ -31,6 +31,7 @@
|
|||||||
"express": "^4.16.4",
|
"express": "^4.16.4",
|
||||||
"file-type": "^10.10.0",
|
"file-type": "^10.10.0",
|
||||||
"fs": "0.0.1-security",
|
"fs": "0.0.1-security",
|
||||||
|
"image-size": "^0.7.3",
|
||||||
"jsonwebtoken": "^8.4.0",
|
"jsonwebtoken": "^8.4.0",
|
||||||
"morgan": "^1.9.1",
|
"morgan": "^1.9.1",
|
||||||
"multiparty": "^4.2.1",
|
"multiparty": "^4.2.1",
|
||||||
|
|||||||
Reference in New Issue
Block a user