Created
          September 6, 2018 13:13 
        
      - 
      
- 
        Save fulopdaniel/64005c35ea28b90fc5c481abddd75790 to your computer and use it in GitHub Desktop. 
  
    
      This file contains hidden or 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
    
  
  
    
  | const getContentType = (event) => { | |
| const contentType = event.headers['content-type'] | |
| if (!contentType) { | |
| return event.headers['Content-Type']; | |
| } | |
| return contentType; | |
| }; | |
| const parser = (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(error)); | |
| busboy.on('finish', () => { | |
| event.body = result; | |
| resolve(event); | |
| }); | |
| busboy.write(event.body, event.isBase64Encoded ? 'base64' : 'binary'); | |
| busboy.end(); | |
| }); | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment