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

Upgraded formidable version to 3.5.1 #1317

Merged
merged 3 commits into from
Nov 10, 2023
Merged
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
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": "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",
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
Loading