node-formidable / formidable

The most used, flexible, fast and streaming parser for multipart form data. Supports uploading to serverless environments, AWS S3, Azure, GCP or the filesystem. Used in production.
MIT License
7k stars 680 forks source link

(v2) How to get file information before completion of the upload? #893

Closed aderchox closed 1 year ago

aderchox commented 1 year ago

Support plan

Context

What problem are you trying to solve?

This is a 'question'. I want to clean up uploads that are aborted or have a size more than the max size specified. It seems that this has been fixed in a PR but it's been on v3 and doesn't work for me on v2, so I decided to do the deletion manually, however, if the upload is aborted, the file variable in the callback becomes an empty object making it impossible to find and delete the incomplete upload file... Is there a way to read file information before the upload is complete? Or if any other solutions to this? I don't want to change my module system just for this.

   form.parse(req, (err, fields, file) => {
       // file is an empty object on abort / maxSizeExceed

Do you have a new or modified API suggestion to solve the problem?

I recommend putting at least the upload's filename in the err object passed to the callback so it can be used for the deletion.

   form.parse(req, (err, fields, file) => {
       // err can have some useful information about the incomplete upload

.

aderchox commented 1 year ago

Oh it seems that there are events I can use. I'll try them and close this if they do what I need.

aderchox commented 1 year ago

Yeah using events works. Closing.