Skip to content

Instantly share code, notes, and snippets.

@andyshora
Last active August 29, 2015 14:06
Show Gist options
  • Save andyshora/ca83307d59c6f5572b6f to your computer and use it in GitHub Desktop.
Save andyshora/ca83307d59c6f5572b6f to your computer and use it in GitHub Desktop.
Sample backend route for angular-image-crop
exports.changeProfilePic = function(req, res) {
var id = req.params.id;
// check user is logged in
if (!req.session.userId){
res.send(401);
return;
}
var requestFields = req.body;
if (!requestFields.datauri) {
res.send(400);
return;
}
var fileBuffer = new Buffer(requestFields.datauri.replace(/^data:image\/\w+;base64,/, ''), 'base64');
var contentLength = fileBuffer.length;
var imageName = '_p_' + req.session.userId + '_' + utilsService.getRandomId(4) + '.png';
if (contentLength > 1000000) {
res.send(400, { errors: ['Sorry, your image must be less than 1Mb.'] });
return;
}
// save to S3 bucket
var params = {
Bucket: config.s3.bucket,
ACL: 'public-read',
Key: imageName,
Body: fileBuffer,
ContentType: 'image/png',
ContentLength: contentLength
};
// s3.putObject...
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment