question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

gridfs-stream compatability

See original GitHub issue

I am trying to use your library to stream multipart file uploads directly into gridfs using gridfs-stream. I’m having an issue with the encoding of the original file changing. If I pipe the busboy file stream into an fs writestream, then read the file back out with an fs readstream and pipe it to gridfs, all is well. but if I skip the fs streams and go directly to gridfs, the encoding gets messed up.

I have an issue open with gridfs-stream, but I wanted to check if there’s anything I could do from this end.

https://github.com/aheckmann/gridfs-stream/issues/76

Edit:

Here’s how I want to do it, followed by what is actually working for me.

Results in bad/incorrect encoding when read back out:

exports.create = function(req, res) {
    var busboy = new Busboy({ headers: req.headers });
    var fileId = new mongoose.Types.ObjectId();

    busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
        var writeStream = gfs.createWriteStream({
            _id: fileId,
            filename: filename,
            mode: 'w',
            content_type: mimetype,
            metadata: {
                uploadedBy: req.user._id,
                encoding: encoding,
            }
        });

        file.pipe(writeStream);
    });

    busboy.on('finish', function() {
        return res.status(200).send({
            message: fileId.toString()
        });
    });

    req.pipe(busboy);
};

works:

exports.create = function(req, res) {
    var busboy = new Busboy({ headers: req.headers });
    var fileId = new mongoose.Types.ObjectId();

    busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
        var tmpPath = '/tmp/' + filename;
        var fsWriteStream = fs.createWriteStream(tmpPath);

        busboy.on('finish', function() {

            var fsReadStream = fs.createReadStream(tmpPath);

            var writeStream = gfs.createWriteStream({
                _id: fileId,
                filename: filename,
                mode: 'w',
                content_type: mimetype,
                metadata: {
                    uploadedBy: req.user._id,
                    encoding: encoding,
                }
            });

            fsReadStream.on('end', function() {
                fs.unlink(tmpPath, function (err) {
                    if (err) throw err;
                    res.status(200).send({
                        message: fileId.toString()
                    }); 
                });
            });

            fsReadStream.pipe(writeStream);

        });

        file.pipe(fsWriteStream);
    });

    req.pipe(busboy);
};

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
wdawson4commented, Jun 4, 2015

@pos1tron I am experiencing the same issue. Were you able to figure out what was causing this?

I managed to debug the issue. I narrowed it down to where i was confident that the problem was a piece of express middleware modified the request. I disabled my middleware one by one until i found the unlikely culprit: connect-livereload

I commented out app.use(require('connect-livereload')()); and the problem went away. I believe it was injecting the livereload script into the response (a binary image file).

0reactions
SOSANAcommented, Jun 28, 2015

1+ thanks everyone! helped me as well

Read more comments on GitHub >

github_iconTop Results From Across the Web

gridfs-stream - npm
Writable/Readable Nodejs compatible GridFS streams. Latest version: 1.1.1, last published: 8 years ago. Start using gridfs-stream in your ...
Read more >
GridFS API - GitHub Pages
The MongoDB Node.js driver now supports a stream-based API for GridFS that's compatible with Node.js' streams3, so you can .pipe() directly from file ......
Read more >
GridFS — Node.js - MongoDB
If there are multiple documents with the same filename value, GridFS will stream the most recent file with the given name (as determined...
Read more >
Gridfs | npm.io
Gridfs Packages. gridfs-stream. Writable/Readable Nodejs compatible GridFS streams. mongodbmongoosegridfs. 1.1.1 • ...
Read more >
gridfs-stream examples - CodeSandbox
Learn how to use gridfs-stream by viewing and forking example apps that make use of gridfs-stream on CodeSandbox. ; Latest version1.1.1. License ;...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found