Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

updated MongoConnection for GridStore read

  • Loading branch information...
commit 88ed02fb8cdfd10537814193ceafb12187975239 1 parent 3e6be2c
authored

Showing 1 changed file with 59 additions and 0 deletions. Show diff stats Hide diff stats

  1. +59 0 lib/MongoConnection.js
59 lib/MongoConnection.js
@@ -23,6 +23,9 @@
23 23 MongoConnection.dropCollection(collectionName, <callback>)
24 24 MongoConnection.ensureIndex(collectionName, index, <options>, <callback>)
25 25 MongoConnection.findAndModify(inCollection, query, sort, doc, options, callback)
  26 + MongoConnection.readGridFS(fileName, callback)
  27 + MongoConnection.streamGridFS(fileName, callback)
  28 + ** callback will be called with (err, data) or in the case of EOF (err, NULL)
26 29 */
27 30
28 31 var mongodb = require("mongodb");
@@ -31,6 +34,7 @@ var Connection = mongodb.Connection;
31 34 var Server = mongodb.Server;
32 35 var ReplSet = mongodb.ReplSet;
33 36 var Db = mongodb.Db;
  37 +var GridStore = mongodb.GridStore;
34 38
35 39 var errors = {
36 40 E_CONNCLOSED: 'No active server connection!',
@@ -354,4 +358,59 @@ MongoConnection.prototype.ensureIndex = function(collectionName, index, options,
354 358 throw errors.E_INVALIDINDEX;
355 359 }
356 360 self.db.ensureIndex(collectionName, index, options, callback);
  361 +};
  362 +
  363 +MongoConnection.prototype.readGridFS = function(fileName, callback){
  364 + var self = this;
  365 + if(!self.active){
  366 + throw errors.E_CONNCLOSED;
  367 + }
  368 + GridStore.exist(self.db, fileName, function(err, exists){
  369 + if(exists===false){
  370 + callback(err, false);
  371 + }else{
  372 + var gridStore = new GridStore(self.db, fileName, 'r');
  373 + gridStore.open(function(err, gridStore) {
  374 + gridStore.read(function(){
  375 + callback.apply(self, arguments);
  376 + gridStore.close(function(){});
  377 + });
  378 + });
  379 + }
  380 + });
  381 +};
  382 +
  383 +MongoConnection.prototype.streamGridFS = function(fileName, callback){
  384 + var self = this;
  385 + if(!self.active){
  386 + throw errors.E_CONNCLOSED;
  387 + }
  388 + GridStore.exist(self.db, fileName, function(err, exists){
  389 + if(!exists){
  390 + callback(err, false);
  391 + }else{
  392 + var gridStore = new GridStore(self.db, fileName, 'r');
  393 + var doput = function(done){
  394 + return function(data){
  395 + if(data){
  396 + callback(null, data.toString());
  397 + }
  398 + if(done){
  399 + callback(null, null);
  400 + }
  401 + };
  402 + };
  403 + var doerror = function(done){
  404 + return function(err){
  405 + callback(err, null);
  406 + };
  407 + };
  408 + gridStore.open(function(err, gridStore) {
  409 + var stream = gridStore.stream(true);
  410 + stream.on('data', doput(false));
  411 + stream.on('error', doerror(true));
  412 + stream.on('end', doput(true));
  413 + });
  414 + }
  415 + });
357 416 };

0 comments on commit 88ed02f

Please sign in to comment.
Something went wrong with that request. Please try again.