@@ -39,14 +39,21 @@ class BunnyCDNAdapter implements FilesystemAdapter
39
39
*/
40
40
private BunnyCDNClient $ client ;
41
41
42
+ /**
43
+ * @var string
44
+ */
45
+ private string $ prefixPath ;
46
+
42
47
/**
43
48
* @param BunnyCDNClient $client
44
49
* @param string $pullzone_url
50
+ * @param string $prefixPath
45
51
*/
46
- public function __construct (BunnyCDNClient $ client , string $ pullzone_url = '' )
52
+ public function __construct (BunnyCDNClient $ client , string $ pullzone_url = '' , string $ prefixPath = '' )
47
53
{
48
54
$ this ->client = $ client ;
49
55
$ this ->pullzone_url = $ pullzone_url ;
56
+ $ this ->prefixPath = rtrim ($ prefixPath , '/ ' );
50
57
}
51
58
52
59
/**
@@ -57,6 +64,9 @@ public function __construct(BunnyCDNClient $client, string $pullzone_url = '')
57
64
*/
58
65
public function copy ($ source , $ destination , Config $ config ): void
59
66
{
67
+ $ this ->prependPrefix ($ source );
68
+ $ this ->prependPrefix ($ destination );
69
+
60
70
try {
61
71
$ this ->write ($ destination , $ this ->read ($ source ), new Config ());
62
72
// @codeCoverageIgnoreStart
@@ -73,6 +83,8 @@ public function copy($source, $destination, Config $config): void
73
83
*/
74
84
public function write ($ path , $ contents , Config $ config ): void
75
85
{
86
+ $ this ->prependPrefix ($ path );
87
+
76
88
try {
77
89
$ this ->client ->upload ($ path , $ contents );
78
90
// @codeCoverageIgnoreStart
@@ -88,6 +100,8 @@ public function write($path, $contents, Config $config): void
88
100
*/
89
101
public function read ($ path ): string
90
102
{
103
+ $ this ->prependPrefix ($ path );
104
+
91
105
try {
92
106
return $ this ->client ->download ($ path );
93
107
// @codeCoverageIgnoreStart
@@ -104,6 +118,8 @@ public function read($path): string
104
118
*/
105
119
public function listContents (string $ path = '' , bool $ deep = false ): iterable
106
120
{
121
+ $ this ->prependPrefix ($ path );
122
+
107
123
try {
108
124
$ entries = $ this ->client ->list ($ path );
109
125
// @codeCoverageIgnoreStart
@@ -190,6 +206,8 @@ private function extractExtraMetadata(array $bunny_file_array): array
190
206
*/
191
207
public function detectMimeType (string $ path ): string
192
208
{
209
+ $ this ->prependPrefix ($ path );
210
+
193
211
try {
194
212
$ detector = new FinfoMimeTypeDetector ();
195
213
$ mimeType = $ detector ->detectMimeTypeFromPath ($ path );
@@ -212,6 +230,8 @@ public function detectMimeType(string $path): string
212
230
*/
213
231
public function writeStream ($ path , $ contents , Config $ config ): void
214
232
{
233
+ $ this ->prependPrefix ($ path );
234
+
215
235
$ this ->write ($ path , stream_get_contents ($ contents ), $ config );
216
236
}
217
237
@@ -233,6 +253,8 @@ public function readStream($path)
233
253
*/
234
254
public function deleteDirectory (string $ path ): void
235
255
{
256
+ $ this ->prependPrefix ($ path );
257
+
236
258
try {
237
259
$ this ->client ->delete (
238
260
rtrim ($ path , '/ ' ).'/ '
@@ -250,6 +272,8 @@ public function deleteDirectory(string $path): void
250
272
*/
251
273
public function createDirectory (string $ path , Config $ config ): void
252
274
{
275
+ $ this ->prependPrefix ($ path );
276
+
253
277
try {
254
278
$ this ->client ->make_directory ($ path );
255
279
// @codeCoverageIgnoreStart
@@ -269,6 +293,8 @@ public function createDirectory(string $path, Config $config): void
269
293
*/
270
294
public function setVisibility (string $ path , string $ visibility ): void
271
295
{
296
+ $ this ->prependPrefix ($ path );
297
+
272
298
throw UnableToSetVisibility::atLocation ($ path , 'BunnyCDN does not support visibility ' );
273
299
}
274
300
@@ -277,6 +303,8 @@ public function setVisibility(string $path, string $visibility): void
277
303
*/
278
304
public function visibility (string $ path ): FileAttributes
279
305
{
306
+ $ this ->prependPrefix ($ path );
307
+
280
308
try {
281
309
return new FileAttributes ($ this ->getObject ($ path )->path (), null , $ this ->pullzone_url ? 'public ' : 'private ' );
282
310
} catch (UnableToReadFile $ e ) {
@@ -296,6 +324,8 @@ public function visibility(string $path): FileAttributes
296
324
*/
297
325
public function mimeType (string $ path ): FileAttributes
298
326
{
327
+ $ this ->prependPrefix ($ path );
328
+
299
329
try {
300
330
$ object = $ this ->getObject ($ path );
301
331
@@ -334,6 +364,8 @@ public function mimeType(string $path): FileAttributes
334
364
*/
335
365
protected function getObject (string $ path = '' ): StorageAttributes
336
366
{
367
+ $ this ->prependPrefix ($ path );
368
+
337
369
$ directory = pathinfo ($ path , PATHINFO_DIRNAME );
338
370
$ list = (new DirectoryListing ($ this ->listContents ($ directory )))
339
371
->filter (function (StorageAttributes $ item ) use ($ path ) {
@@ -357,6 +389,8 @@ protected function getObject(string $path = ''): StorageAttributes
357
389
*/
358
390
public function lastModified (string $ path ): FileAttributes
359
391
{
392
+ $ this ->prependPrefix ($ path );
393
+
360
394
try {
361
395
return $ this ->getObject ($ path );
362
396
} catch (UnableToReadFile $ e ) {
@@ -372,6 +406,8 @@ public function lastModified(string $path): FileAttributes
372
406
*/
373
407
public function fileSize (string $ path ): FileAttributes
374
408
{
409
+ $ this ->prependPrefix ($ path );
410
+
375
411
try {
376
412
return $ this ->getObject ($ path );
377
413
} catch (UnableToReadFile $ e ) {
@@ -387,6 +423,9 @@ public function fileSize(string $path): FileAttributes
387
423
*/
388
424
public function move (string $ source , string $ destination , Config $ config ): void
389
425
{
426
+ $ this ->prependPrefix ($ source );
427
+ $ this ->prependPrefix ($ destination );
428
+
390
429
try {
391
430
$ this ->write ($ destination , $ this ->read ($ source ), new Config ());
392
431
$ this ->delete ($ source );
@@ -401,6 +440,8 @@ public function move(string $source, string $destination, Config $config): void
401
440
*/
402
441
public function delete ($ path ): void
403
442
{
443
+ $ this ->prependPrefix ($ path );
444
+
404
445
try {
405
446
$ this ->client ->delete ($ path );
406
447
// @codeCoverageIgnoreStart
@@ -426,6 +467,8 @@ public function directoryExists(string $path): bool
426
467
*/
427
468
public function fileExists (string $ path ): bool
428
469
{
470
+ $ this ->prependPrefix ($ path );
471
+
429
472
$ list = new DirectoryListing ($ this ->listContents (
430
473
Util::splitPathIntoDirectoryAndFile ($ path )['dir ' ]
431
474
));
@@ -457,4 +500,21 @@ private static function parse_bunny_timestamp(string $timestamp): int
457
500
{
458
501
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 ();
459
502
}
503
+
504
+ private function prependPrefix (string &$ path ): void
505
+ {
506
+ if ($ this ->prefixPath === '' ) {
507
+ return ;
508
+ }
509
+
510
+ if ($ path === $ this ->prefixPath ) {
511
+ return ;
512
+ }
513
+
514
+ if (\str_starts_with ($ path , $ this ->prefixPath .'/ ' )) {
515
+ return ;
516
+ }
517
+
518
+ $ path = $ this ->prefixPath .'/ ' .$ path ;
519
+ }
460
520
}
0 commit comments