1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CybozuHttp\Api\Kintone; |
4
|
|
|
|
5
|
|
|
use CybozuHttp\Client; |
6
|
|
|
use CybozuHttp\Api\KintoneApi; |
7
|
|
|
use CybozuHttp\Middleware\JsonStream; |
8
|
|
|
use GuzzleHttp\Pool; |
9
|
|
|
use GuzzleHttp\Psr7\MultipartStream; |
10
|
|
|
use GuzzleHttp\Psr7\Request; |
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author ochi51 <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class File |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var Client |
21
|
|
|
*/ |
22
|
|
|
private $client; |
23
|
|
|
|
24
|
1 |
|
public function __construct(Client $client) |
25
|
|
|
{ |
26
|
1 |
|
$this->client = $client; |
27
|
1 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Get file |
31
|
|
|
* https://cybozudev.zendesk.com/hc/ja/articles/202166180#step1 |
32
|
|
|
* |
33
|
|
|
* @param string $fileKey |
34
|
|
|
* @param int $guestSpaceId |
35
|
|
|
* @return string |
36
|
|
|
*/ |
37
|
1 |
|
public function get($fileKey, $guestSpaceId = null) |
38
|
|
|
{ |
39
|
1 |
|
$options = ['json' => ['fileKey' => $fileKey]]; |
40
|
1 |
|
$response = $this->client->get(KintoneApi::generateUrl('file.json', $guestSpaceId), $options); |
41
|
|
|
|
42
|
1 |
|
return (string)$response->getBody(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get file stream response |
47
|
|
|
* https://cybozudev.zendesk.com/hc/ja/articles/202166180#step1 |
48
|
|
|
* |
49
|
|
|
* @param string $fileKey |
50
|
|
|
* @param int $guestSpaceId |
51
|
|
|
* @return ResponseInterface |
52
|
|
|
*/ |
53
|
1 |
|
public function getStreamResponse($fileKey, $guestSpaceId = null) |
54
|
|
|
{ |
55
|
|
|
$options = [ |
56
|
1 |
|
'json' => ['fileKey' => $fileKey], |
57
|
|
|
'stream' => true |
58
|
1 |
|
]; |
59
|
1 |
|
$response = $this->client->get(KintoneApi::generateUrl('file.json', $guestSpaceId), $options); |
60
|
|
|
|
61
|
1 |
|
return $response; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param array $fileKeys |
66
|
|
|
* @param int|null $guestSpaceId |
67
|
|
|
* @return array [contents, contents, ...] The order of $fileKeys |
68
|
|
|
*/ |
69
|
1 |
|
public function multiGet(array $fileKeys, $guestSpaceId = null) |
70
|
|
|
{ |
71
|
1 |
|
$result = []; |
72
|
1 |
|
$concurrency = $this->client->getConfig('concurrency'); |
73
|
1 |
|
$headers = $this->client->getConfig('headers'); |
74
|
1 |
|
$headers['Content-Type'] = 'application/json'; |
75
|
1 |
|
$url = KintoneApi::generateUrl('file.json', $guestSpaceId); |
76
|
|
|
$requests = function () use ($fileKeys, $url, $headers) { |
77
|
1 |
|
foreach ($fileKeys as $fileKey) { |
78
|
1 |
|
$body = \GuzzleHttp\json_encode(['fileKey' => $fileKey]); |
79
|
1 |
|
yield new Request('GET', $url, $headers, $body); |
80
|
1 |
|
} |
81
|
1 |
|
}; |
82
|
1 |
|
$pool = new Pool($this->client, $requests(), [ |
83
|
1 |
|
'concurrency' => $concurrency ?: 1, |
84
|
|
|
'fulfilled' => function (ResponseInterface $response, $index) use (&$result) { |
85
|
1 |
|
$result[$index] = (string)$response->getBody(); |
86
|
1 |
|
} |
87
|
1 |
|
]); |
88
|
1 |
|
$pool->promise()->wait(); |
89
|
|
|
|
90
|
1 |
|
return $result; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Post file |
95
|
|
|
* https://cybozudev.zendesk.com/hc/ja/articles/201941824#step1 |
96
|
|
|
* |
97
|
|
|
* @param string $path |
98
|
|
|
* @param int|null $guestSpaceId |
99
|
|
|
* @param string|null $filename |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
2 |
|
public function post($path, $guestSpaceId = null, $filename = null) |
103
|
|
|
{ |
104
|
2 |
|
$options = ['multipart' => [self::createMultipart($path, $filename)]]; |
105
|
2 |
|
$this->changeLocale(); |
106
|
|
|
|
107
|
|
|
/** @var JsonStream $stream */ |
108
|
2 |
|
$stream = $this->client |
109
|
2 |
|
->post(KintoneApi::generateUrl('file.json', $guestSpaceId), $options) |
110
|
2 |
|
->getBody(); |
111
|
|
|
|
112
|
2 |
|
return $stream->jsonSerialize()['fileKey']; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param array $fileNames |
117
|
|
|
* @param int|null $guestSpaceId |
118
|
|
|
* @return array [fileKey, fileKey, ...] The order of $fileNames |
119
|
|
|
* @throws \InvalidArgumentException |
120
|
|
|
*/ |
121
|
1 |
|
public function multiPost(array $fileNames, $guestSpaceId = null) |
122
|
|
|
{ |
123
|
1 |
|
$this->changeLocale(); |
124
|
|
|
|
125
|
1 |
|
$result = []; |
126
|
1 |
|
$concurrency = $this->client->getConfig('concurrency'); |
127
|
1 |
|
$headers = $this->client->getConfig('headers'); |
128
|
1 |
|
$url = KintoneApi::generateUrl('file.json', $guestSpaceId); |
129
|
|
|
$requests = function () use ($fileNames, $url, $headers) { |
130
|
1 |
|
foreach ($fileNames as $filename) { |
131
|
1 |
|
$body = new MultipartStream([self::createMultipart($filename)]); |
132
|
1 |
|
yield new Request('POST', $url, $headers, $body); |
133
|
1 |
|
} |
134
|
1 |
|
}; |
135
|
1 |
|
$pool = new Pool($this->client, $requests(), [ |
136
|
1 |
|
'concurrency' => $concurrency ?: 1, |
137
|
1 |
|
'fulfilled' => function (ResponseInterface $response, $index) use (&$result) { |
138
|
|
|
/** @var JsonStream $stream */ |
139
|
1 |
|
$stream = $response->getBody(); |
140
|
1 |
|
$result[$index] = $stream->jsonSerialize()['fileKey']; |
141
|
1 |
|
} |
142
|
1 |
|
]); |
143
|
1 |
|
$pool->promise()->wait(); |
144
|
1 |
|
ksort($result); |
145
|
|
|
|
146
|
1 |
|
return $result; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Returns locale independent base name of the given path. |
151
|
|
|
* |
152
|
|
|
* @param string $name The new file name |
153
|
|
|
* @return string containing |
154
|
|
|
*/ |
155
|
12 |
|
public static function getFilename($name) |
156
|
|
|
{ |
157
|
12 |
|
$originalName = str_replace('\\', '/', $name); |
158
|
12 |
|
$pos = strrpos($originalName, '/'); |
159
|
12 |
|
$originalName = false === $pos ? $originalName : substr($originalName, $pos + 1); |
160
|
|
|
|
161
|
12 |
|
return $originalName; |
162
|
|
|
} |
163
|
|
|
|
164
|
3 |
|
private function changeLocale() |
165
|
|
|
{ |
166
|
3 |
|
$baseUri = $this->client->getConfig('base_uri'); |
167
|
3 |
|
if (strpos($baseUri->getHost(), 'cybozu.com') > 0) { // Japanese kintone |
168
|
3 |
|
setlocale(LC_ALL, 'ja_JP.UTF-8'); |
169
|
3 |
|
} |
170
|
3 |
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param string $path |
174
|
|
|
* @param string|null $filename |
175
|
|
|
* @return array |
176
|
|
|
*/ |
177
|
3 |
|
private static function createMultipart($path, $filename = null) |
178
|
|
|
{ |
179
|
|
|
return [ |
180
|
3 |
|
'name' => 'file', |
181
|
3 |
|
'filename' => self::getFilename($filename ?: $path), |
182
|
3 |
|
'contents' => fopen($path, 'rb'), |
183
|
3 |
|
'headers' => ['Content-Type' => mime_content_type($path)] |
184
|
3 |
|
]; |
185
|
|
|
} |
186
|
|
|
} |