diff --git a/README.md b/README.md index dc76849..ed87549 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,17 @@ [GridFS](https://docs.mongodb.com/manual/core/gridfs) storage engine for [Multer](https://github.com/expressjs/multer) to store uploaded files directly to MongoDb. +## Fork Notice +This is a fork of [multer-gridfs-storage](https://github.com/devconcept/multer-gridfs-storage) with a fix for the finish event issue where file metadata is not returned correctly. I modified the code to use `writeStream.id` so that the uploaded file's metadata is accurately emitted. + +Since the original repository is inactive, you can use this fork in your project. Simply update your dependency in your `package.json`: +```sh +"dependencies": { + "multer-gridfs-storage": "git+https://github.com/amar-codingenthusiast/multer-gridfs-storage.git#master" +} +``` +Feel free to use, modify, and contribute further! + ## 🔥 Features - Compatibility with MongoDb versions 2 and 3. diff --git a/lib/gridfs.js b/lib/gridfs.js index c8419ff..5d47afa 100644 --- a/lib/gridfs.js +++ b/lib/gridfs.js @@ -302,21 +302,37 @@ class GridFsStorage extends node_events_1.EventEmitter { this.emit('streamError', streamError, streamOptions); reject(streamError); }; - const emitFile = (f) => { + // const emitFile = (f) => { + // const storedFile = { + // id: f._id, + // filename: f.filename, + // metadata: f.metadata || null, + // bucketName: streamOptions.bucketName, + // chunkSize: f.chunkSize, + // size: f.length, + // md5: f.md5, + // uploadDate: f.uploadDate, + // contentType: f.contentType, + // }; + // this.emit('file', storedFile); + // resolve(storedFile); + // }; + const emitFile = () => { + const fileId = writeStream.id; const storedFile = { - id: f._id, - filename: f.filename, - metadata: f.metadata || null, + id: fileId, + filename: streamOptions.filename, + metadata: streamOptions.metadata || null, bucketName: streamOptions.bucketName, - chunkSize: f.chunkSize, - size: f.length, - md5: f.md5, - uploadDate: f.uploadDate, - contentType: f.contentType, + chunkSize: null, + size: null, + md5: null, + uploadDate: new Date(), + contentType: streamOptions.contentType || null, }; this.emit('file', storedFile); resolve(storedFile); - }; + }; const writeStream = this.createStream(streamOptions); // Multer already handles the error event on the readable stream(Busboy). // Invoking the callback with an error will cause file removal and aborting routines to be called twice