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 $filename |
98
|
|
|
* @param int $guestSpaceId |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
2 |
|
public function post($filename, $guestSpaceId = null) |
102
|
|
|
{ |
103
|
2 |
|
$options = ['multipart' => [self::createMultipart($filename)]]; |
104
|
2 |
|
$this->changeLocale(); |
105
|
|
|
|
106
|
|
|
/** @var JsonStream $stream */ |
107
|
2 |
|
$stream = $this->client |
108
|
2 |
|
->post(KintoneApi::generateUrl('file.json', $guestSpaceId), $options) |
109
|
2 |
|
->getBody(); |
110
|
|
|
|
111
|
2 |
|
return $stream->jsonSerialize()['fileKey']; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param array $fileNames |
116
|
|
|
* @param int|null $guestSpaceId |
117
|
|
|
* @return array [fileKey, fileKey, ...] The order of $fileNames |
118
|
|
|
* @throws \InvalidArgumentException |
119
|
|
|
*/ |
120
|
1 |
|
public function multiPost(array $fileNames, $guestSpaceId = null) |
121
|
|
|
{ |
122
|
1 |
|
$this->changeLocale(); |
123
|
|
|
|
124
|
1 |
|
$result = []; |
125
|
1 |
|
$concurrency = $this->client->getConfig('concurrency'); |
126
|
1 |
|
$headers = $this->client->getConfig('headers'); |
127
|
1 |
|
$url = KintoneApi::generateUrl('file.json', $guestSpaceId); |
128
|
|
|
$requests = function () use ($fileNames, $url, $headers) { |
129
|
1 |
|
foreach ($fileNames as $filename) { |
130
|
1 |
|
$body = new MultipartStream([self::createMultipart($filename)]); |
131
|
1 |
|
yield new Request('POST', $url, $headers, $body); |
132
|
1 |
|
} |
133
|
1 |
|
}; |
134
|
1 |
|
$pool = new Pool($this->client, $requests(), [ |
135
|
1 |
|
'concurrency' => $concurrency ?: 1, |
136
|
1 |
|
'fulfilled' => function (ResponseInterface $response, $index) use (&$result) { |
137
|
|
|
/** @var JsonStream $stream */ |
138
|
1 |
|
$stream = $response->getBody(); |
139
|
1 |
|
$result[$index] = $stream->jsonSerialize()['fileKey']; |
140
|
1 |
|
} |
141
|
1 |
|
]); |
142
|
1 |
|
$pool->promise()->wait(); |
143
|
1 |
|
ksort($result); |
144
|
|
|
|
145
|
1 |
|
return $result; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Returns locale independent base name of the given path. |
150
|
|
|
* |
151
|
|
|
* @param string $name The new file name |
152
|
|
|
* @return string containing |
153
|
|
|
*/ |
154
|
12 |
|
public static function getFilename($name) |
155
|
|
|
{ |
156
|
12 |
|
$originalName = str_replace('\\', '/', $name); |
157
|
12 |
|
$pos = strrpos($originalName, '/'); |
158
|
12 |
|
$originalName = false === $pos ? $originalName : substr($originalName, $pos + 1); |
159
|
|
|
|
160
|
12 |
|
return $originalName; |
161
|
|
|
} |
162
|
|
|
|
163
|
3 |
|
private function changeLocale() |
164
|
|
|
{ |
165
|
3 |
|
$baseUri = $this->client->getConfig('base_uri'); |
166
|
3 |
|
if (strpos($baseUri->getHost(), 'cybozu.com') > 0) { // Japanese kintone |
167
|
3 |
|
setlocale(LC_ALL, 'ja_JP.UTF-8'); |
168
|
3 |
|
} |
169
|
3 |
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param string $filename |
173
|
|
|
* @return array |
174
|
|
|
*/ |
175
|
3 |
|
private static function createMultipart($filename) |
176
|
|
|
{ |
177
|
|
|
return [ |
178
|
3 |
|
'name' => 'file', |
179
|
3 |
|
'filename' => self::getFilename($filename), |
180
|
3 |
|
'contents' => fopen($filename, 'rb'), |
181
|
3 |
|
'headers' => ['Content-Type' => mime_content_type($filename)] |
182
|
3 |
|
]; |
183
|
|
|
} |
184
|
|
|
} |