Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

updated MongoConnection for GridStore read

  • Loading branch information...
commit 88ed02fb8cdfd10537814193ceafb12187975239 1 parent 3e6be2c
@jdarling authored
Showing with 59 additions and 0 deletions.
  1. +59 −0 lib/MongoConnection.js
View
59 lib/MongoConnection.js
@@ -23,6 +23,9 @@
MongoConnection.dropCollection(collectionName, <callback>)
MongoConnection.ensureIndex(collectionName, index, <options>, <callback>)
MongoConnection.findAndModify(inCollection, query, sort, doc, options, callback)
+ MongoConnection.readGridFS(fileName, callback)
+ MongoConnection.streamGridFS(fileName, callback)
+ ** callback will be called with (err, data) or in the case of EOF (err, NULL)
*/
var mongodb = require("mongodb");
@@ -31,6 +34,7 @@ var Connection = mongodb.Connection;
var Server = mongodb.Server;
var ReplSet = mongodb.ReplSet;
var Db = mongodb.Db;
+var GridStore = mongodb.GridStore;
var errors = {
E_CONNCLOSED: 'No active server connection!',
@@ -354,4 +358,59 @@ MongoConnection.prototype.ensureIndex = function(collectionName, index, options,
throw errors.E_INVALIDINDEX;
}
self.db.ensureIndex(collectionName, index, options, callback);
+};
+
+MongoConnection.prototype.readGridFS = function(fileName, callback){
+ var self = this;
+ if(!self.active){
+ throw errors.E_CONNCLOSED;
+ }
+ GridStore.exist(self.db, fileName, function(err, exists){
+ if(exists===false){
+ callback(err, false);
+ }else{
+ var gridStore = new GridStore(self.db, fileName, 'r');
+ gridStore.open(function(err, gridStore) {
+ gridStore.read(function(){
+ callback.apply(self, arguments);
+ gridStore.close(function(){});
+ });
+ });
+ }
+ });
+};
+
+MongoConnection.prototype.streamGridFS = function(fileName, callback){
+ var self = this;
+ if(!self.active){
+ throw errors.E_CONNCLOSED;
+ }
+ GridStore.exist(self.db, fileName, function(err, exists){
+ if(!exists){
+ callback(err, false);
+ }else{
+ var gridStore = new GridStore(self.db, fileName, 'r');
+ var doput = function(done){
+ return function(data){
+ if(data){
+ callback(null, data.toString());
+ }
+ if(done){
+ callback(null, null);
+ }
+ };
+ };
+ var doerror = function(done){
+ return function(err){
+ callback(err, null);
+ };
+ };
+ gridStore.open(function(err, gridStore) {
+ var stream = gridStore.stream(true);
+ stream.on('data', doput(false));
+ stream.on('error', doerror(true));
+ stream.on('end', doput(true));
+ });
+ }
+ });
};
Please sign in to comment.
Something went wrong with that request. Please try again.