Fix: Use "writeStream.id" in "finish" Event to Ensure Proper File Metadata for Multer-GridFS-Storage #588
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
The
multer-gridfs-storagepackage currently uses thefinishevent onwriteStreamto emit metadata for uploaded files. However, the metadata emitted relies on thefileobject returned by GridFS after the upload completes. This approach fails for certain scenarios, particularly in newer versions of MongoDB or specific configurations wherewriteStreamfinishes before complete metadata (such asmd5orsize) is available.This bug causes issues like:
Steps to Reproduce the Issue
multer-gridfs-storagewith a MongoDB instance.Observed Behavior:
md5,sizeandchunkSize, are null or incorrect.Expected Behavior:
finishevent should always contain complete and accurate information about the uploaded file.Solution
To address this, I modified the
lib/gridfs.jsfile to use thewriteStream.idas the unique identifier for uploaded files. The solution ensures that metadata is constructed directly from thewriteStreamoptions and does not depend on the incompletefileobject.Changes include:
emitFilelogic to usewriteStream.id.streamOptionsandwriteStream.fileevent with accurate metadata upon upload completion.Updated
emitFilefunction:Testing
The fix was tested in the following scenarios:
id,filename,metadata, and other properties matched the database entries.All tests confirmed the fix resolves the issue without introducing new bugs.
Why This Fix is Important
This fix is critical for developers relying on multer-gridfs-storage to handle file uploads in production environments. Without this fix, the package produces incomplete metadata, leading to unreliable file management and potential application failures.
By resolving this issue, this PR ensures:
Additional Note:
Since the official repository has been inactive for some time and this issue remains unresolved upstream, you may consider using my fork which includes this fix. To use my forked version in your project, simply update your package.json dependency as follows:
This fork is maintained with the fix for the file metadata issue, ensuring compatibility with current Node.js and MongoDB versions. Contributions and feedback are welcome.