Repository navigation
Conversation
When an upload fails after some files were stored, multer removes those files, but array(), fields() and any() kept a copy of each file in req.files. The copy kept its path (disk) or buffer (memory) after the file was deleted, and err.storageErrors[].file pointed at a different object. single() did not have this problem because req.file is the tracked object. Track the object that req.files holds instead of a copy.
|
I checked For each case, I sent the first file and waited for its storage callback to complete before sending a second file that On base The full suite also passed: 174 passing, 2 pending; lint was clean. |
When an upload fails after some files were already stored (a later file hits
LIMIT_FILE_SIZE,LIMIT_UNEXPECTED_FILE, afileFiltererror and so on), multer removes the stored files before callingnext(err). Withsingle()the error handler then seesreq.filewithout itspath(disk) orbuffer(memory), becausereq.fileis the same object multer passes to_removeFile.With
array(),fields()andany()that is not the case.replacePlaceholdercopies the file's properties onto the placeholder inreq.files, butuploadedFileskeeps the original object. So on error:req.files[i].pathstill points at a file that has just been unlinkedreq.files[i].bufferstill holds the whole file, although_removeFilealready "freed" iterr.storageErrors[i].fileis a different object from the entry inreq.files(the existing test forsingle()assertserr.storageErrors[0].file === req.file)Repro (disk storage):
The fix has
replacePlaceholderreturn the object it stored inreq.file/req.files, and the middleware tracks that object inuploadedFiles. Removal then goes through the same object the error handler sees, so all strategies behave likesingle()already does. The object passed to_removeFilestill carries the info returned by_handleFile, so the existing_removeFiletests are unaffected.Tests:
fields()+LIMIT_UNEXPECTED_FILEleavesreq.files.tiny0[0]withoutpath/filename/destination, and the upload dir emptyarray(name, 1)+ a second file leavesreq.files[0].bufferundefinederr.storageErrors[0].file === req.files.tiny0[0]forfields()All three fail on
mainand pass with the change; the full suite andstandardpass.