12
12
use Illuminate \Http \Client \RequestException ;
13
13
use Illuminate \Support \Arr ;
14
14
use Illuminate \Support \Facades \Http ;
15
+ use Illuminate \Support \Str ;
15
16
use League \Flysystem \Adapter \AbstractAdapter ;
16
17
use League \Flysystem \Adapter \Polyfill \NotSupportingVisibilityTrait ;
17
18
use League \Flysystem \Config ;
@@ -353,10 +354,24 @@ public function listContents($directory = '', $recursive = false): array
353
354
$ options = [
354
355
'type ' => 'upload ' ,
355
356
'prefix ' => $ directory ,
357
+ 'max_results ' => 500 ,
356
358
];
357
359
358
360
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
360
375
->cloudinary
361
376
->adminApi ()
362
377
->assets ($ options );
@@ -369,22 +384,39 @@ public function listContents($directory = '', $recursive = false): array
369
384
return [];
370
385
}
371
386
372
- event (new FlysystemCloudinaryResponseLog ($ responseFiles ));
387
+ event (new FlysystemCloudinaryResponseLog ($ responseRawFiles ));
388
+ event (new FlysystemCloudinaryResponseLog ($ responseImageFiles ));
389
+ event (new FlysystemCloudinaryResponseLog ($ responseVideoFiles ));
373
390
event (new FlysystemCloudinaryResponseLog ($ responseDirectories ));
374
391
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 ) {
376
401
return $ this ->normalizeResponse ($ resource , $ resource ['public_id ' ]);
377
- }, $ responseFiles ->getArrayCopy ()['resources ' ]);
402
+ }, $ responseVideoFiles ->getArrayCopy ()['resources ' ]);
378
403
379
404
$ folders = array_map (function (array $ resource ) {
405
+ $ path = $ this ->ensurePrefixedFolderIsRemoved ($ resource ['path ' ]);
406
+
380
407
return [
381
408
'type ' => 'dir ' ,
382
- 'path ' => $ resource [ ' path ' ] ,
409
+ 'path ' => $ path ,
383
410
'name ' => $ resource ['name ' ],
384
411
];
385
412
}, $ responseDirectories ->getArrayCopy ()['folders ' ]);
386
413
387
- return [...$ files , ...$ folders ];
414
+ return [
415
+ ...$ rawFiles ,
416
+ ...$ imageFiles ,
417
+ ...$ videoFiles ,
418
+ ...$ folders ,
419
+ ];
388
420
}
389
421
390
422
/**
@@ -468,6 +500,19 @@ protected function ensureFolderIsPrefixed(string $path): string
468
500
return $ path ;
469
501
}
470
502
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
+
471
516
/**
472
517
* Normalize the object result array.
473
518
*
@@ -480,6 +525,8 @@ protected function normalizeResponse(
480
525
string $ path ,
481
526
$ body = null ,
482
527
): array {
528
+ $ path = $ this ->ensurePrefixedFolderIsRemoved ($ path );
529
+
483
530
return [
484
531
'contents ' => $ body ,
485
532
'etag ' => Arr::get ($ response , 'etag ' ),
0 commit comments