diff --git a/lib/Doctrine/CouchDB/CouchDBClient.php b/lib/Doctrine/CouchDB/CouchDBClient.php index ff8bb57..367386d 100644 --- a/lib/Doctrine/CouchDB/CouchDBClient.php +++ b/lib/Doctrine/CouchDB/CouchDBClient.php @@ -560,6 +560,24 @@ public function compactDatabase() return $response->body; } + /** + * Retrieve specific binary attachment data. + * + * @param string $id + * @param string $fileName + * @return string + */ + public function getAttachment($id, $fileName) { + $attachmentPath = '/' . $this->databaseName . '/' . $id . '/' . $fileName; + $response = $this->httpClient->request('GET', $attachmentPath, null, true); + + if ($response->status != 200) { + throw HTTPException::fromResponse($attachmentPath, $response); + } + + return $response->body; + } + /** * POST /db/_compact/designDoc * diff --git a/tests/Doctrine/Tests/CouchDB/Functional/CouchDBClientTest.php b/tests/Doctrine/Tests/CouchDB/Functional/CouchDBClientTest.php index a235727..6aecfc8 100644 --- a/tests/Doctrine/Tests/CouchDB/Functional/CouchDBClientTest.php +++ b/tests/Doctrine/Tests/CouchDB/Functional/CouchDBClientTest.php @@ -178,6 +178,36 @@ public function testPostDocument() $this->assertEquals(array("_id" => $id, "_rev" => $rev, "foo" => "bar"), $response->body); } + public function testGetAttachments() + { + $client = $this->couchClient; + + $mimeType = 'image/png'; + $base64Image = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEX/TQBcNTh/AAAAAXRSTlPM0jRW/QAAAApJREFUe' . + 'JxjYgAAAAYAAzY3fKgAAAAASUVORK5CYII='; // 1px^2 PNG image + + $fileName1 = 'file1.png'; + $fileName2 = 'file2.png'; + + $attachments = array( + $fileName1 => array( + 'content_type' => $mimeType, + 'data' => $base64Image, + ), + $fileName2 => array( + 'content_type' => $mimeType, + 'data' => $base64Image, + ), + ); + + list($id, $rev) = $client->postDocument(array("foo" => "bar", "_attachments" => $attachments)); + + $response = $client->findDocument($id); + $this->assertEquals($rev, $response->body['_rev']); + $this->assertEquals(count($response->body['_attachments']), count($attachments)); + $this->assertEquals(base64_encode($this->couchClient->getAttachment($id, $fileName1)), $base64Image); + } + public function testPutDocument() { $id = "foo-bar-baz";