| 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\Exception\RequestException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use GuzzleHttp\Pool; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use GuzzleHttp\Psr7\MultipartStream; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use GuzzleHttp\Psr7\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Psr\Http\Message\ResponseInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @author ochi51 <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | class File | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @var Client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     private $client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 | 2 |  |     public function __construct(Client $client) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 2 |  |         $this->client = $client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * Get file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * https://cybozudev.zendesk.com/hc/ja/articles/202166180#step1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @param string $fileKey | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @param int $guestSpaceId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @return string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 38 | 1 |  |     public function get($fileKey, $guestSpaceId = null): string | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 40 | 1 |  |         $options = ['json' => ['fileKey' => $fileKey]]; | 
            
                                                                        
                            
            
                                    
            
            
                | 41 | 1 |  |         $response = $this->client->get(KintoneApi::generateUrl('file.json', $guestSpaceId), $options); | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 43 | 1 |  |         return (string)$response->getBody(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * Get file stream response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * https://cybozudev.zendesk.com/hc/ja/articles/202166180#step1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @param string $fileKey | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @param int $guestSpaceId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @return ResponseInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @throws RequestException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 2 |  |     public function getStreamResponse($fileKey, $guestSpaceId = null): ResponseInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 2 |  |         $options = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 2 |  |             'json' => ['fileKey' => $fileKey], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 2 |  |             'stream' => true | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 2 |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 2 |  |         return $this->client->get(KintoneApi::generateUrl('file.json', $guestSpaceId), $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 | 1 |  |         $requests = static 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 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 1 |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 1 |  |         $pool = new Pool($this->client, $requests(), [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 1 |  |             'concurrency' => $concurrency ?: 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 1 |  |             'fulfilled' => static 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): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 | 1 |  |         $requests = static 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 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 1 |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 1 |  |         $pool = new Pool($this->client, $requests(), [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 1 |  |             'concurrency' => $concurrency ?: 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 1 |  |             'fulfilled' => static 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): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |      * @param string $path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |      * @param string|null $filename | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 | 3 |  |     private static function createMultipart($path, $filename = null): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 | 3 |  |         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 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 187 |  |  |  | 
            
                        
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.