fix(users): don't fail on missing old image on image upload (#1839)

Fixes scenarios where previously when old image file would be missing, uploading new image file over it would fail because unlinking previous file fails.
This commit is contained in:
Mikael Korpela
2017-08-13 21:52:38 +03:00
committed by GitHub
parent 1e3eeb7e3b
commit be88a2ca1f
2 changed files with 43 additions and 1 deletions

View File

@@ -111,7 +111,15 @@ exports.changeProfilePicture = function (req, res) {
if (existingImageUrl !== User.schema.path('profileImageURL').defaultValue) {
fs.unlink(existingImageUrl, function (unlinkError) {
if (unlinkError) {
console.log(unlinkError);
// If file didn't exist, no need to reject promise
if (unlinkError.code === 'ENOENT') {
console.log('Removing profile image failed because file did not exist.');
return resolve();
}
console.error(unlinkError);
reject({
message: 'Error occurred while deleting old profile picture'
});