Skip to content

Commit 4da2f89

Browse files
committed
🐛 List folders
1 parent a7971af commit 4da2f89

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All notable changes to `laravel-cloudinary` will be documented in this file.
44

5+
## 1.0.1 - 2021-06-23
6+
7+
- 🐛 Bug Fix: List folders
8+
59
## 1.0.0 - 2021-06-03
610

711
- Stable release

src/FlysystemCloudinaryAdapter.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -356,19 +356,35 @@ public function listContents($directory = '', $recursive = false): array
356356
];
357357

358358
try {
359-
$response = $this
359+
$responseFiles = $this
360360
->cloudinary
361361
->adminApi()
362362
->assets($options);
363-
} catch (RateLimited) {
363+
364+
$responseDirectories = $this
365+
->cloudinary
366+
->adminApi()
367+
->subFolders($directory);
368+
} catch (RateLimited | ApiError) {
364369
return [];
365370
}
366371

367-
event(new FlysystemCloudinaryResponseLog($response));
372+
event(new FlysystemCloudinaryResponseLog($responseFiles));
373+
event(new FlysystemCloudinaryResponseLog($responseDirectories));
368374

369-
return array_map(function (array $resource) {
375+
$files = array_map(function (array $resource) {
370376
return $this->normalizeResponse($resource, $resource['public_id']);
371-
}, $response->getArrayCopy()['resources']);
377+
}, $responseFiles->getArrayCopy()['resources']);
378+
379+
$folders = array_map(function (array $resource) {
380+
return [
381+
'type' => 'dir',
382+
'path' => $resource['path'],
383+
'name' => $resource['name'],
384+
];
385+
}, $responseDirectories->getArrayCopy()['folders']);
386+
387+
return [...$files, ...$folders];
372388
}
373389

374390
/**

tests/Feature/AdapterTest.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,14 +210,17 @@ public function it_can_delete_a_directory()
210210
$mock->shouldReceive('adminApi->assets')->once()->andReturn(new ApiResponse([
211211
'resources' => [],
212212
], []));
213+
$mock->shouldReceive('adminApi->subFolders')->once()->andReturn(new ApiResponse([
214+
'folders' => [],
215+
], []));
213216
$mock->shouldReceive('adminApi->deleteFolder')->once()->andReturn(new ApiResponse([], []));
214217
});
215218
$adapter = new FlysystemCloudinaryAdapter($mock);
216219

217220
$bool = $adapter->deleteDir('::path::');
218221

219222
$this->assertTrue($bool);
220-
Event::assertDispatched(FlysystemCloudinaryResponseLog::class, 2);
223+
Event::assertDispatched(FlysystemCloudinaryResponseLog::class, 3);
221224
}
222225

223226
/** @test */
@@ -294,13 +297,17 @@ public function it_can_list_directory_contents()
294297
$mock->shouldReceive('adminApi->assets')->once()->andReturn(new ApiResponse([
295298
'resources' => [],
296299
], []));
300+
301+
$mock->shouldReceive('adminApi->subFolders')->once()->andReturn(new ApiResponse([
302+
'folders' => [],
303+
], []));
297304
});
298305
$adapter = new FlysystemCloudinaryAdapter($mock);
299306

300307
$files = $adapter->listContents('::path::');
301308

302309
$this->assertSame([], $files);
303-
Event::assertDispatched(FlysystemCloudinaryResponseLog::class, 1);
310+
Event::assertDispatched(FlysystemCloudinaryResponseLog::class, 2);
304311
}
305312

306313
/** @test */

0 commit comments

Comments
 (0)