Skip to content

Commit

Permalink
Fix forbidden error on setAvatar REST endpoint (#7159)
Browse files Browse the repository at this point in the history
  • Loading branch information
sampaiodiego authored and geekgonecrazy committed Jun 3, 2017
1 parent 3c3bb48 commit 28366df
Showing 1 changed file with 26 additions and 24 deletions.
50 changes: 26 additions & 24 deletions packages/rocketchat-api/server/v1/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,32 +197,34 @@ RocketChat.API.v1.addRoute('users.setAvatar', { authRequired: true }, {
return RocketChat.API.v1.unauthorized();
}

if (this.bodyParams.avatarUrl) {
RocketChat.setUserAvatar(user, this.bodyParams.avatarUrl, '', 'url');
} else {
const Busboy = Npm.require('busboy');
const busboy = new Busboy({ headers: this.request.headers });

Meteor.wrapAsync((callback) => {
busboy.on('file', Meteor.bindEnvironment((fieldname, file, filename, encoding, mimetype) => {
if (fieldname !== 'image') {
return callback(new Meteor.Error('invalid-field'));
}

const imageData = [];
file.on('data', Meteor.bindEnvironment((data) => {
imageData.push(data);
}));
Meteor.runAsUser(user._id, () => {
if (this.bodyParams.avatarUrl) {
RocketChat.setUserAvatar(user, this.bodyParams.avatarUrl, '', 'url');
} else {
const Busboy = Npm.require('busboy');
const busboy = new Busboy({ headers: this.request.headers });

Meteor.wrapAsync((callback) => {
busboy.on('file', Meteor.bindEnvironment((fieldname, file, filename, encoding, mimetype) => {
if (fieldname !== 'image') {
return callback(new Meteor.Error('invalid-field'));
}

const imageData = [];
file.on('data', Meteor.bindEnvironment((data) => {
imageData.push(data);
}));

file.on('end', Meteor.bindEnvironment(() => {
RocketChat.setUserAvatar(user, Buffer.concat(imageData), mimetype, 'rest');
callback();
}));

file.on('end', Meteor.bindEnvironment(() => {
RocketChat.setUserAvatar(user, Buffer.concat(imageData), mimetype, 'rest');
callback();
}));

}));
this.request.pipe(busboy);
})();
}
this.request.pipe(busboy);
})();
}
});

return RocketChat.API.v1.success();
}
Expand Down

0 comments on commit 28366df

Please sign in to comment.