From b861a3995e2a7fbc4c973557f53459d299227148 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=AA=20Quang=20V=C5=A9?= Date: Wed, 2 Sep 2020 15:17:58 +0700 Subject: [PATCH 1/3] Added 'hashAlgorithm' options to use another algorithm other than md5 --- lib/index.js | 1 + lib/memHandler.js | 2 +- lib/tempFileHandler.js | 6 +++--- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/index.js b/lib/index.js index 41437a6..50d90ae 100644 --- a/lib/index.js +++ b/lib/index.js @@ -18,6 +18,7 @@ const DEFAULT_OPTIONS = { createParentPath: false, parseNested: false, useTempFiles: false, + hashAlgorithm: 'md5', tempFileDir: path.join(process.cwd(), 'tmp') }; diff --git a/lib/memHandler.js b/lib/memHandler.js index 09accfe..a138484 100644 --- a/lib/memHandler.js +++ b/lib/memHandler.js @@ -10,7 +10,7 @@ const { debugLog } = require('./utilities'); */ module.exports = (options, fieldname, filename) => { const buffers = []; - const hash = crypto.createHash('md5'); + const hash = crypto.createHash(options.hashAlgorithm); let fileSize = 0; let completed = false; diff --git a/lib/tempFileHandler.js b/lib/tempFileHandler.js index bfaca8b..6751935 100644 --- a/lib/tempFileHandler.js +++ b/lib/tempFileHandler.js @@ -14,8 +14,8 @@ module.exports = (options, fieldname, filename) => { checkAndMakeDir({ createParentPath: true }, tempFilePath); debugLog(options, `Temporary file path is ${tempFilePath}`); - - const hash = crypto.createHash('md5'); + + const hash = crypto.createHash(options.hashAlgorithm); let fileSize = 0; let completed = false; @@ -54,7 +54,7 @@ module.exports = (options, fieldname, filename) => { completed = true; debugLog(options, `Cleaning up temporary file ${tempFilePath}...`); writeStream.end(); - deleteFile(tempFilePath, err => (err + deleteFile(tempFilePath, err => (err ? debugLog(options, `Cleaning up temporary file ${tempFilePath} failed: ${err}`) : debugLog(options, `Cleaning up temporary file ${tempFilePath} done.`) )); From 9f4e197dd5c6b3771d98fef0e1e10f1fd7826881 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=AA=20Quang=20V=C5=A9?= Date: Wed, 2 Sep 2020 15:36:43 +0700 Subject: [PATCH 2/3] hashAlgorithm options: Expose hash using the algorithm name --- lib/fileFactory.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/fileFactory.js b/lib/fileFactory.js index 02b82df..19eb967 100644 --- a/lib/fileFactory.js +++ b/lib/fileFactory.js @@ -40,7 +40,7 @@ module.exports = (options, fileUploadOptions = {}) => { // firefox uploads empty file in case of cache miss when f5ing page. // resulting in unexpected behavior. if there is no file data, the file is invalid. // if (!fileUploadOptions.useTempFiles && !options.buffer.length) return; - + // Create and return file object. return { name: options.name, @@ -50,7 +50,7 @@ module.exports = (options, fileUploadOptions = {}) => { tempFilePath: options.tempFilePath, truncated: options.truncated, mimetype: options.mimetype, - md5: options.hash, + [fileUploadOptions.hashAlgorithm]: options.hash, mv: (filePath, callback) => { // Define a propper move function. const moveFunc = fileUploadOptions.useTempFiles From 91fdaed874d2cf78e5f1708d4d1ff26085713dda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=AA=20Quang=20V=C5=A9?= Date: Wed, 2 Sep 2020 15:43:11 +0700 Subject: [PATCH 3/3] hashAlgorithm options: use fallback 'md5' name to pass tests --- lib/fileFactory.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/fileFactory.js b/lib/fileFactory.js index 19eb967..a44f14f 100644 --- a/lib/fileFactory.js +++ b/lib/fileFactory.js @@ -50,7 +50,7 @@ module.exports = (options, fileUploadOptions = {}) => { tempFilePath: options.tempFilePath, truncated: options.truncated, mimetype: options.mimetype, - [fileUploadOptions.hashAlgorithm]: options.hash, + [fileUploadOptions.hashAlgorithm || 'md5']: options.hash, mv: (filePath, callback) => { // Define a propper move function. const moveFunc = fileUploadOptions.useTempFiles