Skip to content

Commit 9bf9e09

Browse files
authored
Merge pull request #36 from ben182/master
fix: #35 several broken prefix paths
2 parents ed8d596 + 7cda890 commit 9bf9e09

File tree

2 files changed

+94
-28
lines changed

2 files changed

+94
-28
lines changed

src/BunnyCDNAdapter.php

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ public function __construct(BunnyCDNClient $client, string $pullzone_url = '', s
6565
*/
6666
public function copy($source, $destination, Config $config): void
6767
{
68-
$this->prependPrefix($source);
69-
$this->prependPrefix($destination);
68+
$source = $this->prependPrefix($source);
69+
$destination = $this->prependPrefix($destination);
7070

7171
try {
7272
$this->write($destination, $this->read($source), new Config());
@@ -84,7 +84,7 @@ public function copy($source, $destination, Config $config): void
8484
*/
8585
public function write($path, $contents, Config $config): void
8686
{
87-
$this->prependPrefix($path);
87+
$path = $this->prependPrefix($path);
8888

8989
try {
9090
$this->client->upload($path, $contents);
@@ -101,7 +101,7 @@ public function write($path, $contents, Config $config): void
101101
*/
102102
public function read($path): string
103103
{
104-
$this->prependPrefix($path);
104+
$path = $this->prependPrefix($path);
105105

106106
try {
107107
return $this->client->download($path);
@@ -119,7 +119,7 @@ public function read($path): string
119119
*/
120120
public function listContents(string $path = '', bool $deep = false): iterable
121121
{
122-
$this->prependPrefix($path);
122+
$path = $this->prependPrefix($path);
123123

124124
try {
125125
$entries = $this->client->list($path);
@@ -147,6 +147,8 @@ public function listContents(string $path = '', bool $deep = false): iterable
147147
*/
148148
protected function normalizeObject(array $bunny_file_array): StorageAttributes
149149
{
150+
$bunny_file_array['Path'] = str_replace($this->prependPrefix(''), '', $bunny_file_array['Path']);
151+
150152
return match ($bunny_file_array['IsDirectory']) {
151153
true => new DirectoryAttributes(
152154
Util::normalizePath(
@@ -204,7 +206,7 @@ private function extractExtraMetadata(array $bunny_file_array): array
204206
*/
205207
public function detectMimeType(string $path): string
206208
{
207-
$this->prependPrefix($path);
209+
$path = $this->prependPrefix($path);
208210

209211
try {
210212
$detector = new FinfoMimeTypeDetector();
@@ -228,7 +230,7 @@ public function detectMimeType(string $path): string
228230
*/
229231
public function writeStream($path, $contents, Config $config): void
230232
{
231-
$this->prependPrefix($path);
233+
$path = $this->prependPrefix($path);
232234

233235
$this->write($path, stream_get_contents($contents), $config);
234236
}
@@ -241,6 +243,8 @@ public function writeStream($path, $contents, Config $config): void
241243
*/
242244
public function readStream($path)
243245
{
246+
$path = $this->prependPrefix($path);
247+
244248
try {
245249
return $this->client->stream($path);
246250
// @codeCoverageIgnoreStart
@@ -256,7 +260,7 @@ public function readStream($path)
256260
*/
257261
public function deleteDirectory(string $path): void
258262
{
259-
$this->prependPrefix($path);
263+
$path = $this->prependPrefix($path);
260264

261265
try {
262266
$this->client->delete(
@@ -275,7 +279,7 @@ public function deleteDirectory(string $path): void
275279
*/
276280
public function createDirectory(string $path, Config $config): void
277281
{
278-
$this->prependPrefix($path);
282+
$path = $this->prependPrefix($path);
279283

280284
try {
281285
$this->client->make_directory($path);
@@ -296,7 +300,7 @@ public function createDirectory(string $path, Config $config): void
296300
*/
297301
public function setVisibility(string $path, string $visibility): void
298302
{
299-
$this->prependPrefix($path);
303+
$path = $this->prependPrefix($path);
300304

301305
throw UnableToSetVisibility::atLocation($path, 'BunnyCDN does not support visibility');
302306
}
@@ -306,7 +310,7 @@ public function setVisibility(string $path, string $visibility): void
306310
*/
307311
public function visibility(string $path): FileAttributes
308312
{
309-
$this->prependPrefix($path);
313+
$path = $this->prependPrefix($path);
310314

311315
try {
312316
return new FileAttributes($this->getObject($path)->path(), null, $this->pullzone_url ? 'public' : 'private');
@@ -323,7 +327,7 @@ public function visibility(string $path): FileAttributes
323327
*/
324328
public function mimeType(string $path): FileAttributes
325329
{
326-
$this->prependPrefix($path);
330+
$path = $this->prependPrefix($path);
327331

328332
try {
329333
$object = $this->getObject($path);
@@ -363,12 +367,12 @@ public function mimeType(string $path): FileAttributes
363367
*/
364368
protected function getObject(string $path = ''): StorageAttributes
365369
{
366-
$this->prependPrefix($path);
367-
368370
$directory = pathinfo($path, PATHINFO_DIRNAME);
369371
$list = (new DirectoryListing($this->listContents($directory)))
370372
->filter(function (StorageAttributes $item) use ($path) {
371-
return $item->path() === $path;
373+
$itemPath = $this->prependPrefix(Util::normalizePath($item->path()));
374+
375+
return $itemPath === $path;
372376
})->toArray();
373377

374378
if (count($list) === 1) {
@@ -390,7 +394,7 @@ protected function getObject(string $path = ''): StorageAttributes
390394
*/
391395
public function lastModified(string $path): FileAttributes
392396
{
393-
$this->prependPrefix($path);
397+
$path = $this->prependPrefix($path);
394398

395399
try {
396400
return $this->getObject($path);
@@ -407,7 +411,7 @@ public function lastModified(string $path): FileAttributes
407411
*/
408412
public function fileSize(string $path): FileAttributes
409413
{
410-
$this->prependPrefix($path);
414+
$path = $this->prependPrefix($path);
411415

412416
try {
413417
return $this->getObject($path);
@@ -424,8 +428,8 @@ public function fileSize(string $path): FileAttributes
424428
*/
425429
public function move(string $source, string $destination, Config $config): void
426430
{
427-
$this->prependPrefix($source);
428-
$this->prependPrefix($destination);
431+
$source = $this->prependPrefix($source);
432+
$destination = $this->prependPrefix($destination);
429433

430434
try {
431435
$this->write($destination, $this->read($source), new Config());
@@ -441,7 +445,7 @@ public function move(string $source, string $destination, Config $config): void
441445
*/
442446
public function delete($path): void
443447
{
444-
$this->prependPrefix($path);
448+
$path = $this->prependPrefix($path);
445449

446450
try {
447451
$this->client->delete($path);
@@ -468,14 +472,16 @@ public function directoryExists(string $path): bool
468472
*/
469473
public function fileExists(string $path): bool
470474
{
471-
$this->prependPrefix($path);
475+
$path = $this->prependPrefix($path);
472476

473477
$list = new DirectoryListing($this->listContents(
474478
Util::splitPathIntoDirectoryAndFile($path)['dir']
475479
));
476480

477481
$count = $list->filter(function (StorageAttributes $item) use ($path) {
478-
return Util::normalizePath($item->path()) === Util::normalizePath($path);
482+
$itemPath = $this->prependPrefix(Util::normalizePath($item->path()));
483+
484+
return $itemPath === Util::normalizePath($path);
479485
})->toArray();
480486

481487
return (bool) count($count);
@@ -503,20 +509,20 @@ private static function parse_bunny_timestamp(string $timestamp): int
503509
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();
504510
}
505511

506-
private function prependPrefix(string &$path): void
512+
private function prependPrefix(string $path): string
507513
{
508514
if ($this->prefixPath === '') {
509-
return;
515+
return $path;
510516
}
511517

512518
if ($path === $this->prefixPath) {
513-
return;
519+
return $path;
514520
}
515521

516522
if (\str_starts_with($path, $this->prefixPath.'/')) {
517-
return;
523+
return $path;
518524
}
519525

520-
$path = $this->prefixPath.'/'.$path;
526+
return $this->prefixPath.'/'.$path;
521527
}
522528
}

tests/FlysystemTestSuite.php

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace PlatformCommunity\Flysystem\BunnyCDN\Tests;
44

5-
use Faker\Provider\File;
65
use GuzzleHttp\Psr7\Response;
76
use League\Flysystem\AdapterTestUtilities\FilesystemAdapterTestCase;
87
use League\Flysystem\Config;
@@ -72,6 +71,12 @@ private static function bunnyCDNClient(): BunnyCDNClient
7271
return static::$bunnyCDNClient;
7372
}
7473

74+
if (isset($_SERVER['STORAGEZONE'], $_SERVER['APIKEY'])) {
75+
static::$bunnyCDNClient = new BunnyCDNClient($_SERVER['STORAGEZONE'], $_SERVER['APIKEY']);
76+
77+
return static::$bunnyCDNClient;
78+
}
79+
7580
$filesystem = new Filesystem(new InMemoryFilesystemAdapter());
7681

7782
$mock_client = Mockery::mock(new BunnyCDNClient(self::STORAGE_ZONE, 'api-key'));
@@ -180,6 +185,13 @@ public function prefix_path(): void
180185
$adapter->read(static::$prefixPath.'/source.file.svg')
181186
);
182187

188+
$this->assertEquals($content, stream_get_contents($prefixPathAdapter->readStream('source.file.svg')));
189+
190+
$this->assertEquals(
191+
stream_get_contents($prefixPathAdapter->readStream('source.file.svg')),
192+
stream_get_contents($adapter->readStream(static::$prefixPath.'/source.file.svg'))
193+
);
194+
183195
$this->assertSame(
184196
'image/svg+xml',
185197
$prefixPathAdapter->mimeType('source.file.svg')->mimeType()
@@ -190,6 +202,26 @@ public function prefix_path(): void
190202
$adapter->mimeType(static::$prefixPath.'/source.file.svg')->mimeType()
191203
);
192204

205+
$this->assertGreaterThan(
206+
0,
207+
$prefixPathAdapter->fileSize('source.file.svg')->fileSize()
208+
);
209+
210+
$this->assertEquals(
211+
$prefixPathAdapter->fileSize('source.file.svg')->fileSize(),
212+
$adapter->fileSize(static::$prefixPath.'/source.file.svg')->fileSize()
213+
);
214+
215+
$this->assertGreaterThan(
216+
time() - 30,
217+
$prefixPathAdapter->lastModified('source.file.svg')->lastModified()
218+
);
219+
220+
$this->assertEquals(
221+
$prefixPathAdapter->lastModified('source.file.svg')->lastModified(),
222+
$adapter->lastModified(static::$prefixPath.'/source.file.svg')->lastModified()
223+
);
224+
193225
$prefixPathAdapter->delete(
194226
'source.file.svg'
195227
);
@@ -200,6 +232,34 @@ public function prefix_path(): void
200232
});
201233
}
202234

235+
/**
236+
* @test
237+
*/
238+
public function prefix_path_not_in_meta_pr_36(): void
239+
{
240+
$this->runScenario(function () {
241+
$prefixPathAdapter = $this->prefixAdapter();
242+
243+
$prefixPathAdapter->write(
244+
'source.file.svg',
245+
'----',
246+
new Config([Config::OPTION_VISIBILITY => Visibility::PUBLIC])
247+
);
248+
249+
$this->assertSame(
250+
'source.file.svg',
251+
$prefixPathAdapter->mimeType('source.file.svg')->path()
252+
);
253+
254+
$contents = \iterator_to_array($prefixPathAdapter->listContents('/'));
255+
256+
$this->assertCount(1, $contents);
257+
$this->assertSame('source.file.svg', $contents[0]['path']);
258+
259+
$prefixPathAdapter->delete('source.file.svg');
260+
});
261+
}
262+
203263
/**
204264
* Skipped
205265
*/

0 commit comments

Comments
 (0)