Created
April 24, 2016 20:04
-
-
Save thedewpoint/ff143e0fc780b251b0281a866b008bdc to your computer and use it in GitHub Desktop.
Image manipulation and upload to S3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by daniel on 9/29/2015. | |
*/ | |
var AWS = require('aws-sdk'); | |
AWS.config.update({accessKeyId: 'ID', secretAccessKey: 'KEY'}); | |
AWS.config.update({region: 'us-east-1'}); | |
var s3 = new AWS.S3(); | |
var fileType = require('file-type'); | |
var lwip = require('lwip'); | |
var uuid = require('node-uuid'); | |
var q = require('q'); | |
var request = require('request'); | |
var userService = require('../services/user_service'); | |
var STATIC_BUCKET = "BUCKET_NAME"; | |
request = q.denodeify(request); | |
var validator = require('validator'); | |
var imageSizes = { | |
large:{ | |
width:600, | |
height:600 | |
}, | |
medium: { | |
width:250, | |
height:200 | |
}, | |
small: { | |
width:100, | |
height:100 | |
} | |
}; | |
//maintain aspect ratio | |
function getDimensions(originalWidth,originalHeight,newWidth,newHeight){ | |
var dimensions = {}; | |
dimensions.width = originalWidth; | |
dimensions.height = originalHeight; | |
if(originalWidth > newWidth){ | |
dimensions.width = newWidth; | |
dimensions.height = (dimensions.width * originalHeight)/originalWidth; | |
} | |
if(dimensions.height > newHeight){ | |
dimensions.height = newHeight; | |
dimensions.width = (dimensions.height * originalWidth) / originalHeight; | |
} | |
return dimensions; | |
} | |
function resizeAndUpload (img){ | |
console.log(img); | |
var deferred = q.defer(); | |
var getBuffer; | |
if(validator.isBase64(img)){ | |
console.log("base!"); | |
var buffer = new Buffer(img, 'base64'); | |
getBuffer = q(buffer); | |
} | |
if(validator.isURL(img)){ | |
console.log("fb!"); | |
getBuffer = request({uri:img,encoding:'binary'}).then(function(args){ | |
console.log("request done!"); | |
return new Buffer(args[1].toString(),'binary'); | |
}); | |
} | |
var id = uuid.v1(); | |
var originalWidth, originalHeight,newDimensions; | |
try { | |
getBuffer.then(function(body){ | |
console.log("get buffer"); | |
lwip.open(body,fileType(body).ext, function (err, img) { | |
console.log("resize 1"); | |
originalWidth = img.width(); | |
originalHeight = img.height(); | |
newDimensions = getDimensions(originalWidth,originalHeight,imageSizes.large.width,imageSizes.large.height); | |
img.resize(newDimensions.width,newDimensions.height,function(err, img){ | |
console.log("resize 1 done"); | |
uploadImage(img,id+"/large").then(function(){ | |
console.log("upload 1"); | |
originalWidth = img.width(); | |
originalHeight = img.height(); | |
newDimensions = getDimensions(originalWidth,originalHeight,imageSizes.medium.width,imageSizes.medium.height); | |
img.resize(newDimensions.width,newDimensions.height,function(err, img){ | |
console.log("resize 2 done"); | |
uploadImage(img,id+"/medium").then(function(){ | |
console.log("upload 2"); | |
originalWidth = img.width(); | |
originalHeight = img.height(); | |
newDimensions = getDimensions(originalWidth,originalHeight,imageSizes.small.width,imageSizes.small.height); | |
img.resize(newDimensions.width,newDimensions.height,function(err, img){ | |
console.log("resize 3 done"); | |
uploadImage(img,id+"/small").then(function(){ | |
console.log("resolving"); | |
deferred.resolve({photoid:id, new:false}); | |
}); | |
}); | |
}); | |
}); | |
}); | |
}); | |
}); | |
},function(error){ | |
console.log("some request error"); | |
console.log(error); | |
}).catch(function (error) { | |
console.log("some request error"); | |
console.log(error); | |
}); | |
}catch(e){ | |
console.log("error"); | |
} | |
return deferred.promise; | |
}; | |
function uploadImage(img,path){ | |
console.log("test"); | |
var deferred = q.defer(); | |
img.toBuffer('jpg',function(err,buffer){ | |
var params = { | |
Bucket: STATIC_BUCKET, | |
Key: path, | |
Body: buffer, | |
ACL:'public-read', | |
ContentType: fileType(buffer).mime | |
}; | |
s3.putObject(params, function (err,data) { | |
if (err) { throw err; } | |
console.log(data); | |
console.log(path); | |
deferred.resolve(); | |
}); | |
}); | |
return deferred.promise; | |
}; | |
process.on('message',function(data){ | |
var promises = []; | |
data.images.forEach(function(img){ | |
var promise = img.new && img.data? resizeAndUpload(img.data) : q(img); | |
promises.push(promise); | |
}); | |
q.all(promises) | |
.then(function(photos){ | |
console.log("then!"); | |
var deleteArr = []; | |
if (data.user.profile.photos){ | |
data.user.profile.photos.forEach(function(oldPhoto){ | |
var found = false; | |
photos.forEach(function(newPhoto){ | |
if(newPhoto.photoid === oldPhoto.photoid){ | |
found = true; | |
} | |
}); | |
if(!found){ | |
deleteArr.push({Key:oldPhoto.photoid}); | |
} | |
}); | |
} | |
data.user.profile.photos = photos; | |
if(deleteArr.length){ | |
console.log(deleteArr); | |
s3.deleteObjects({Bucket:STATIC_BUCKET,Delete:{Objects:deleteArr}},function(err,data){ | |
console.log(err); | |
console.log(data); | |
}); | |
} | |
console.log("trying to update"); | |
userService.updateUser(data.user).then(function(){ | |
console.log("updated"); | |
process.send(data.user); | |
}); | |
}); | |
}); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fork = require('child_process').fork; | |
var path = require('path'); | |
var imgTask = fork(path.join(__dirname,'../tasks/resize_and_upload_task.js')); | |
var globalSocket = require('../objects/global-socket'); | |
exports.resizeAndUpload = function (images,user){ | |
var data = {images:images,user:user}; | |
imgTask.send(data); | |
imgTask.on("message",function(user){ | |
globalSocket.get().sockets.in(user.username).emit('img_upload_complete', user.profile.photos); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for sharing. That's a really good starting point.