Skip to content

Commit 83742d4

Browse files
committed
Merge pull request kbsali#117 from gpilla/master
Added support for downloading attachments
2 parents 8ef5bbd + 10ed240 commit 83742d4

File tree

5 files changed

+24
-5
lines changed

5 files changed

+24
-5
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: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,20 @@ public function getUrl()
146146

147147
/**
148148
* HTTP GETs a json $path and tries to decode it
149-
* @param string $path
149+
* @param string $path
150+
* @param boolean $decode
150151
* @return array
151152
*/
152-
public function get($path)
153+
public function get($path, $decode = true)
153154
{
154155
if (false === $json = $this->runRequest($path, 'GET')) {
155156
return false;
156157
}
157158

159+
if (!$decode) {
160+
return $json;
161+
}
162+
158163
return $this->decode($json);
159164
}
160165

lib/Redmine/TestUrlClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
class TestUrlClient extends Client
66
{
7-
public function get($path)
7+
public function get($path, $decode = true)
88
{
99
return $this->runRequest($path, 'GET');
1010
}

0 commit comments

Comments
 (0)