1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JiraRestApi\Attachment; |
4
|
|
|
|
5
|
|
|
use JiraRestApi\Issue\Attachment; |
6
|
|
|
use JiraRestApi\JiraClient; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class AttachmentService |
10
|
|
|
* |
11
|
|
|
* @package JiraRestApi\Group |
12
|
|
|
*/ |
13
|
|
|
class AttachmentService extends \JiraRestApi\JiraClient |
14
|
|
|
{ |
15
|
|
|
private $uri = '/attachment/'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Returns the meta-data for an attachment, including the URI of the actual attached file. |
19
|
|
|
* |
20
|
|
|
* @param $id string|int attachment Id |
21
|
|
|
* @outDir string downloads the content and store into outDir |
22
|
|
|
* @overwrite boolean determines whether to overwrite the file if it already exists. |
23
|
|
|
* |
24
|
|
|
* @return \JiraRestApi\Issue\Attachment |
25
|
|
|
* |
26
|
|
|
* @throws \JiraRestApi\JiraException |
27
|
|
|
* @throws \JsonMapper_Exception |
28
|
|
|
*/ |
29
|
|
|
public function get($id, $outDir = null, $overwrite = false) |
30
|
|
|
{ |
31
|
|
|
$ret = $this->exec($this->uri.$id, null); |
32
|
|
|
|
33
|
|
|
$this->log->addInfo("Result=\n".$ret); |
34
|
|
|
|
35
|
|
|
$attachment = $this->json_mapper->map( |
36
|
|
|
json_decode($ret), new Attachment() |
37
|
|
|
); |
38
|
|
|
|
39
|
|
|
if ($outDir == null) { |
40
|
|
|
return $attachment; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
// download contents |
44
|
|
|
if (! file_exists($outDir)) { |
45
|
|
|
mkdir($outDir); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
// extract filename |
49
|
|
|
$file = substr(strrchr($attachment->content, '/'), 1); |
50
|
|
|
|
51
|
|
|
if (file_exists($outDir.DIRECTORY_SEPARATOR.$file) && $overwrite == false) { |
52
|
|
|
return $attachment; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$this->download($attachment->content, $outDir, $file); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Remove an attachment from an issue. |
60
|
|
|
* |
61
|
|
|
* @param $id string|int attachment id |
62
|
|
|
* @return boolean |
63
|
|
|
* |
64
|
|
|
* @throws \JiraRestApi\JiraException |
65
|
|
|
*/ |
66
|
|
|
public function remove($id) |
67
|
|
|
{ |
68
|
|
|
$ret = $this->exec($this->uri.$id, null, 'DELETE'); |
69
|
|
|
|
70
|
|
|
$this->log->addInfo("Result=\n".$ret); |
71
|
|
|
|
72
|
|
|
return $ret; |
|
|
|
|
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|