Skip to content

Commit

Permalink
Upgraded formidable version to 3.5.1 (#1317)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnishRBabu authored Nov 10, 2023
1 parent aa4606e commit 72fb895
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 24 deletions.
24 changes: 13 additions & 11 deletions lib/enableMultipart.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down
26 changes: 20 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"execa": "8.0.1",
"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",
Expand Down
19 changes: 13 additions & 6 deletions test/multipart.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
}
}
}
}

Expand Down

0 comments on commit 72fb895

Please sign in to comment.