-
Notifications
You must be signed in to change notification settings - Fork 38
/
index.js
31 lines (29 loc) · 1.33 KB
/
index.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
function getValueIgnoringKeyCase(object, key) {
const foundKey = Object
.keys(object)
.find(currentKey => currentKey.toLocaleLowerCase() === key.toLowerCase());
return object[foundKey];
}
function getBoundary(event) {
return getValueIgnoringKeyCase(event.headers, 'Content-Type').split('=')[1];
}
module.exports.parse = (event, spotText) => {
const boundary = getBoundary(event);
const result = {};
event.body
.split(boundary)
.forEach(item => {
if (/filename=".+"/g.test(item)) {
result[item.match(/name=".+";/g)[0].slice(6, -2)] = {
type: 'file',
filename: item.match(/filename=".+"/g)[0].slice(10, -1),
contentType: item.match(/Content-Type:\s.+/g)[0].slice(14),
content: spotText? Buffer.from(item.slice(item.search(/Content-Type:\s.+/g) + item.match(/Content-Type:\s.+/g)[0].length + 4, -4), 'binary'):
item.slice(item.search(/Content-Type:\s.+/g) + item.match(/Content-Type:\s.+/g)[0].length + 4, -4),
};
} else if (/name=".+"/g.test(item)){
result[item.match(/name=".+"/g)[0].slice(6, -1)] = item.slice(item.search(/name=".+"/g) + item.match(/name=".+"/g)[0].length + 4, -4);
}
});
return result;
};