1
votes

I'm trying to insert a binary attachment to CouchDB with nano. I have a JPG in data returned by http.request.

I save it with nano as follows

db.attachment.insert( id, 'content', self._data, contentType, {rev: rev}, function(err, body) { 
        callback();
});

but when I try view it though a web browser the image is broken.

The file is full of UTF-8 escape characters which is visible when I pull it with CURL:

$ curl http://127.0.0.1:5984/web-crawler/doc-test.jpg/content
"ÿØÿà\u0000\u0010JFIF\u0000\u0001\u0001\u0001\u0000H\u0000H\u0000\u0000ÿâ\fXICC_PROFILE\u0000\u0001\u0001\u0000\u0000\fHLino\u0002\u0010\u0000\u0000mntrRGB XYZ \u0007Î\u0000\u0002\u0000\t\u0000\u0006\u00001\u0000\u0000acspMSFT\u0000\u0000\u0000\u0000IEC sRGB\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000öÖ\u0000\u0001\u0000\u0000\u0000\u0000Ó-HP  \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\

Content is not corrupted because if I save it to a file I can see the image.

var fs = require('fs');
fs.writeFile('logo.jpg', data, 'binary', function(err){
  if (err) throw err
  console.log('File saved.')
});

What is the right way to do it?

1
If you do curl -o doc-test.jpg http://127.0.0.1:5984/web-crawler/doc-test.jpg/content are you saying the file isn't valid? What mime/type is being returned from the server? - WiredPrairie
Yes, content returned by CouchDB is broken. It's not a mime/type issue. - Lukasz Kujawa
Still, what mime/type is being returned from the server? What contentType did you specify in the insert? - WiredPrairie

1 Answers

2
votes

Ok, this question can be close. The answer to my problem is Buffer:

db.attachment.insert( id, 'content', new Buffer(self._data, "binary"), contentType, {rev: rev}, function(err, body) { 
        callback();
});