Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more metadata for corresponding files #19

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ This nodejs module will parse the multipart-form containing files and fields fro
content: <Buffer 25 50 6f 62 ... >,
contentType: 'application/pdf',
encoding: '7bit',
fieldname: 'uploadFile1'
fieldname: 'uploadFile1',
size: 26000,
checksum: "387d3143b0baa6beb292eda4f81b2d33e55c6744"
}
],
field1: 'VALUE1',
Expand All @@ -39,6 +41,9 @@ Please make sure to enable the "Use Lambda Proxy integration" in API Gateway met

If decided not to enable it for some reason, make sure to pass the required Lambda event parameters in Integration Request -> Mapping Templates section, such as body, headers and isBase64Encoded flag.

The `size` of every file is in **bytes**.
The `checksum` of every file is calculated using **SHA1** hashing algorithm.

Sample Lambda and API Gateway implementation with Cloudformation can be found in [here](http://francismeynard.github.io/aws-upload-document-service).

## Test
Expand Down
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ declare module "lambda-multipart-parser" {
contentType: string
encoding: string
fieldname: string
size: number
checksum: string
}

type MultipartRequest = { files: MultipartFile[] } & Record<string, string>
Expand Down
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const Busboy = require('busboy');
const Crypto = require('crypto');

/*
* This module will parse the multipart-form containing files and fields from the lambda event object.
Expand All @@ -13,7 +14,9 @@ const Busboy = require('busboy');
content: <Buffer 25 50 6f 62 ... >,
contentType: 'application/pdf',
encoding: '7bit',
fieldname: 'uploadFile1'
fieldname: 'uploadFile1',
size: 26000,
checksum: "387d3143b0baa6beb292eda4f81b2d33e55c6744"
}
],
field1: 'VALUE1',
Expand Down Expand Up @@ -43,6 +46,8 @@ const parse = (event) => new Promise((resolve, reject) => {
uploadFile.contentType = mimetype;
uploadFile.encoding = encoding;
uploadFile.fieldname = fieldname;
uploadFile.size = Buffer.byteLength(uploadFile.content);
uploadFile.checksum = Crypto.createHash('sha1').update(uploadFile.content, "utf-8").digest('hex');
result.files.push(uploadFile);
}
});
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"multipart/formdata"
],
"dependencies": {
"busboy": "^0.3.0"
"busboy": "^0.3.0",
"crypto": "^1.0.1"
},
"devDependencies": {
"chai": "^3.5.0",
Expand Down
4 changes: 4 additions & 0 deletions test/MultipartParserTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ describe('MultipartParser', () => {
assert.equal(file.contentType, "text/plain");
assert.equal(file.encoding, "7bit");
assert.equal(file.fieldname, "uploadFile1");
assert.equal(file.size, 12);
assert.equal(file.checksum, "2ef7bde608ce5404e97d5f042f95f89f1c232871");
});

it('should parse the multipart form-data successfully given base64 encoded form data', async () => {
Expand Down Expand Up @@ -72,6 +74,8 @@ describe('MultipartParser', () => {
assert.equal(file.contentType, "text/plain");
assert.equal(file.encoding, "7bit");
assert.equal(file.fieldname, "uploadFile1");
assert.equal(file.size, 12);
assert.equal(file.checksum, "2ef7bde608ce5404e97d5f042f95f89f1c232871");
});

it('should parse the multipart form-data successfully given utf8 encoded form data', async () => {
Expand Down