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'
});

View File

@@ -6,6 +6,7 @@ var semver = require('semver'),
path = require('path'),
mongoose = require('mongoose'),
User = mongoose.model('User'),
config = require(path.resolve('./config/config')),
express = require(path.resolve('./config/lib/express'));
/**
@@ -1078,6 +1079,39 @@ describe('User CRUD tests', function () {
});
});
it('should be able to change profile picture and not fail if existing picture file does not exist', function (done) {
user.profileImageURL = config.uploads.profile.image.dest + 'non-existing.png';
user.save(function(saveErr) {
// Handle error
if (saveErr) {
return done(saveErr);
}
agent.post('/api/auth/signin')
.send(credentials)
.expect(200)
.end(function (signinErr) {
// Handle signin error
if (signinErr) {
return done(signinErr);
}
agent.post('/api/users/picture')
.attach('newProfilePicture', './modules/users/client/img/profile/default.png')
.expect(200)
.end(function (userInfoErr) {
should.not.exist(userInfoErr);
return done();
});
});
});
});
afterEach(function (done) {
User.remove().exec(done);
});