1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
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. 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());
// 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 () {
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);
});
});
});
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.maxbytesAudio,
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.maxbytesVideo,
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
|