dmitriy-nz / nestjs-form-data

NestJS middleware for handling multipart/form-data, which is primarily used for uploading files.
https://www.npmjs.com/package/nestjs-form-data
MIT License
117 stars 23 forks source link

Files are still uploaded even if the validation failed #56

Closed 0xmbadr closed 4 months ago

0xmbadr commented 8 months ago

It appears that files are still being uploaded even when the validation process fails. Is there a way to make sure files are deleted after a failed validation?

Thank you.

dmitriy-nz commented 8 months ago

Hi, what storage are you using?

When loading fails or when validation fails or request success, the delete method is called when autoDeleteFile is enable (enabled by default) , which is declared as abstract in StoredFile to delete a file from temporary storage. Thus, the files will be deleted after a failed upload.

Also we have test for check auto delete logic.

If this is not happening in your case, could you please create a minimal repository with the problem reproduced so that I understand what exactly is going wrong.

0xmbadr commented 8 months ago

Thank you for your quick reply.

In my case, I want to save files in a specific directory to be accessed later with their paths saved in my database. I am using FileSystemStoredFile with autoDeleteFile: false. Everything works fine, except for the cases when validation goes wrong, unneeded files are still saved.

Is there a way to improve my approach?

dmitriy-nz commented 4 months ago

@0xmbadr Hi, please check out v1.9.7, there was autoDeleteFile config field separated to two fields: cleanupAfterSuccessHandle and cleanupAfterFailedHandle.
You can use the next configuration for auto delete file when error caused and save when everything is okay:

{
   cleanupAfterSuccessHandle: false,
   cleanupAfterFailedHandle: true // default is true
}

Please reopen issue when your problem wasn't resolved.