diff --git a/lib/enableMultipart.js b/lib/enableMultipart.js index 684e0376..fd1d7110 100644 --- a/lib/enableMultipart.js +++ b/lib/enableMultipart.js @@ -20,7 +20,7 @@ module.exports = app => { // only process forms with multipart content-type if (typeof contentType === 'string' && contentType.includes('multipart/form-data')) { // generate form object with formidable - const form = formidable(params) + const form = new formidable.IncomingForm(params) form.parse(req, (err, fields, files) => { if (err) { @@ -38,19 +38,21 @@ module.exports = app => { * Remove tmp files left behind by formidable */ function cleanup () { - for (const key in files) { - const file = files[key] - const filePath = file.filepath - - if (typeof filePath === 'string') { - fs.remove(filePath, err => { - if (err) { - logger.error(`${appName} failed to remove tmp file: ${filePath}\n`, err) - } - }) + for (const fileArray of Object.values(files)) { + for (const file of fileArray) { + const filePath = file.filepath + + if (typeof filePath === 'string') { + fs.remove(filePath, err => { + if (err) { + logger.error(`${appName} failed to remove tmp file: ${filePath}\n`, err) + } + }) + } } } } + next() }) } else { diff --git a/package-lock.json b/package-lock.json index a984b8ff..3f0f84d0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18,7 +18,7 @@ "execa": "7.2.0", "express": "4.18.2", "express-html-validator": "0.2.1", - "formidable": "2.1.2", + "formidable": "3.5.1", "fs-extra": "11.1.1", "helmet": "7.0.0", "html-minifier": "4.0.0", @@ -3202,14 +3202,13 @@ } }, "node_modules/formidable": { - "version": "2.1.2", - "resolved": "https://registry.npmjs.org/formidable/-/formidable-2.1.2.tgz", - "integrity": "sha512-CM3GuJ57US06mlpQ47YcunuUZ9jpm8Vx+P2CGt2j7HpgkKZO/DJYQ0Bobim8G6PFQmK5lOqOOdUXboU+h73A4g==", + "version": "3.5.1", + "resolved": "https://registry.npmjs.org/formidable/-/formidable-3.5.1.tgz", + "integrity": "sha512-WJWKelbRHN41m5dumb0/k8TeAx7Id/y3a+Z7QfhxP/htI9Js5zYaEDtG8uMgG0vM0lOlqnmjE99/kfpOYi/0Og==", "dependencies": { "dezalgo": "^1.0.4", "hexoid": "^1.0.0", - "once": "^1.4.0", - "qs": "^6.11.0" + "once": "^1.4.0" }, "funding": { "url": "https://ko-fi.com/tunnckoCore/commissions" @@ -6881,6 +6880,21 @@ } } }, + "node_modules/superagent/node_modules/formidable": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/formidable/-/formidable-2.1.2.tgz", + "integrity": "sha512-CM3GuJ57US06mlpQ47YcunuUZ9jpm8Vx+P2CGt2j7HpgkKZO/DJYQ0Bobim8G6PFQmK5lOqOOdUXboU+h73A4g==", + "dev": true, + "dependencies": { + "dezalgo": "^1.0.4", + "hexoid": "^1.0.0", + "once": "^1.4.0", + "qs": "^6.11.0" + }, + "funding": { + "url": "https://ko-fi.com/tunnckoCore/commissions" + } + }, "node_modules/superagent/node_modules/mime": { "version": "2.6.0", "resolved": "https://registry.npmjs.org/mime/-/mime-2.6.0.tgz", diff --git a/package.json b/package.json index 64a59c5a..e3bd5fd2 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,7 @@ "execa": "7.2.0", "express": "4.18.2", "express-html-validator": "0.2.1", - "formidable": "2.1.2", + "formidable": "3.5.1", "fs-extra": "11.1.1", "helmet": "7.0.0", "html-minifier": "4.0.0", diff --git a/test/multipart.js b/test/multipart.js index de3b7d96..6b10eae0 100644 --- a/test/multipart.js +++ b/test/multipart.js @@ -63,12 +63,19 @@ describe('multipart/formidable', () => { const files = req.files // move files to 'complete' directory - for (const key in files) { - const file = files[key] - const filePath = file.filepath - - if (typeof filePath === 'string') { - fs.copyFileSync(filePath, path.join(completeDir, file.originalFilename)) + for (const fileArray of Object.values(files)) { + for (const file of fileArray) { + const filePath = file.filepath + const originalFilename = file.originalFilename + const destPath = path.join(completeDir, originalFilename) + + if (typeof filePath === 'string') { + try { + fs.moveSync(filePath, destPath) + } catch (error) { + console.error(`Error moving file: ${error}`) + } + } } }