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