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

Add method to support retrieving file attachments #64

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions lib/Doctrine/CouchDB/CouchDBClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
30 changes: 30 additions & 0 deletions tests/Doctrine/Tests/CouchDB/Functional/CouchDBClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down