Question

I have written an objective-c app that can write files to my meteor mongodb database. Using RadMongoDB (https://github.com/timburks/RadMongoDB) I write an image over to my mongo's gridfs .files and .chunks.

//Defining RadMongoDB
RadMongoDB *rad = [[RadMongoDB alloc] init];

//Connection Dictionary
NSDictionary *connection = @{
                             @"host" : @"127.0.0.1",
                             @"port" : [NSNumber numberWithInt:3002]};

int num =[rad connectWithOptions:connection];
[rad writeFile:path2 withMIMEType:@"image/png" inCollection:@"contacts" inDatabase:@"meteor"]

The image (path2) is successfully written to the gridfs. Im my meteor mondgodb shell I can see the files successfully written.

enter image description here

.chunks:

enter image description here

.files:

enter image description here

These gridfs files are linked to a collectionfs (https://github.com/CollectionFS/Meteor-CollectionFS) collection containing a variety of other pictures that have been inserted via a meteor app. The problem is pulling the image out written by the driver using collectionfs. It is apparent that the files written to gridfs by the driver do not get worked on by the file handlers. Therefore I tried re-forcing all of the files through (collectionfs filehandler reset) but that still didn't work: (Javascript Below, note ContactsFS is the collectionfs collection corresponding to gridfs contacts).

 //Reset
  ContactsFS.find({}).forEach(function(doc) {
    ContactsFS.update({ _id: doc._id}, { $set: { handledAt: null, fileHandler: {} }  });
  });
  //Set Completed to True
  ContactsFS.update(fileRecord, {$set: {complete: true}});

I have come to the conclusion that the way the driver interacts with gridfs is much different than how meteor and collectionfs read and write to it. Is there any way to fix this? I am desperate for help, thanks!

EDIT:

After setting the uploaded files complete = true the file handler attempts to act on the drivers inserted file. However now I receive a server side error: enter image description here

I believe this is because of how collectionfs reads the gridfs file. The gridfs image's data is stored by the obj-c driver as a Uint8Array (as seen in screenshot 1). I have tried setting each paramater on the obj-c driver image so that collectionfs will be happy:

ContactsFS.update(imageid, {$set: {handledAt: null}});
ContactsFS.update(imageid, {$set: {uploadDate: date}});
ContactsFS.update(imageid, {$set: {countChunks: 1}});
ContactsFS.update(imageid, {$set: {numChunks: 1}});
ContactsFS.update(imageid, {$set: {length: len}});›
ContactsFS.update(imageid, {$set: {uploading: false}});
ContactsFS.update(imageid, {$set: {encoding: encode}});

//Setting complete to True will send the file through the filehandlers
ContactsFS.update(imageid, {$set: {complete: true}});

Still nothing. How do I get around this problem?

Was it helpful?

Solution

Try this:

var len = "" + fileRecord.plength;
var chunkSize = 256 * 1024; // set this to whatever chunk size RadMongoDB is using
var chunkCount = Math.ceil(fileRecord.plength / chunkSize);

ContactsFS.update(imageid, {$set: {
  handledAt: null,
  uploadDate: Date.now(),
  countChunks: chunkCount,
  numChunks: chunkCount,
  length: len,
  uploading: false,
  encoding: 'binary'
}});

ContactsFS.update(imageid, {$set: {complete: true}});

Requires the fix discussed in this issue, too.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top