Skip to content
This repository has been archived by the owner on Aug 30, 2021. It is now read-only.

Commit

Permalink
fix(user): fix changeProfilePicture to overwrite existing files
Browse files Browse the repository at this point in the history
* fix(user): fix changeProfilePicture

* use promises to simplify callbacks

* use fs.unlink to delete old picture once the profile is updated

Fixes #1415

* fix(user): fix changeProfilePicture

* use promises to simplify callbacks

* use fs.unlink to delete old picture once the profile is updated

* log file errors to console

Fixes #1415

* fix(user): fix changeProfilePicture

* use promises to simplify callbacks

* use fs.unlink to delete old picture once the profile is updated

* log file errors to console

* update error handler module to handle file upload errors

Fixes #1415

* fix(user): fix changeProfilePicture

* use promises to simplify callbacks

* use fs.unlink to delete old picture once the profile is updated

* log file errors to console

* update error handler module to handle file upload errors

Fixes #1415
  • Loading branch information
aanev authored and lirantal committed Aug 26, 2016
1 parent 246c5d9 commit 234f0c6
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 21 deletions.
14 changes: 14 additions & 0 deletions modules/core/server/controllers/errors.server.controller.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
'use strict';

/**
* Module dependencies
*/
var path = require('path'),
config = require(path.resolve('./config/config'));

/**
* Get unique error field name
*/
Expand Down Expand Up @@ -39,9 +45,17 @@ exports.getErrorMessage = function (err) {
case 11001:
message = getUniqueErrorMessage(err);
break;
case 'LIMIT_FILE_SIZE':
message = 'Image too big. Please maximum ' + (config.uploads.profileUpload.limits.fileSize / (1024 * 1024)).toFixed(2) + ' Mb files.';
break;
case 'LIMIT_UNEXPECTED_FILE':
message = 'Missing `newProfilePicture` field';
break;
default:
message = 'Something went wrong';
}
} else if (err.message && !err.errors) {
message = err.message;
} else {
for (var errName in err.errors) {
if (err.errors[errName].message) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,39 +60,82 @@ exports.changeProfilePicture = function (req, res) {
var user = req.user;
var upload = multer(config.uploads.profileUpload).single('newProfilePicture');
var profileUploadFileFilter = require(path.resolve('./config/lib/multer')).profileUploadFileFilter;
var existingImageUrl;

// Filtering to upload only images
upload.fileFilter = profileUploadFileFilter;

if (user) {
upload(req, res, function (uploadError) {
if (uploadError) {
return res.status(400).send({
message: 'Error occurred while uploading profile picture'
});
} else {
user.profileImageURL = config.uploads.profileUpload.dest + req.file.filename;
existingImageUrl = user.profileImageURL;
uploadImage()
.then(updateUser)
.then(deleteOldImage)
.then(login)
.then(function () {
res.json(user);
})
.catch(function (err) {
res.status(400).send(err);
});
} else {
res.status(400).send({
message: 'User is not signed in'
});
}

function uploadImage () {
return new Promise(function (resolve, reject) {
upload(req, res, function (uploadError) {
if (uploadError) {
reject(errorHandler.getErrorMessage(uploadError));
} else {
resolve();
}
});
});
}

user.save(function (saveError) {
if (saveError) {
return res.status(400).send({
message: errorHandler.getErrorMessage(saveError)
function updateUser () {
return new Promise(function (resolve, reject) {
user.profileImageURL = config.uploads.profileUpload.dest + req.file.filename;
user.save(function (err, theuser) {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}

function deleteOldImage () {
return new Promise(function (resolve, reject) {
if (existingImageUrl !== User.schema.path('profileImageURL').defaultValue) {
fs.unlink(existingImageUrl, function (unlinkError) {
if (unlinkError) {
console.log(unlinkError);
reject({
message: 'Error occurred while deleting old profile picture'
});
} else {
req.login(user, function (err) {
if (err) {
res.status(400).send(err);
} else {
res.json(user);
}
});
resolve();
}
});
} else {
resolve();
}
});
} else {
res.status(400).send({
message: 'User is not signed in'
}

function login () {
return new Promise(function (resolve, reject) {
req.login(user, function (err) {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
};
Expand Down

0 comments on commit 234f0c6

Please sign in to comment.