1

I'm storing documents with attachments in Cloudant database and I'm using nano client (link provided here: https://github.com/dscape/nano) multipart get to retrieve both the document and the attachment at the same time. The api call works and I get a buffer which is what the documentation says I should get; however I don't know how to parse/split the buffer so I can separate the document and the attachment (a JPG).

The JSON object stored in the database looks like this:

{
  "_id": "3452345",
  "_rev": "12345",
  "projectName": "Project",
  "projectUrl": "https://github.com/",
  "projectDescription": "project",
  "_attachments": {
     "image": {
        "content_type": "image/png",
        "revpos": 1,
        "digest": "md5-yh3lM9PmqK2TCXc9OxSldg==",
        "length": 1396888,
        "stub": true
     }
  }
}

The following function is from nano client and returns a buffer; which I believe contains both the document and the actual image attachment in that buffer. How can I separate or parse the buffer so I can retrieve the document data to convert it to JSON and then the image attachment to have it as base64 encoded.

db.multipart.get('myDoc', function(err, buffer) {
  if (!err){
    //not sure how to split/parse the buffer to retrieve doc and att
  }  
});

Note: I can retrieve the document and the attachment separately however I have two perform to http requests. The document http request returns a JSON object and the attachment returns a buffer which I then use buffer.toString('base64'); and then I can easily retrieve the image.

If you know a way to extract the data from the nodeJS buffer or a better way of doing this please leave a comment. Finally please choose methods that are efficient and quick.

Thank you

Matthew Diaz
  • 11
  • 1
  • 2

1 Answers1

0

You can convert Buffer to JSON:

buffer.toJSON()

Here is documentation for more details: