2

I have a document with an image attachment myimg.jpg which I would like to GET using Sag.

In my browser I am able to retrieve this image if I visit this url: http://localhost:5984/mydb/thedocid/myimg.jpg.

Using sag I am able to retrieve documents, but unable to retrieve attachments. I have tried to retrieve the image like so:

$img = $sag->get('thedocid/myimg.jpg')->body;

Instead of retrieving the image PHP seems to become unresponsive. I also thought disabling JSON decode might solve it, but it still causes PHP to become unresponsive.

$sag->decode(false);
$img = $sag->get('thedocid/myimg.jpg');

What am I doing wrong? How does one properly retrieve an attachment using Sag?

EDIT: After quite some time the attachment has been retrieved. Why is it so slow? The attachment is merely 4kb.

user1973386
  • 1,095
  • 2
  • 10
  • 18
  • 1
    That looks about right. For some inspiration, have a look at the test_attachments() method here: https://github.com/sbisbee/sag/blob/master/tests/SagTest.php – Dave Morrissey Jul 14 '14 at 22:32

1 Answers1

2

I still do not know why my initial code was so unresponsive/extremely slow, but thanks to Dave's comment I got an alternative way to retrieve the document with the attachment:

$doc = $sag->get('thedocid/?attachments=true')->body;
$img = base64_decode($doc->_attachments->{'myimg.jpg'}->data);
user1973386
  • 1,095
  • 2
  • 10
  • 18