diff --git a/src/Client.php b/src/Client.php index e174f6b..e9edd6a 100644 --- a/src/Client.php +++ b/src/Client.php @@ -70,16 +70,39 @@ public function readRaw(string $path): string /** * @param $path * - * @return mixed|string + * @return array * @throws \GuzzleHttp\Exception\GuzzleException */ public function read($path) { $path = urlencode($path); - - $response = $this->request('GET', "files/$path"); - - return $this->responseContents($response); + + $response = $this->request('HEAD', "files/$path"); + + $headers = $response->getHeaders(); + $headers = array_filter( + $headers, + fn($key) => substr($key, 0, 9) == 'X-Gitlab-', + ARRAY_FILTER_USE_KEY + ); + + $keys = array_keys($headers); + $values = array_values($headers); + + array_walk( + $keys, + function(&$key) { + $key = substr($key, 9); + $key = strtolower($key); + $key = preg_replace_callback( + '/[-_]+(.)?/i', + fn($matches) => strtoupper($matches[ 1 ]), + $key + ); + } + ); + + return array_combine($keys, $values); } /** @@ -254,12 +277,11 @@ public function setBranch(string $branch) */ private function request(string $method, string $uri, array $params = []): Response { - $uri = ($method === 'GET') ? $this->buildUri($uri, $params) : $this->buildUri($uri); - - $params = ($method !== 'GET') ? ['form_params' => array_merge(['branch' => $this->branch], $params)] : []; - + $uri = !in_array($method, ['POST', 'PUT', 'DELETE']) ? $this->buildUri($uri, $params) : $this->buildUri($uri); + $params = in_array($method, ['POST', 'PUT', 'DELETE']) ? ['form_params' => array_merge(['branch' => $this->branch], $params)] : []; + $client = new HttpClient(['headers' => ['PRIVATE-TOKEN' => $this->personalAccessToken]]); - + return $client->request($method, $uri, $params); } diff --git a/src/GitlabAdapter.php b/src/GitlabAdapter.php index 683e1ab..99d24f4 100644 --- a/src/GitlabAdapter.php +++ b/src/GitlabAdapter.php @@ -132,9 +132,7 @@ public function writeStream(string $path, $contents, Config $config): void public function read(string $path): string { try { - $response = $this->client->read($this->prefixer->prefixPath($path)); - - return base64_decode($response['content']); + return $this->client->readRaw($this->prefixer->prefixPath($path)); } catch (Throwable $e) { throw UnableToReadFile::fromLocation($path, $e->getMessage(), $e); } @@ -281,9 +279,9 @@ public function lastModified(string $path): FileAttributes public function fileSize(string $path): FileAttributes { try { - $response = $this->client->read($this->prefixer->prefixPath($path)); + $meta = $this->client->read($this->prefixer->prefixPath($path)); - return new FileAttributes($path, $response['size']); + return new FileAttributes($path, $meta['size'][0] ?? 0); } catch (Throwable $e) { throw UnableToRetrieveMetadata::fileSize($path, $e->getMessage(), $e); } diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 0f405fb..17a020b 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -35,10 +35,11 @@ public function it_can_be_instantiated() */ public function it_can_read_a_file() { - $file = $this->client->read('README.md'); + $meta = $this->client->read('README.md'); - $this->assertStringStartsWith(base64_encode('# Testing repo for `flysystem-gitlab`'), - $file[ 'content' ]); + $this->assertArrayHasKey('ref', $meta); + $this->assertArrayHasKey('size', $meta); + $this->assertArrayHasKey('lastCommitId', $meta); } /**