var skipper = require('skipper'); var skipperS3 = require('skipper-s3') // Hack to prevent this god-forsaken module from crashing our shit var d = require('domain').create() d.on('error', function (err) { console.error('Stupid error in S3 upload. Image upload probably prematurely canceled') }) function OKS3(options) { if (!(this instanceof OKS3)) return new OKS3(options); options = options || {}; if (!options.express) throw new Error('Express not provided to OKS3'); if (!options.s3) throw new Error('S3 configuration not provided to OKS3'); var express = options.express; var router = express.Router(); router.use(skipper()); router.post('/', function(req, res) { // req should have a method `file` on it which is // provided by skipper. Use that to do AWS stuff d.run(function () { req.file('image').upload({ adapter: skipperS3, key: options.s3.key, secret: options.s3.secret, bucket: options.s3.bucket, dirname: options.s3.dirname, maxBytes: options.s3.maxbytes, headers: { 'x-amz-acl': 'public-read' } }, function (err, uploadedFiles) { if (err) res.status(500).send(err) res.json(uploadedFiles); }); }) }); this._middleware = router; } OKS3.prototype.middleware = function() { return this._middleware; }; module.exports = OKS3