Skip to content

Commit bf5d4a1

Browse files
committed
🐛 Handle folder prefix correct
1 parent 4da2f89 commit bf5d4a1

File tree

2 files changed

+57
-10
lines changed

2 files changed

+57
-10
lines changed

src/FlysystemCloudinaryAdapter.php

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Illuminate\Http\Client\RequestException;
1313
use Illuminate\Support\Arr;
1414
use Illuminate\Support\Facades\Http;
15+
use Illuminate\Support\Str;
1516
use League\Flysystem\Adapter\AbstractAdapter;
1617
use League\Flysystem\Adapter\Polyfill\NotSupportingVisibilityTrait;
1718
use League\Flysystem\Config;
@@ -353,10 +354,24 @@ public function listContents($directory = '', $recursive = false): array
353354
$options = [
354355
'type' => 'upload',
355356
'prefix' => $directory,
357+
'max_results' => 500,
356358
];
357359

358360
try {
359-
$responseFiles = $this
361+
$options['resource_type'] = 'raw';
362+
$responseRawFiles = $this
363+
->cloudinary
364+
->adminApi()
365+
->assets($options);
366+
367+
$options['resource_type'] = 'image';
368+
$responseImageFiles = $this
369+
->cloudinary
370+
->adminApi()
371+
->assets($options);
372+
373+
$options['resource_type'] = 'video';
374+
$responseVideoFiles = $this
360375
->cloudinary
361376
->adminApi()
362377
->assets($options);
@@ -369,22 +384,39 @@ public function listContents($directory = '', $recursive = false): array
369384
return [];
370385
}
371386

372-
event(new FlysystemCloudinaryResponseLog($responseFiles));
387+
event(new FlysystemCloudinaryResponseLog($responseRawFiles));
388+
event(new FlysystemCloudinaryResponseLog($responseImageFiles));
389+
event(new FlysystemCloudinaryResponseLog($responseVideoFiles));
373390
event(new FlysystemCloudinaryResponseLog($responseDirectories));
374391

375-
$files = array_map(function (array $resource) {
392+
$rawFiles = array_map(function (array $resource) {
393+
return $this->normalizeResponse($resource, $resource['public_id']);
394+
}, $responseRawFiles->getArrayCopy()['resources']);
395+
396+
$imageFiles = array_map(function (array $resource) {
397+
return $this->normalizeResponse($resource, $resource['public_id']);
398+
}, $responseImageFiles->getArrayCopy()['resources']);
399+
400+
$videoFiles = array_map(function (array $resource) {
376401
return $this->normalizeResponse($resource, $resource['public_id']);
377-
}, $responseFiles->getArrayCopy()['resources']);
402+
}, $responseVideoFiles->getArrayCopy()['resources']);
378403

379404
$folders = array_map(function (array $resource) {
405+
$path = $this->ensurePrefixedFolderIsRemoved($resource['path']);
406+
380407
return [
381408
'type' => 'dir',
382-
'path' => $resource['path'],
409+
'path' => $path,
383410
'name' => $resource['name'],
384411
];
385412
}, $responseDirectories->getArrayCopy()['folders']);
386413

387-
return [...$files, ...$folders];
414+
return [
415+
...$rawFiles,
416+
...$imageFiles,
417+
...$videoFiles,
418+
...$folders,
419+
];
388420
}
389421

390422
/**
@@ -468,6 +500,19 @@ protected function ensureFolderIsPrefixed(string $path): string
468500
return $path;
469501
}
470502

503+
protected function ensurePrefixedFolderIsRemoved(string $path): string
504+
{
505+
if (config('flysystem-cloudinary.folder')) {
506+
$prefix = config('flysystem-cloudinary.folder') . '/';
507+
508+
return Str::of($path)
509+
->after($prefix)
510+
->__toString();
511+
}
512+
513+
return $path;
514+
}
515+
471516
/**
472517
* Normalize the object result array.
473518
*
@@ -480,6 +525,8 @@ protected function normalizeResponse(
480525
string $path,
481526
$body = null,
482527
): array {
528+
$path = $this->ensurePrefixedFolderIsRemoved($path);
529+
483530
return [
484531
'contents' => $body,
485532
'etag' => Arr::get($response, 'etag'),

tests/Feature/AdapterTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ public function it_can_delete()
207207
public function it_can_delete_a_directory()
208208
{
209209
$mock = $this->mock(Cloudinary::class, function (MockInterface $mock) {
210-
$mock->shouldReceive('adminApi->assets')->once()->andReturn(new ApiResponse([
210+
$mock->shouldReceive('adminApi->assets')->times(3)->andReturn(new ApiResponse([
211211
'resources' => [],
212212
], []));
213213
$mock->shouldReceive('adminApi->subFolders')->once()->andReturn(new ApiResponse([
@@ -220,7 +220,7 @@ public function it_can_delete_a_directory()
220220
$bool = $adapter->deleteDir('::path::');
221221

222222
$this->assertTrue($bool);
223-
Event::assertDispatched(FlysystemCloudinaryResponseLog::class, 3);
223+
Event::assertDispatched(FlysystemCloudinaryResponseLog::class, 5);
224224
}
225225

226226
/** @test */
@@ -294,7 +294,7 @@ public function it_can_read_stream()
294294
public function it_can_list_directory_contents()
295295
{
296296
$mock = $this->mock(Cloudinary::class, function (MockInterface $mock) {
297-
$mock->shouldReceive('adminApi->assets')->once()->andReturn(new ApiResponse([
297+
$mock->shouldReceive('adminApi->assets')->times(3)->andReturn(new ApiResponse([
298298
'resources' => [],
299299
], []));
300300

@@ -307,7 +307,7 @@ public function it_can_list_directory_contents()
307307
$files = $adapter->listContents('::path::');
308308

309309
$this->assertSame([], $files);
310-
Event::assertDispatched(FlysystemCloudinaryResponseLog::class, 2);
310+
Event::assertDispatched(FlysystemCloudinaryResponseLog::class, 4);
311311
}
312312

313313
/** @test */

0 commit comments

Comments
 (0)