1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NStack\Clients; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use NStack\Exceptions\FailedToParseException; |
7
|
|
|
use NStack\Models\File; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class FilesClient |
11
|
|
|
* |
12
|
|
|
* @package NStack\Clients |
13
|
|
|
* @author Tiago Araujo <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class FilesClient extends NStackClient |
16
|
|
|
{ |
17
|
|
|
/** @var string */ |
18
|
|
|
protected $path = 'content/files'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* store |
22
|
|
|
* |
23
|
|
|
* @param String $name |
24
|
|
|
* @param array $tagsArray |
25
|
|
|
* @param DateTime $goneAt |
26
|
|
|
* @param String $privacy |
27
|
|
|
* @param String $imagePath |
28
|
|
|
* @return File |
29
|
|
|
* @throws FailedToParseException |
30
|
|
|
*/ |
31
|
1 |
|
public function store( |
32
|
|
|
String $name, |
33
|
|
|
String $privacy, |
34
|
|
|
String $imagePath, |
35
|
|
|
array $tagsArray = null, |
36
|
|
|
DateTime $goneAt = null |
37
|
|
|
) { |
38
|
1 |
|
$response = $this->client->post($this->buildPath($this->path), [ |
39
|
|
|
'form_params' => [ |
40
|
1 |
|
'name' => $name, |
41
|
1 |
|
'tags' => is_null($tagsArray) ? null : implode(",", $tagsArray), |
42
|
1 |
|
'gone_at' => $goneAt, |
43
|
1 |
|
'privacy' => $privacy, |
44
|
1 |
|
'file' => fopen($imagePath, 'r'), |
45
|
|
|
], |
46
|
|
|
]); |
47
|
1 |
|
$contents = $response->getBody()->getContents(); |
48
|
1 |
|
$data = json_decode($contents, true); |
49
|
|
|
|
50
|
1 |
|
return new File($data['data']); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* storeWithPath |
55
|
|
|
* |
56
|
|
|
* @param String $name |
57
|
|
|
* @param String $privacy |
58
|
|
|
* @param String $path |
59
|
|
|
* @param String $mime |
60
|
|
|
* @param int $size |
61
|
|
|
* @param array|null $tagsArray |
62
|
|
|
* @param DateTime|null $goneAt |
63
|
|
|
* @return File |
64
|
|
|
* @throws FailedToParseException |
65
|
|
|
*/ |
66
|
1 |
|
public function storeWithPath( |
67
|
|
|
String $name, |
68
|
|
|
String $privacy, |
69
|
|
|
String $path, |
70
|
|
|
String $mime, |
71
|
|
|
int $size, |
72
|
|
|
array $tagsArray = null, |
73
|
|
|
DateTime $goneAt = null |
74
|
|
|
) { |
75
|
1 |
|
$response = $this->client->post($this->buildPath($this->path . '/path'), [ |
76
|
|
|
'form_params' => [ |
77
|
1 |
|
'name' => $name, |
78
|
1 |
|
'tags' => is_null($tagsArray) ? null : implode(",", $tagsArray), |
79
|
1 |
|
'gone_at' => $goneAt, |
80
|
1 |
|
'privacy' => $privacy, |
81
|
1 |
|
'path' => $path, |
82
|
1 |
|
'mime' => $mime, |
83
|
1 |
|
'size' => $size, |
84
|
|
|
], |
85
|
|
|
]); |
86
|
1 |
|
$contents = $response->getBody()->getContents(); |
87
|
1 |
|
$data = json_decode($contents, true); |
88
|
|
|
|
89
|
1 |
|
return new File($data['data']); |
90
|
|
|
} |
91
|
|
|
} |