Last active
October 6, 2016 15:16
-
-
Save DorkNstein/80dcbcb7d996d83548b1943d0bb8e4e3 to your computer and use it in GitHub Desktop.
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
// Busboy upload file to s3 | |
exports.create = function(req, res, next) { | |
var busboy = new Busboy({ headers: req.headers }); | |
busboy.on("file", function(fieldname, file, filename, encoding, mimetype) { | |
var filename = uuid.v4() + '.' + filename.split('.')[1]; | |
var s3obj = new AWS.S3({ params: { Bucket: config.aws.s3.bucket, Key: filename } }); | |
s3obj.upload({ Body: file }) | |
.send(function(err, data) { | |
if (err) { | |
res.send(500, err); | |
return next(); | |
} else { | |
console.log(data); | |
res.json(data); | |
next(); | |
} | |
}); | |
}); | |
req.pipe(busboy); | |
}; | |
// s3 read Image fromurl | |
exports.retrieveImage = function(imageName, res) { | |
console.log('retrieving image with key: ' + imageName); | |
var params = { | |
'Bucket': config.aws.s3.bucket, | |
'Key': imageName | |
}; | |
s3Bucket.getObject(params).createReadStream().pipe(res); | |
}; | |
// s3 download single image | |
exports.getImage = function(req, res) { | |
var params = { | |
'Bucket': config.aws.s3.bucket, | |
'Key': imageName | |
}; | |
s3Bucket.getObject(params, function(err, data){ | |
console.log(err, data.Body); | |
res.send(200, data.Body); | |
}) | |
}; | |
// s3 download group of images as a zip | |
// URL: API + '/v/1/file/getimage/' + folder + '?primary1=' +image1+ '&'+'primary2=' +image2+ '&'+'primary3=' +image3+ '&'+'primary4=' +image4; | |
exports.getImage = function(req, res) { | |
var folder = req.params.id +'/'; | |
var image1 = req.query.primary1; | |
var image2 = req.query.primary2; | |
var image3 = req.query.primary3; | |
var image4 = req.query.primary4; | |
res.setHeader('Content-type', 'application/octet-stream'); | |
res.setHeader("Content-Description", "File Transfer"); | |
res.setHeader("Content-Disposition", "attachment; filename='file.zip'"); | |
res.setHeader("Content-Transfer-Encoding", "binary"); | |
s3Zip | |
.archive({ region: config.aws.region, bucket: config.aws.s3.bucket }, folder, [image1, image2, image3, image4]) | |
.pipe(res); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment