diff --git a/README.md b/README.md index 979feb4..3d3db51 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,9 @@ This nodejs module will parse the multipart-form containing files and fields fro content: , contentType: 'application/pdf', encoding: '7bit', - fieldname: 'uploadFile1' + fieldname: 'uploadFile1', + size: 26000, + checksum: "387d3143b0baa6beb292eda4f81b2d33e55c6744" } ], field1: 'VALUE1', @@ -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 diff --git a/index.d.ts b/index.d.ts index 8d78cc2..368c514 100644 --- a/index.d.ts +++ b/index.d.ts @@ -9,6 +9,8 @@ declare module "lambda-multipart-parser" { contentType: string encoding: string fieldname: string + size: number + checksum: string } type MultipartRequest = { files: MultipartFile[] } & Record diff --git a/index.js b/index.js index 3189ad7..cad3260 100644 --- a/index.js +++ b/index.js @@ -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. @@ -13,7 +14,9 @@ const Busboy = require('busboy'); content: , contentType: 'application/pdf', encoding: '7bit', - fieldname: 'uploadFile1' + fieldname: 'uploadFile1', + size: 26000, + checksum: "387d3143b0baa6beb292eda4f81b2d33e55c6744" } ], field1: 'VALUE1', @@ -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); } }); diff --git a/package.json b/package.json index c0dd70f..70c648e 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,8 @@ "multipart/formdata" ], "dependencies": { - "busboy": "^0.3.0" + "busboy": "^0.3.0", + "crypto": "^1.0.1" }, "devDependencies": { "chai": "^3.5.0", diff --git a/test/MultipartParserTest.js b/test/MultipartParserTest.js index a2e2614..62e7f53 100644 --- a/test/MultipartParserTest.js +++ b/test/MultipartParserTest.js @@ -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 () => { @@ -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 () => {