Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Francis Pasoquen committed Feb 4, 2019
0 parents commit 9ef028f
Show file tree
Hide file tree
Showing 7 changed files with 576 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
**/node_modules/**
**/target/**
.project
.jsbeautifyrc
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Francis Pasoquen

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
26 changes: 26 additions & 0 deletions README.md
@@ -0,0 +1,26 @@
# lambda-multipart-parser

## Introduction
This module will parse the multipart-form containing files and fields from the lambda event object.

## Description
```
@param {event} - an event containing the multipart-form in the body
@return {object} - a JSON object containing array of files and fields, sample below.
{
files: [
{
filename: 'test.pdf',
content: <Buffer 25 50 6f 62 ... >,
contentType: 'application/pdf',
encoding: '7bit',
fieldname: 'uploadFile1'
}
],
field1: 'VALUE1',
field2: 'VALUE2',
}
```

## Usage
67 changes: 67 additions & 0 deletions index.js
@@ -0,0 +1,67 @@
'use strict';

const Busboy = require('busboy');

/*
* This module will parse the multipart-form containing files and fields from the lambda event object.
* @param {event} - an event containing the multipart-form in the body
* @return {object} - a JSON object containing array of files and fields, sample below.
{
files: [
{
filename: 'test.pdf',
content: <Buffer 25 50 6f 62 ... >,
contentType: 'application/pdf',
encoding: '7bit',
fieldname: 'uploadFile1'
}
],
field1: 'VALUE1',
field2: 'VALUE2',
}
*/
const parse = (event) => new Promise((resolve, reject) => {
const busboy = new Busboy({
headers: {
'content-type': event.headers['content-type'] || event.headers['Content-Type']
}
});
const result = {
files: []
};

busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {
const uploadFile = {};

file.on('data', data => {
uploadFile.content = data;
});

file.on('end', () => {
if (uploadFile.content) {
uploadFile.filename = filename;
uploadFile.contentType = mimetype;
uploadFile.encoding = encoding;
uploadFile.fieldname = fieldname;
result.files.push(uploadFile);
}
});
});

busboy.on('field', (fieldname, value) => {
result[fieldname] = value;
});

busboy.on('error', error => {
reject(error);
});

busboy.on('finish', () => {
resolve(result);
});

busboy.write(event.body, event.isBase64Encoded ? 'base64' : 'binary');
busboy.end();
});

module.exports.parse = parse;

0 comments on commit 9ef028f

Please sign in to comment.