3
3
namespace PlatformCommunity \Flysystem \BunnyCDN ;
4
4
5
5
use Exception ;
6
+ use League \Flysystem \CalculateChecksumFromStream ;
7
+ use League \Flysystem \ChecksumProvider ;
6
8
use League \Flysystem \Config ;
7
9
use League \Flysystem \DirectoryAttributes ;
8
10
use League \Flysystem \DirectoryListing ;
21
23
use League \Flysystem \UnableToRetrieveMetadata ;
22
24
use League \Flysystem \UnableToSetVisibility ;
23
25
use League \Flysystem \UnableToWriteFile ;
26
+ use League \Flysystem \UrlGeneration \PublicUrlGenerator ;
24
27
use League \Flysystem \Visibility ;
25
28
use League \MimeTypeDetection \FinfoMimeTypeDetector ;
26
29
use RuntimeException ;
27
30
use TypeError ;
28
31
29
- class BunnyCDNAdapter implements FilesystemAdapter
32
+ class BunnyCDNAdapter implements FilesystemAdapter, PublicUrlGenerator, ChecksumProvider
30
33
{
34
+ use CalculateChecksumFromStream;
35
+
31
36
/**
32
37
* Pull Zone URL
33
38
*
@@ -40,21 +45,18 @@ class BunnyCDNAdapter implements FilesystemAdapter
40
45
*/
41
46
private BunnyCDNClient $ client ;
42
47
43
- /**
44
- * @var string
45
- */
46
- private string $ prefixPath ;
47
-
48
48
/**
49
49
* @param BunnyCDNClient $client
50
50
* @param string $pullzone_url
51
- * @param string $prefixPath
52
51
*/
53
- public function __construct (BunnyCDNClient $ client , string $ pullzone_url = '' , string $ prefixPath = '' )
52
+ public function __construct (BunnyCDNClient $ client , string $ pullzone_url = '' )
54
53
{
55
54
$ this ->client = $ client ;
56
55
$ this ->pullzone_url = $ pullzone_url ;
57
- $ this ->prefixPath = rtrim ($ prefixPath , '/ ' );
56
+
57
+ if (\func_num_args () > 2 && (string ) \func_get_arg (2 ) !== '' ) {
58
+ throw new \RuntimeException ('PrefixPath is no longer supported directly. Use PathPrefixedAdapter instead: https://flysystem.thephpleague.com/docs/adapter/path-prefixing/ ' );
59
+ }
58
60
}
59
61
60
62
/**
@@ -65,9 +67,6 @@ public function __construct(BunnyCDNClient $client, string $pullzone_url = '', s
65
67
*/
66
68
public function copy ($ source , $ destination , Config $ config ): void
67
69
{
68
- $ source = $ this ->prependPrefix ($ source );
69
- $ destination = $ this ->prependPrefix ($ destination );
70
-
71
70
try {
72
71
$ this ->write ($ destination , $ this ->read ($ source ), new Config ());
73
72
// @codeCoverageIgnoreStart
@@ -84,8 +83,6 @@ public function copy($source, $destination, Config $config): void
84
83
*/
85
84
public function write ($ path , $ contents , Config $ config ): void
86
85
{
87
- $ path = $ this ->prependPrefix ($ path );
88
-
89
86
try {
90
87
$ this ->client ->upload ($ path , $ contents );
91
88
// @codeCoverageIgnoreStart
@@ -101,8 +98,6 @@ public function write($path, $contents, Config $config): void
101
98
*/
102
99
public function read ($ path ): string
103
100
{
104
- $ path = $ this ->prependPrefix ($ path );
105
-
106
101
try {
107
102
return $ this ->client ->download ($ path );
108
103
// @codeCoverageIgnoreStart
@@ -117,10 +112,8 @@ public function read($path): string
117
112
* @param bool $deep
118
113
* @return iterable
119
114
*/
120
- public function listContents (string $ path = '' , bool $ deep = false ): iterable
115
+ public function listContents (string $ path , bool $ deep ): iterable
121
116
{
122
- $ path = $ this ->prependPrefix ($ path );
123
-
124
117
try {
125
118
$ entries = $ this ->client ->list ($ path );
126
119
// @codeCoverageIgnoreStart
@@ -147,8 +140,6 @@ public function listContents(string $path = '', bool $deep = false): iterable
147
140
*/
148
141
protected function normalizeObject (array $ bunny_file_array ): StorageAttributes
149
142
{
150
- $ bunny_file_array ['Path ' ] = $ this ->replaceFirst ($ this ->prependPrefix ('' ), '' , $ bunny_file_array ['Path ' ]);
151
-
152
143
return match ($ bunny_file_array ['IsDirectory ' ]) {
153
144
true => new DirectoryAttributes (
154
145
Util::normalizePath (
@@ -206,8 +197,6 @@ private function extractExtraMetadata(array $bunny_file_array): array
206
197
*/
207
198
public function detectMimeType (string $ path ): string
208
199
{
209
- $ path = $ this ->prependPrefix ($ path );
210
-
211
200
try {
212
201
$ detector = new FinfoMimeTypeDetector ();
213
202
$ mimeType = $ detector ->detectMimeTypeFromPath ($ path );
@@ -230,8 +219,6 @@ public function detectMimeType(string $path): string
230
219
*/
231
220
public function writeStream ($ path , $ contents , Config $ config ): void
232
221
{
233
- $ path = $ this ->prependPrefix ($ path );
234
-
235
222
$ this ->write ($ path , stream_get_contents ($ contents ), $ config );
236
223
}
237
224
@@ -243,8 +230,6 @@ public function writeStream($path, $contents, Config $config): void
243
230
*/
244
231
public function readStream ($ path )
245
232
{
246
- $ path = $ this ->prependPrefix ($ path );
247
-
248
233
try {
249
234
return $ this ->client ->stream ($ path );
250
235
// @codeCoverageIgnoreStart
@@ -260,8 +245,6 @@ public function readStream($path)
260
245
*/
261
246
public function deleteDirectory (string $ path ): void
262
247
{
263
- $ path = $ this ->prependPrefix ($ path );
264
-
265
248
try {
266
249
$ this ->client ->delete (
267
250
rtrim ($ path , '/ ' ).'/ '
@@ -279,8 +262,6 @@ public function deleteDirectory(string $path): void
279
262
*/
280
263
public function createDirectory (string $ path , Config $ config ): void
281
264
{
282
- $ path = $ this ->prependPrefix ($ path );
283
-
284
265
try {
285
266
$ this ->client ->make_directory ($ path );
286
267
// @codeCoverageIgnoreStart
@@ -300,8 +281,6 @@ public function createDirectory(string $path, Config $config): void
300
281
*/
301
282
public function setVisibility (string $ path , string $ visibility ): void
302
283
{
303
- $ path = $ this ->prependPrefix ($ path );
304
-
305
284
throw UnableToSetVisibility::atLocation ($ path , 'BunnyCDN does not support visibility ' );
306
285
}
307
286
@@ -310,8 +289,6 @@ public function setVisibility(string $path, string $visibility): void
310
289
*/
311
290
public function visibility (string $ path ): FileAttributes
312
291
{
313
- $ path = $ this ->prependPrefix ($ path );
314
-
315
292
try {
316
293
return new FileAttributes ($ this ->getObject ($ path )->path (), null , $ this ->pullzone_url ? 'public ' : 'private ' );
317
294
} catch (UnableToReadFile |TypeError $ e ) {
@@ -327,8 +304,6 @@ public function visibility(string $path): FileAttributes
327
304
*/
328
305
public function mimeType (string $ path ): FileAttributes
329
306
{
330
- $ path = $ this ->prependPrefix ($ path );
331
-
332
307
try {
333
308
$ object = $ this ->getObject ($ path );
334
309
@@ -368,11 +343,9 @@ public function mimeType(string $path): FileAttributes
368
343
protected function getObject (string $ path = '' ): StorageAttributes
369
344
{
370
345
$ directory = pathinfo ($ path , PATHINFO_DIRNAME );
371
- $ list = (new DirectoryListing ($ this ->listContents ($ directory )))
346
+ $ list = (new DirectoryListing ($ this ->listContents ($ directory, false )))
372
347
->filter (function (StorageAttributes $ item ) use ($ path ) {
373
- $ itemPath = $ this ->prependPrefix (Util::normalizePath ($ item ->path ()));
374
-
375
- return $ itemPath === $ path ;
348
+ return Util::normalizePath ($ item ->path ()) === $ path ;
376
349
})->toArray ();
377
350
378
351
if (count ($ list ) === 1 ) {
@@ -394,8 +367,6 @@ protected function getObject(string $path = ''): StorageAttributes
394
367
*/
395
368
public function lastModified (string $ path ): FileAttributes
396
369
{
397
- $ path = $ this ->prependPrefix ($ path );
398
-
399
370
try {
400
371
return $ this ->getObject ($ path );
401
372
} catch (UnableToReadFile $ e ) {
@@ -411,8 +382,6 @@ public function lastModified(string $path): FileAttributes
411
382
*/
412
383
public function fileSize (string $ path ): FileAttributes
413
384
{
414
- $ path = $ this ->prependPrefix ($ path );
415
-
416
385
try {
417
386
return $ this ->getObject ($ path );
418
387
} catch (UnableToReadFile $ e ) {
@@ -428,9 +397,6 @@ public function fileSize(string $path): FileAttributes
428
397
*/
429
398
public function move (string $ source , string $ destination , Config $ config ): void
430
399
{
431
- $ source = $ this ->prependPrefix ($ source );
432
- $ destination = $ this ->prependPrefix ($ destination );
433
-
434
400
try {
435
401
$ this ->write ($ destination , $ this ->read ($ source ), new Config ());
436
402
$ this ->delete ($ source );
@@ -445,8 +411,6 @@ public function move(string $source, string $destination, Config $config): void
445
411
*/
446
412
public function delete ($ path ): void
447
413
{
448
- $ path = $ this ->prependPrefix ($ path );
449
-
450
414
try {
451
415
$ this ->client ->delete ($ path );
452
416
// @codeCoverageIgnoreStart
@@ -472,30 +436,37 @@ public function directoryExists(string $path): bool
472
436
*/
473
437
public function fileExists (string $ path ): bool
474
438
{
475
- $ path = $ this ->prependPrefix ($ path );
476
-
477
439
$ list = new DirectoryListing ($ this ->listContents (
478
- Util::splitPathIntoDirectoryAndFile ($ path )['dir ' ]
440
+ Util::splitPathIntoDirectoryAndFile ($ path )['dir ' ],
441
+ false
479
442
));
480
443
481
444
$ count = $ list ->filter (function (StorageAttributes $ item ) use ($ path ) {
482
- $ itemPath = $ this ->prependPrefix (Util::normalizePath ($ item ->path ()));
483
-
484
- return $ itemPath === Util::normalizePath ($ path );
445
+ return Util::normalizePath ($ item ->path ()) === Util::normalizePath ($ path );
485
446
})->toArray ();
486
447
487
448
return (bool ) count ($ count );
488
449
}
489
450
490
451
/**
491
- * getURL method for Laravel users who want to use BunnyCDN's PullZone to retrieve a public URL
452
+ * @deprecated use publicUrl instead
492
453
*
493
454
* @param string $path
494
455
* @return string
495
456
* @codeCoverageIgnore
496
457
* @noinspection PhpUnused
497
458
*/
498
459
public function getUrl (string $ path ): string
460
+ {
461
+ return $ this ->publicUrl ($ path , new Config ());
462
+ }
463
+
464
+ /**
465
+ * @param string $path
466
+ * @param Config $config
467
+ * @return string
468
+ */
469
+ public function publicUrl (string $ path , Config $ config ): string
499
470
{
500
471
if ($ this ->pullzone_url === '' ) {
501
472
throw new RuntimeException ('In order to get a visible URL for a BunnyCDN object, you must pass the "pullzone_url" parameter to the BunnyCDNAdapter. ' );
@@ -509,31 +480,19 @@ private static function parse_bunny_timestamp(string $timestamp): int
509
480
return (date_create_from_format ('Y-m-d\TH:i:s.u ' , $ timestamp ) ?: date_create_from_format ('Y-m-d\TH:i:s ' , $ timestamp ))->getTimestamp ();
510
481
}
511
482
512
- private function prependPrefix (string $ path ): string
513
- {
514
- if ($ this ->prefixPath === '' ) {
515
- return $ path ;
516
- }
517
-
518
- if ($ path === $ this ->prefixPath ) {
519
- return $ path ;
520
- }
521
-
522
- if (\str_starts_with ($ path , $ this ->prefixPath .'/ ' )) {
523
- return $ path ;
524
- }
525
-
526
- return $ this ->prefixPath .'/ ' .$ path ;
527
- }
528
-
529
- private function replaceFirst ($ search , $ replace , $ subject )
483
+ private function replaceFirst (string $ search , string $ replace , string $ subject ): string
530
484
{
531
485
$ position = strpos ($ subject , $ search );
532
486
533
487
if ($ position !== false ) {
534
- return substr_replace ($ subject , $ replace , $ position , strlen ($ search ));
488
+ return ( string ) substr_replace ($ subject , $ replace , $ position , strlen ($ search ));
535
489
}
536
490
537
491
return $ subject ;
538
492
}
493
+
494
+ public function checksum (string $ path , Config $ config ): string
495
+ {
496
+ return $ this ->calculateChecksumFromStream ($ path , $ config );
497
+ }
539
498
}
0 commit comments