From 0d26b21a2c79bfc2b514d80024e1ecbbdd860e54 Mon Sep 17 00:00:00 2001 From: Baris Soner Usakli Date: Wed, 8 Jan 2014 02:59:14 -0500 Subject: [PATCH] prevent double res.send/json if error occurs in profile image upload --- src/routes/user.js | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/src/routes/user.js b/src/routes/user.js index 144cf08508..d279de0870 100644 --- a/src/routes/user.js +++ b/src/routes/user.js @@ -176,23 +176,25 @@ var fs = require('fs'), user.setUserField(uid, 'picture', imageUrl); if (convertToPNG && extension !== '.png') { - im.convert([uploadPath, 'png:-'], - function(err, stdout){ - if (err) { - winston.err(err); - res.send({ - error: 'Unable to convert image to PNG.' - }); - return; - } - - fs.writeFileSync(uploadPath, stdout, 'binary'); + im.convert([uploadPath, 'png:-'], function(err, stdout) { + if (err) { + winston.err(err); + res.send({ + error: 'Unable to convert image to PNG.' + }); + return; + } + + fs.writeFileSync(uploadPath, stdout, 'binary'); + res.json({ + path: imageUrl }); + }); + } else { + res.json({ + path: imageUrl + }); } - - res.json({ - path: imageUrl - }); } if(extension === '.gif') {