Skip to content

Commit

Permalink
fix: save multiple objects in GridFS and download binaries
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaixhin committed Oct 25, 2015
1 parent b16e35f commit 1186de5
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions index.js
Expand Up @@ -338,17 +338,16 @@ app.put("/api/experiments/:id/files", upload.array("_files"), function(req, res,
delete req.body._id; // Delete ID (will not update otherwise)
var filesP = Array(req.files.length);

for (var i = 0; i < req.files.length; i++) {
var file = req.files[i];
var saveGFSFile = function(fileObj) {
var fileId = new db.ObjectID(); // Create file ID
// Open new file
var gfs = new db.GridStore(db, fileId, file.originalname, "w", {content_type: file.mimetype, promiseLibrary: Promise});
var gfs = new db.GridStore(db, fileId, fileObj.originalname, "w", {content_type: fileObj.mimetype, promiseLibrary: Promise});
gfs.open(function(err, gfs) {
if (err) {
console.log(err);
} else {
// Write from buffer and flush to db
gfs.write(file.buffer, true)
gfs.write(fileObj.buffer, true)
.then(function(gfs) {
// Save file reference
filesP[i] = db.experiments.updateByIdAsync(req.params.id, {$push: {_files: {_id: gfs.fileId, filename: gfs.filename}}});
Expand All @@ -358,6 +357,10 @@ app.put("/api/experiments/:id/files", upload.array("_files"), function(req, res,
});
}
});
};

for (var i = 0; i < req.files.length; i++) {
saveGFSFile(req.files[i]); // Save file in function closure
}

// Check file promises
Expand Down Expand Up @@ -481,7 +484,7 @@ app.get("/files/:id", function(req, res, next) {
.then(function(file) {
res.setHeader("Content-Disposition", "attachment; filename=" + gfs.filename); // Set as download
res.setHeader("Content-Type", gfs.contentType); // MIME Type
res.send(file.toString()); // Send file
res.send(file); // Send file
})
.catch(function(err) {
next(err);
Expand Down

0 comments on commit 1186de5

Please sign in to comment.