mirror of
https://github.com/taobataoma/meanTorrent.git
synced 2026-03-02 02:00:58 +01:00
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:
@@ -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'
|
||||
});
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user