0

I´m storing images from my angular app in MongoDB using GridFs. But i cant figure out, how to GET the images out of the DB to the app?
I´m using a custom objectId for the query.

EDIT
It looks like the GET part now works, but then there was no media in the collection. I played a bit with the code, and now I can see fs.chunks and fs.files in the database. I think the problem is, that I try to query for metadata in the GET request. This returns no response data. Anybody got an idea how to fix this?

var fs = require('fs');
var conn = mongoose.connection;
var Grid = require ('gridfs-stream');
Grid.mongo = mongoose.mongo;
var gfs = Grid (conn.db);
var buffer = "";


app.post('/uploads/', multer({
    upload: null,
    onFileUploadStart: function (file, req){
        this.upload = gfs.createWriteStream({
            filename: file.originalname,
            metadata:{"objectId" : req.body.project_id},
            mode: "w",
            chunkSize: 1024*4,
            content_type: file.mimetype,
            root: "fs",
        });
    },

    onFileUploadData: function(file, data) {
        this.upload.write(data);
    },
    onFileUploadComplete: function(file, res) {
        done=true;
    }
}), function(req, res){
    res.status(200);
    res.send("Success!");
});
app.route('/uploads/media/:projectId').get(function (req, res){
    var readstream = gfs.createReadStream({
        "metadata.objectId" : req.params.projectId
    });
    res.set('Content-Type', 'image/jpeg');
    readstream.pipe(res);
});
Wandkleister
  • 189
  • 1
  • 3
  • 13

2 Answers2

0

You need to write the stream back out to your response. Here is another similar question. But basically you either need to pipe the stream to your response, or use the stream's end event and write the result to your response. The following code pipes to the response and sets a content-type of image/jpeg.

app.get('/uploads/:objectId', function(req, res){

    var options = {
        _id : req.params.objectId
    };

    gfs.exist(options, function(err, exists) {
        if(!exists) {
            res.status(404);
            res.end();
        } else {
            var readstream = gfs.createReadStream(options);

            res.set('Content-Type', 'image/jpeg');

            readstream.pipe(res);
        }
    });
});
Community
  • 1
  • 1
officert
  • 1,232
  • 9
  • 12
  • the param definitly matches with the objectId in my entry fs.files metadata. maybe the problem is, that i´m not querying the default ObjectId of a GridFS entry. It´s a parameter in metadata – Wandkleister May 04 '15 at 19:30
  • Ok, I implemented the code slightly different than you, because it seemed like it didnt work properly. Now I´m getting a 404 – Wandkleister May 04 '15 at 19:48
  • Edited my initial post for you ... trying to work over the POST request – Wandkleister May 05 '15 at 12:10
0
        var pi_id   =  fields.pic_id;
        gfs.findOne({ _id: pi_id }, function (err, file) {
                    console.log(file);
                    if (err) return res.status(400).send(err);
                    if (!file) return res.status(404).send('');

                    res.set('Content-Type', file.contentType);
                    res.set('Content-Disposition', 'attachment; filename="' + file.filename + '"');

                    var readstream = gfs.createReadStream({
                      _id: file._id
                    });

                    readstream.on("error", function(err) {
                      console.log("Got error while processing stream " + err.message);
                      res.end();
                    });

                    readstream.pipe(res);

                    console.log(readstream.pipe(res))
                  });   
Jijo Paulose
  • 1,896
  • 18
  • 20