Skip to content

Commit 57b3ceb

Browse files
committed
Added support for downloading attachments
1 parent 8ef5bbd commit 57b3ceb

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed

example.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,9 @@
213213
// Attachments
214214
$client->api('attachment')->show($attachmentId);
215215

216+
$file_content = $client->api('attachment')->download($attachmentId);
217+
file_put_contents('example.png', $file_content);
218+
216219
// ----------------------------
217220
// News
218221
$client->api('news')->all('test');

lib/Redmine/Api/AbstractApi.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ public function lastCallFailed()
4444
*
4545
* @return array
4646
*/
47-
protected function get($path)
47+
protected function get($path, $decode = true)
4848
{
49-
return $this->client->get($path);
49+
return $this->client->get($path, $decode);
5050
}
5151

5252
/**

lib/Redmine/Api/Attachment.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ public function show($id)
2222
return $this->get('/attachments/'.urlencode($id).'.json');
2323
}
2424

25+
/**
26+
* Get attachment content as a binary file
27+
*
28+
* @param string $id the attachment number
29+
* @return string the attachment content
30+
*/
31+
public function download($id)
32+
{
33+
return $this->get('/attachments/'.urlencode($id), false);
34+
}
35+
2536
/**
2637
* Upload a file to redmine
2738
* @link http://www.redmine.org/projects/redmine/wiki/Rest_api#Attaching-files

lib/Redmine/Client.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,16 @@ public function getUrl()
149149
* @param string $path
150150
* @return array
151151
*/
152-
public function get($path)
152+
public function get($path, $decode = true)
153153
{
154154
if (false === $json = $this->runRequest($path, 'GET')) {
155155
return false;
156156
}
157157

158+
if (!$decode) {
159+
return $json;
160+
}
161+
158162
return $this->decode($json);
159163
}
160164

0 commit comments

Comments
 (0)