Created
January 17, 2019 02:51
-
-
Save lastday154/8c0b39560e22862ccd56cc3f10ba569a to your computer and use it in GitHub Desktop.
paser csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const Busboy = require('busboy'); | |
const getContentType = (event) => { | |
const contentType = event.headers['content-type']; | |
if (!contentType) { | |
return event.headers['Content-Type']; | |
} | |
return contentType; | |
}; | |
const parse = (event) => new Promise((resolve, reject) => { | |
const busboy = new Busboy({ | |
headers: { | |
'content-type': getContentType(event), | |
} | |
}); | |
const result = {}; | |
busboy.on('file', (fieldname, file, filename, encoding, mimetype) => { | |
file.on('data', (data) => { | |
result.file = data; | |
}); | |
file.on('end', () => { | |
result.filename = filename; | |
result.contentType = mimetype; | |
}); | |
}); | |
busboy.on('field', (fieldname, value) => { | |
result[fieldname] = value; | |
}); | |
busboy.on('error', (error) => reject(`Parse error: ${error}`)); | |
busboy.on('finish', () => { | |
event.body = result; | |
resolve(event); | |
}); | |
busboy.write(event.body, event.isBase64Encoded ? 'base64' : 'binary'); | |
busboy.end(); | |
}); | |
module.exports = { | |
parse | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment