Skip to content
This repository has been archived by the owner on Nov 5, 2018. It is now read-only.

Using examples from tutorials cannot upload PNG #82

Closed
InvisibleTech opened this issue May 24, 2012 · 3 comments
Closed

Using examples from tutorials cannot upload PNG #82

InvisibleTech opened this issue May 24, 2012 · 3 comments

Comments

@InvisibleTech
Copy link

All the code is at this gist, some is based on http://writings.nunojob.com/2011/08/nano-minimalistic-couchdb-client-for-nodejs.html, and some is based on https://github.com/mikeal/request, and some more is based on this repo's docs.

My code:

https://gist.github.com/e9c4501d68f218ec739a

Basically I can:

  • use Futon to upload a file - the PNG is viewable.
  • use the request npm to download the PNG it is viewable
  • upload the file PNG from my hard drive to Couch - I can view the PNG from Futon.
  • I cannot use the pipe to get the PNG from my couch server, and attach it to a new document as per the sample from Nuno's blog. The attachment only has 2 bytes and so is a broken PNG.

This happens on Windows XP and OS X 10.5.8

node version: v0.6.15
request version:  request@2.9.202
nano version: nano@3.0.5

couch: {"couchdb":"Welcome","version":"1.1.1"} on Win XP I think it is the same on OS X but I am not at that machine right now.

@dscape
Copy link
Contributor

dscape commented May 24, 2012

Gist e9c4501d68f218ec739a

var request = require('request');
var fs = require('fs');

request('http://localhost:5984/_utils/image/logo.png')
  .pipe(fs.createWriteStream('download_to_fs.png'));

// Test #2 --> Pipe not working with nano --
// I only get 2 bytes of the image with this one, it is a bad PNG!
  var db      = require('nano')('http://localhost:5984/test');
  var request = require('request');

  // {} for empty body as parameter is required but will be piped in
  request.get("http://localhost:5984/_utils/image/logo.png").pipe(
    db.attachment.insert("piped-in", "piped_in.png", {}, "image/png")
  );

// Test #3 --> Assumes you have run test #1 from same directory. 
// However, if I use the local file and post using nano... it works. 
//  Nano seems to be a-okay with file.
var db      = require('nano')('http://localhost:5984/test');
var fs = require('fs');

fs.readFile('logos_kronos.png', function(err, data) {
  if (!err) {
    db.attachment.insert('upload-from-fs', 'upload_from_fs.png', data, 'image/png',
      { }, function(err, body) {
        if (!err)
          console.log(body);
    });
  }
});

@dscape dscape closed this as completed in baf3505 May 25, 2012
dscape added a commit that referenced this issue May 25, 2012
@dscape
Copy link
Contributor

dscape commented May 25, 2012

Thanks for the bug report.

Everywhere you could see

  request.get("http://nodejs.org/logo.png").pipe(
    db.attachment.insert("new", "logo.png", {}, "image/png")
  );

Should have been

  request.get("http://nodejs.org/logo.png").pipe(
    db.attachment.insert("new", "logo.png", null, "image/png")
  );

Use null not {} when inserting attachments.

I've added this to the tests, to make sure it's properly tested going forward (it wasn't, hence the bad documentation and mistakes on my side)

@dscape
Copy link
Contributor

dscape commented May 25, 2012

Also sorry for the inconvenience. I should have added this test earlier. Was lame on my behalf

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants