Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gridfs-stream always stores files with "contentType: binary/octet-stream"

Tags:

I am trying to code a program in nodejs that stores a list of files in mongodb. It works OK, but there is a problem: it always stores the contentType metadata as binary/octet-stream, and I would like it to store the actual mime-type. I have tried getting the mime type before the readStream (via promises), but even if I hardcode the contentType (for example "image/jpeg"), it always saves the metadata as "binary/octet-stream.

It is my code:

files.forEach(function(f) {

    var conn = mongoose.createConnection(db);

    conn.once('open', function () {
      var gfs = Grid(conn.db);
      var writeStream = gfs.createWriteStream({
        filename: f.location,
        mode: 'w',
        contentType: 'image/jpeg'
      });

      writeStream.on('finish', function() {
          console.log('Storing', f.location, 'OK');
          return;
      })
      .on('error', function(data) {
          console.log('Error', f.location, data) 
      });

      fs.createReadStream(path.join(__dirname, 'files', f.location), 'utf-8')
      .pipe(writeStream);

    });
});

Any idea? Thank you for your responses!

like image 909
Andrés Bayón Avatar asked Mar 20 '16 09:03

Andrés Bayón


1 Answers

In order to set the content type you need to do this:

var writeStream = gfs.createWriteStream({
  filename: f.location,
  mode: 'w',
  content_type: 'image/jpeg'
});
like image 130
nigava Avatar answered Nov 15 '22 06:11

nigava