From 433c7d91eb478b39fcddd5674db0f6ddd875e65d Mon Sep 17 00:00:00 2001 From: Baris Usakli Date: Thu, 25 Jul 2013 14:44:43 -0400 Subject: [PATCH 1/4] some fixes --- src/routes/user.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/routes/user.js b/src/routes/user.js index 0be3abcf12..4c98b6c81f 100644 --- a/src/routes/user.js +++ b/src/routes/user.js @@ -217,6 +217,7 @@ var user = require('./../user.js'), function api_method(req, res) { var callerUID = req.user ? req.user.uid : 0; + console.log(req.user); var userslug = req.params.userslug; var section = req.params.section ? String(req.params.section).toLowerCase() : null; @@ -297,7 +298,7 @@ var user = require('./../user.js'), res.json(404, { error: 'User not found!' }) ; return; } - uid = parseInt(uid, 10); + if(uid !== callerUid || callerUid === "0") { res.json(403, { error: 'Not allowed!' }); return; @@ -357,12 +358,10 @@ var user = require('./../user.js'), user.get_uid_by_userslug(userslug, function(uid) { if(uid === null) { - callback(null); + callback(null) return; } - - uid = parseInt(uid, 10); - + user.getUserData(uid, function(data) { if(data) { data.joindate = utils.relativeTime(data.joindate); From 6420e4c6a6db0d513e9946eab616fc14279ec2f3 Mon Sep 17 00:00:00 2001 From: Baris Usakli Date: Thu, 25 Jul 2013 14:51:31 -0400 Subject: [PATCH 2/4] added this back, file extensions might change need to delete old pic --- src/routes/user.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/routes/user.js b/src/routes/user.js index 4c98b6c81f..1f53282806 100644 --- a/src/routes/user.js +++ b/src/routes/user.js @@ -126,7 +126,22 @@ var user = require('./../user.js'), return; } - uploadUserPicture(req.user.uid, path.extname(req.files.userPhoto.name), req.files.userPhoto.path, res); + user.getUserField(req.user.uid, 'uploadedpicture', function(oldpicture) { + if(!oldpicture) { + uploadUserPicture(req.user.uid, path.extname(req.files.userPhoto.name), req.files.userPhoto.path, res); + return; + } + + var absolutePath = path.join(global.configuration['ROOT_DIRECTORY'], global.nconf.get('upload_path'), path.basename(oldpicture)); + + fs.unlink(absolutePath, function(err) { + if(err) { + console.error('[%d] %s', Date.now(), + err); + } + + uploadUserPicture(req.user.uid, path.extname(req.files.userPhoto.name), req.files.userPhoto.path, res); + }); + }); }); function uploadUserPicture(uid, extension, tempPath, res) { From 9160a135a922c0194066b9b667cc7961aa99ca0a Mon Sep 17 00:00:00 2001 From: Baris Usakli Date: Thu, 25 Jul 2013 15:19:44 -0400 Subject: [PATCH 3/4] changed to crop --- src/routes/user.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/routes/user.js b/src/routes/user.js index 1f53282806..7beb456b85 100644 --- a/src/routes/user.js +++ b/src/routes/user.js @@ -171,13 +171,12 @@ var user = require('./../user.js'), user.setUserField(uid, 'uploadedpicture', imageUrl); user.setUserField(uid, 'picture', imageUrl); - var im = require('node-imagemagick'); - - im.resize({ + require('node-imagemagick').crop({ srcPath: uploadPath, - dstPath: uploadPath, - width: 128 - }, function(err, stdout, stderr) { + dstPath: uploadPath, + width: 128, + height: 128 + }, function(err, stdout, stderr){ if (err) { // @todo: better logging method; for now, send to stderr. // ideally, this should be happening in another process @@ -232,7 +231,7 @@ var user = require('./../user.js'), function api_method(req, res) { var callerUID = req.user ? req.user.uid : 0; - console.log(req.user); + var userslug = req.params.userslug; var section = req.params.section ? String(req.params.section).toLowerCase() : null; @@ -373,7 +372,7 @@ var user = require('./../user.js'), user.get_uid_by_userslug(userslug, function(uid) { if(uid === null) { - callback(null) + callback(null); return; } From 58ad74abb5d13df98821d19dd748a6d8643f3e07 Mon Sep 17 00:00:00 2001 From: Baris Usakli Date: Thu, 25 Jul 2013 15:26:35 -0400 Subject: [PATCH 4/4] send new url when we are done --- src/routes/user.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/routes/user.js b/src/routes/user.js index 7beb456b85..9c332e59a5 100644 --- a/src/routes/user.js +++ b/src/routes/user.js @@ -166,8 +166,6 @@ var user = require('./../user.js'), var imageUrl = global.nconf.get('upload_url') + filename; - res.json({ path: imageUrl }); - user.setUserField(uid, 'uploadedpicture', imageUrl); user.setUserField(uid, 'picture', imageUrl); @@ -184,6 +182,8 @@ var user = require('./../user.js'), // to crash the main process console.error('[%d] %s', Date.now(), + err); } + + res.json({ path: imageUrl }); }); });