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

only add requestFile to tmpUploads if it is not rejected by busboy #171

Merged
merged 7 commits into from
Oct 21, 2020
Merged
Changes from 3 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
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,14 +454,15 @@ function fastifyMultipart (fastify, options = {}, done) {
const target = createWriteStream(filepath)
try {
await pump(file.file, target)
this.tmpUploads.push(filepath)
requestFiles.push({ ...file, filepath })
// busboy set truncated to true when the configured file size limit was reached
if (file.file.truncated) {
const err = new RequestFileTooLargeError()
err.part = file
throw err
}
// Delay adding the filepath to tmpUploads until the file has been checked for size
this.tmpUploads.push(filepath)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to add it there to clean up the files after the response.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also opened an issue to explain further why this change is necessary, see #172

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In short: this adds the file to the list to be cleaned up when the response is closed, but by then it is already deleted.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. We remove the file afterward in the catch handler. I propose to leave it as it is but remove the additional unlink operation in the catch case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'm thinking this should be better

} catch (err) {
try {
await unlink(filepath)
Expand Down