-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
93 lines (78 loc) · 2.24 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
81
82
83
84
85
86
87
88
89
90
91
92
93
'use strict';
const hof = require('hof');
const config = require('./config.js');
const busboy = require('busboy');
const bl = require('bl');
const logger = require('hof/lib/logger')({ env: config.env });
let settings = require('./hof.settings');
settings = Object.assign({}, settings, {
behaviours: settings.behaviours.map(require),
routes: settings.routes.map(require)
});
const app = hof(settings);
app.use((req, res, next) => {
res.locals.htmlLang = 'en';
// TODO destructure *form*, *returnUrl* & *mac* to generate the complete URL
const { url } = config.feedback;
res.locals.feedbackUrl = url;
if (req.is('multipart/form-data')) {
try {
const bb = busboy({
headers: req.headers,
limits: {
fileSize: config.upload.maxFileSizeInBytes
}
});
bb.on('field', (key, value) => {
req.body[key] = value;
});
bb.on('file', (key, file, fileInfo) => {
file.pipe(
bl((err, d) => {
if (err) {
logger.log('error', `Error processing file : ${err}`);
return;
}
if (!(d.length || fileInfo.filename)) {
logger.log('warn', 'Empty file received');
return;
}
const fileData = {
data: file.truncated ? null : d,
name: fileInfo.filename || null,
encoding: fileInfo.encoding,
mimetype: fileInfo.mimeType,
truncated: file.truncated,
size: file.truncated ? null : Buffer.byteLength(d, 'binary')
};
if (settings.multi) {
req.files[key] = req.files[key] || [];
req.files[key].push(fileData);
} else {
req.files[key] = fileData;
}
})
);
});
let error;
bb.on('error', function (err) {
error = err;
next(err);
});
bb.on('finish', function () {
if (!error) {
next();
}
});
req.files = req.files || {};
req.body = req.body || {};
req.pipe(bb);
} catch (err) {
logger.log('error', `Error processing file: ${err}`);
next(err);
}
} else {
next();
}
});
module.exports = app;