-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathserver.js
81 lines (68 loc) · 2.54 KB
/
server.js
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
'use strict';
var async = require('async');
var express = require('express');
var fs = require('fs');
var multiparty = require('multiparty');
var util = require('util');
/* Make an http server to receive the webhook. */
var server = express();
server.head('/webhook', function (req, res) {
console.log('Received head request from webhook.');
res.send(200);
});
server.post('/webhook', function (req, res) {
console.log('Receiving webhook.');
/* Respond early to avoid timouting the mailin server. */
// res.send(200);
/* Parse the multipart form. The attachments are parsed into fields and can
* be huge, so set the maxFieldsSize accordingly. */
var form = new multiparty.Form({
maxFieldsSize: 70000000
});
form.on('progress', function () {
var start = Date.now();
var lastDisplayedPercentage = -1;
return function (bytesReceived, bytesExpected) {
var elapsed = Date.now() - start;
var percentage = Math.floor(bytesReceived / bytesExpected * 100);
if (percentage % 20 === 0 && percentage !== lastDisplayedPercentage) {
lastDisplayedPercentage = percentage;
console.log('Form upload progress ' +
percentage + '% of ' + bytesExpected / 1000000 + 'Mb. ' + elapsed + 'ms');
}
};
}());
form.parse(req, function (err, fields) {
console.log(util.inspect(fields.mailinMsg, {
depth: 5
}));
console.log('Parsed fields: ' + Object.keys(fields));
/* Write down the payload for ulterior inspection. */
async.auto({
writeParsedMessage: function (cbAuto) {
fs.writeFile('payload.json', fields.mailinMsg, cbAuto);
},
writeAttachments: function (cbAuto) {
var msg = JSON.parse(fields.mailinMsg);
async.eachLimit(msg.attachments, 3, function (attachment, cbEach) {
fs.writeFile(attachment.generatedFileName, fields[attachment.generatedFileName], 'base64', cbEach);
}, cbAuto);
}
}, function (err) {
if (err) {
console.log(err.stack);
res.send(500, 'Unable to write payload');
} else {
console.log('Webhook payload written.');
res.send(200);
}
});
});
});
server.listen(3000, function (err) {
if (err) {
console.log(err);
} else {
console.log('Http server listening on port 3000');
}
});