From 1997093ac490f7833dfc46e4676e5c58fd410669 Mon Sep 17 00:00:00 2001 From: Chad Bean Date: Sun, 5 Jun 2016 09:20:36 -0400 Subject: [PATCH 1/2] Add method to support retrieving file attachments --- lib/Doctrine/CouchDB/CouchDBClient.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/Doctrine/CouchDB/CouchDBClient.php b/lib/Doctrine/CouchDB/CouchDBClient.php index 87bf815..3b6c5bf 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 * From f3936701fe6050c1487b2dc68bf62b9ebaefc210 Mon Sep 17 00:00:00 2001 From: Chad Bean Date: Mon, 12 Sep 2016 14:31:16 -0400 Subject: [PATCH 2/2] Add functional test for getting attachments --- .../CouchDB/Functional/CouchDBClientTest.php | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) 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";