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.log(err) console.error('Stupid error in S3 upload. 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'); if (!options.s3.image) options.s3.image = {} if (!options.s3.audio) options.s3.audio = {} if (!options.s3.video) options.s3.video = {} /* // TODO: maxBytes property doesn't work - if you upload a large file, // it will just hang until you reload the browser, and then CRASH. // Make sure maxbytes property is there - it can be a number, // or zero/undefined (for no maximum upload size) if (options.s3.maxbytes) { if (! ('maxbytes' in options.s3.image)) options.s3.image.maxbytes = options.s3.maxbytes if (! ('maxbytes' in options.s3.video)) options.s3.video.maxbytes = options.s3.maxbytes if (! ('maxbytes' in options.s3.audio)) options.s3.audio.maxbytes = options.s3.maxbytes } */ if (options.s3.image.preserveFilename) options.s3.image.preserveFilename = preserveFilename if (options.s3.video.preserveFilename) options.s3.video.preserveFilename = preserveFilename if (options.s3.audio.preserveFilename) options.s3.audio.preserveFilename = preserveFilename var express = options.express; var router = express.Router(); router.use(skipper()); // req should have a method `file` on it which is // provided by skipper. Use that to do AWS stuff router.post('/image', function(req, res) { d.run(function () { var skip = 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.image.maxbytes, saveAs: options.s3.image.preserveFilename, headers: { 'x-amz-acl': 'public-read' } }, function (err, uploadedFiles) { if (err) return res.status(500).send(err) res.json(uploadedFiles); }); }); }); router.post('/audio', function(req, res) { d.run(function () { if (! options.s3.allowAudioUploads) { return res.status(500).json({ error: "audio uploading not permitted" }) } req.file('audio').upload({ adapter: skipperS3, key: options.s3.key, secret: options.s3.secret, bucket: options.s3.bucket, dirname: options.s3.dirname, // maxBytes: options.s3.audio.maxbytes, saveAs: options.s3.audio.preserveFilename, headers: { 'x-amz-acl': 'public-read' } }, function (err, uploadedFiles) { if (err) res.status(500).send(err) res.json(uploadedFiles); }); }); }); router.post('/video', function(req, res) { d.run(function () { if (! options.s3.allowVideoUploads) { return res.status(500).json({ error: "video uploading not permitted" }) } req.file('video').upload({ adapter: skipperS3, key: options.s3.key, secret: options.s3.secret, bucket: options.s3.bucket, dirname: options.s3.dirname, // maxBytes: options.s3.video.maxbytes, saveAs: options.s3.video.preserveFilename, headers: { 'x-amz-acl': 'public-read' } }, function (err, uploadedFiles) { if (err) res.status(500).send(err) res.json(uploadedFiles); }); }); }); function preserveFilename (stream, cb){ cb(null, stream.filename) } this._middleware = router; } OKS3.prototype.middleware = function() { return this._middleware; }; module.exports = OKS3