From ffef534d41bfe9c0d4e7ef123e9a92df619e9b04 Mon Sep 17 00:00:00 2001 From: Gabriel Ramos Date: Sat, 28 Sep 2024 12:25:00 -0300 Subject: [PATCH 1/2] fix prefix when listing blobs --- src/BlobStorage/Managers/Blob/BlobManager.php | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/BlobStorage/Managers/Blob/BlobManager.php b/src/BlobStorage/Managers/Blob/BlobManager.php index 545fe51..8a198a6 100644 --- a/src/BlobStorage/Managers/Blob/BlobManager.php +++ b/src/BlobStorage/Managers/Blob/BlobManager.php @@ -35,24 +35,38 @@ public function __construct( /** * @param array $options - * @param string[] $includes + * @param array{includes?: string[], prefix?: string} $queryParams */ - public function list(array $options = [], array $includes = []): Blobs + public function list(array $options = [], array $queryParams = []): Blobs { + /** @var string[] $includes */ + $includes = $queryParams['includes'] ?? []; + if (array_diff($includes, $availableOptions = BlobIncludeOption::toArray()) !== []) { throw InvalidArgumentException::create(sprintf("Invalid include option. \nValid options: %s", implode(', ', $availableOptions))); } - $include = ''; + $parameters = []; if (!empty($includes)) { - $include = sprintf('&include=%s', implode(',', $includes)); + $parameters[] = sprintf('include=%s', implode(',', $includes)); } + /** @var string $prefix */ + $prefix = rtrim($queryParams['prefix'] ?? '', '/') . '/'; + + if (trim($prefix) !== '/') { + $parameters[] = "prefix={$prefix}"; + } + + $queryString = count($parameters) >= 1 + ? ('&' . implode('&', $parameters)) + : ''; + try { $response = $this->request ->withOptions($options) - ->get("{$this->containerName}/?restype=container&comp=list{$include}") + ->get("{$this->containerName}/?restype=container&comp=list{$queryString}") ->getBody(); // @codeCoverageIgnoreStart From 643431f430e44a7041d86541ae7505e6d5239144 Mon Sep 17 00:00:00 2001 From: Gabriel Ramos Date: Sat, 28 Sep 2024 12:25:28 -0300 Subject: [PATCH 2/2] fix tests --- .../Managers/Blob/BlobStorageBlobManagerTest.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/Feature/BlobStorage/Managers/Blob/BlobStorageBlobManagerTest.php b/tests/Feature/BlobStorage/Managers/Blob/BlobStorageBlobManagerTest.php index 3d62d4d..2bd89ac 100644 --- a/tests/Feature/BlobStorage/Managers/Blob/BlobStorageBlobManagerTest.php +++ b/tests/Feature/BlobStorage/Managers/Blob/BlobStorageBlobManagerTest.php @@ -144,7 +144,10 @@ $request = (new RequestFake()) ->withFakeResponse(new ResponseFake($body)); - $result = (new BlobManager($request, $container = 'container'))->list(['option' => 'value'], includes: ['metadata', 'snapshots']); + $result = (new BlobManager($request, $container = 'container'))->list( + ['option' => 'value'], + ['includes' => ['metadata', 'snapshots'], 'prefix' => 'test'], + ); expect($result) ->toBeInstanceOf(Blobs::class) @@ -166,14 +169,14 @@ ->leaseState->toBe('available') ->serverEncrypted->toBe(true); - $request->assertGet("{$container}/?restype=container&comp=list&include=metadata,snapshots") + $request->assertGet("{$container}/?restype=container&comp=list&include=metadata,snapshots&prefix=test/") ->assertSentWithOptions(['option' => 'value']); }); it('should an exception if the BlobIncludeOption is invalid', function () { $blobManager = new BlobManager(new RequestFake(), 'container'); - $blobManager->list(includes: ['invalid']); + $blobManager->list(queryParams: ['includes' => ['invalid']]); })->throws(InvalidArgumentException::class); it('should find by tag', function () {