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
|
|
|
* @param array $fileKeys |
47
|
|
|
* @param int|null $guestSpaceId |
48
|
|
|
* @return array [contents, contents, ...] The order of $fileKeys |
49
|
|
|
*/ |
50
|
1 |
|
public function multiGet(array $fileKeys, $guestSpaceId = null) |
51
|
|
|
{ |
52
|
1 |
|
$result = []; |
53
|
1 |
|
$concurrency = $this->client->getConfig('concurrency'); |
54
|
1 |
|
$headers = $this->client->getConfig('headers'); |
55
|
1 |
|
$headers['Content-Type'] = 'application/json'; |
56
|
1 |
|
$url = KintoneApi::generateUrl('file.json', $guestSpaceId); |
57
|
|
|
$requests = function () use ($fileKeys, $url, $headers) { |
58
|
1 |
|
foreach ($fileKeys as $fileKey) { |
59
|
1 |
|
$body = \GuzzleHttp\json_encode(['fileKey' => $fileKey]); |
60
|
1 |
|
yield new Request('GET', $url, $headers, $body); |
61
|
1 |
|
} |
62
|
1 |
|
}; |
63
|
1 |
|
$pool = new Pool($this->client, $requests(), [ |
64
|
1 |
|
'concurrency' => $concurrency ?: 1, |
65
|
|
|
'fulfilled' => function (ResponseInterface $response, $index) use (&$result) { |
66
|
1 |
|
$result[$index] = (string)$response->getBody(); |
67
|
1 |
|
} |
68
|
1 |
|
]); |
69
|
1 |
|
$pool->promise()->wait(); |
70
|
|
|
|
71
|
1 |
|
return $result; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Post file |
76
|
|
|
* https://cybozudev.zendesk.com/hc/ja/articles/201941824#step1 |
77
|
|
|
* |
78
|
|
|
* @param string $filename |
79
|
|
|
* @param int $guestSpaceId |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
1 |
|
public function post($filename, $guestSpaceId = null) |
83
|
|
|
{ |
84
|
1 |
|
$options = ['multipart' => [self::createMultipart($filename)]]; |
85
|
1 |
|
$this->changeLocale(); |
86
|
|
|
|
87
|
|
|
/** @var JsonStream $stream */ |
88
|
1 |
|
$stream = $this->client |
89
|
1 |
|
->post(KintoneApi::generateUrl('file.json', $guestSpaceId), $options) |
90
|
1 |
|
->getBody(); |
91
|
|
|
|
92
|
1 |
|
return $stream->jsonSerialize()['fileKey']; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param array $fileNames |
97
|
|
|
* @param int|null $guestSpaceId |
98
|
|
|
* @return array [fileKey, fileKey, ...] The order of $fileNames |
99
|
|
|
* @throws \InvalidArgumentException |
100
|
|
|
*/ |
101
|
1 |
|
public function multiPost(array $fileNames, $guestSpaceId = null) |
102
|
|
|
{ |
103
|
1 |
|
$this->changeLocale(); |
104
|
|
|
|
105
|
1 |
|
$result = []; |
106
|
1 |
|
$concurrency = $this->client->getConfig('concurrency'); |
107
|
1 |
|
$headers = $this->client->getConfig('headers'); |
108
|
1 |
|
$url = KintoneApi::generateUrl('file.json', $guestSpaceId); |
109
|
|
|
$requests = function () use ($fileNames, $url, $headers) { |
110
|
1 |
|
foreach ($fileNames as $filename) { |
111
|
1 |
|
$body = new MultipartStream([self::createMultipart($filename)]); |
112
|
1 |
|
yield new Request('POST', $url, $headers, $body); |
113
|
1 |
|
} |
114
|
1 |
|
}; |
115
|
1 |
|
$pool = new Pool($this->client, $requests(), [ |
116
|
1 |
|
'concurrency' => $concurrency ?: 1, |
117
|
1 |
|
'fulfilled' => function (ResponseInterface $response, $index) use (&$result) { |
118
|
|
|
/** @var JsonStream $stream */ |
119
|
1 |
|
$stream = $response->getBody(); |
120
|
1 |
|
$result[$index] = $stream->jsonSerialize()['fileKey']; |
121
|
1 |
|
} |
122
|
1 |
|
]); |
123
|
1 |
|
$pool->promise()->wait(); |
124
|
1 |
|
ksort($result); |
125
|
|
|
|
126
|
1 |
|
return $result; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Returns locale independent base name of the given path. |
131
|
|
|
* |
132
|
|
|
* @param string $name The new file name |
133
|
|
|
* @return string containing |
134
|
|
|
*/ |
135
|
11 |
|
public static function getFilename($name) |
136
|
|
|
{ |
137
|
11 |
|
$originalName = str_replace('\\', '/', $name); |
138
|
11 |
|
$pos = strrpos($originalName, '/'); |
139
|
11 |
|
$originalName = false === $pos ? $originalName : substr($originalName, $pos + 1); |
140
|
|
|
|
141
|
11 |
|
return $originalName; |
142
|
|
|
} |
143
|
|
|
|
144
|
2 |
|
private function changeLocale() |
145
|
|
|
{ |
146
|
2 |
|
$baseUri = $this->client->getConfig('base_uri'); |
147
|
2 |
|
if (strpos($baseUri->getHost(), 'cybozu.com') > 0) { // Japanese kintone |
148
|
2 |
|
setlocale(LC_ALL, 'ja_JP.UTF-8'); |
149
|
2 |
|
} |
150
|
2 |
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param string $filename |
154
|
|
|
* @return array |
155
|
|
|
*/ |
156
|
2 |
|
private static function createMultipart($filename) |
157
|
|
|
{ |
158
|
|
|
return [ |
159
|
2 |
|
'name' => 'file', |
160
|
2 |
|
'filename' => self::getFilename($filename), |
161
|
2 |
|
'contents' => fopen($filename, 'rb'), |
162
|
2 |
|
'headers' => ['Content-Type' => mime_content_type($filename)] |
163
|
2 |
|
]; |
164
|
|
|
} |
165
|
|
|
} |