reactjsmongodbmongoosegridfs

How to get information about the file


I am able to open and stream the file no issue by using the following, however I need to be able to use the file information that is stored inside the bucket.

 const db = connection.connections[0].db
          const bucket = new mongoose.mongo.GridFSBucket(db, {
            bucketName: bucketName
          });
          bucket.openDownloadStreamByName(filename).pipe(res)
          

For example I would like to be able to set the following

res.setHeader('Content-Type', (TYPE)),
res.setHeader('Content-Length', (LENGTH)),

I am wondering the following above allows options however I don't know if the pipe stops us from setting the content-type and length after it starts piping.


Solution

  • According to docs, no you can't get file info from stream but in source code seems you can.

    According to this and this, you could get contentType by accessing

    bucket.openDownloadStreamByName(...).s.files[0].contentType
    

    or

    bucket.openDownloadStreamByName(...).s.file?.contentType