Skip to content

Commit 7cda890

Browse files
authored
Merge pull request #1 from tinect/fix2
fix: prefixPath
2 parents 8b8e157 + 40b09d4 commit 7cda890

File tree

2 files changed

+97
-23
lines changed

2 files changed

+97
-23
lines changed

src/BunnyCDNAdapter.php

Lines changed: 36 additions & 22 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,7 +147,7 @@ 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->prefixPath, '', $bunny_file_array['Path']);
150+
$bunny_file_array['Path'] = str_replace($this->prependPrefix(''), '', $bunny_file_array['Path']);
151151

152152
return match ($bunny_file_array['IsDirectory']) {
153153
true => new DirectoryAttributes(
@@ -206,7 +206,7 @@ private function extractExtraMetadata(array $bunny_file_array): array
206206
*/
207207
public function detectMimeType(string $path): string
208208
{
209-
$this->prependPrefix($path);
209+
$path = $this->prependPrefix($path);
210210

211211
try {
212212
$detector = new FinfoMimeTypeDetector();
@@ -230,7 +230,7 @@ public function detectMimeType(string $path): string
230230
*/
231231
public function writeStream($path, $contents, Config $config): void
232232
{
233-
$this->prependPrefix($path);
233+
$path = $this->prependPrefix($path);
234234

235235
$this->write($path, stream_get_contents($contents), $config);
236236
}
@@ -243,7 +243,7 @@ public function writeStream($path, $contents, Config $config): void
243243
*/
244244
public function readStream($path)
245245
{
246-
$this->prependPrefix($path);
246+
$path = $this->prependPrefix($path);
247247

248248
try {
249249
return $this->client->stream($path);
@@ -260,7 +260,7 @@ public function readStream($path)
260260
*/
261261
public function deleteDirectory(string $path): void
262262
{
263-
$this->prependPrefix($path);
263+
$path = $this->prependPrefix($path);
264264

265265
try {
266266
$this->client->delete(
@@ -279,7 +279,7 @@ public function deleteDirectory(string $path): void
279279
*/
280280
public function createDirectory(string $path, Config $config): void
281281
{
282-
$this->prependPrefix($path);
282+
$path = $this->prependPrefix($path);
283283

284284
try {
285285
$this->client->make_directory($path);
@@ -300,7 +300,7 @@ public function createDirectory(string $path, Config $config): void
300300
*/
301301
public function setVisibility(string $path, string $visibility): void
302302
{
303-
$this->prependPrefix($path);
303+
$path = $this->prependPrefix($path);
304304

305305
throw UnableToSetVisibility::atLocation($path, 'BunnyCDN does not support visibility');
306306
}
@@ -310,6 +310,8 @@ public function setVisibility(string $path, string $visibility): void
310310
*/
311311
public function visibility(string $path): FileAttributes
312312
{
313+
$path = $this->prependPrefix($path);
314+
313315
try {
314316
return new FileAttributes($this->getObject($path)->path(), null, $this->pullzone_url ? 'public' : 'private');
315317
} catch (UnableToReadFile|TypeError $e) {
@@ -325,6 +327,8 @@ public function visibility(string $path): FileAttributes
325327
*/
326328
public function mimeType(string $path): FileAttributes
327329
{
330+
$path = $this->prependPrefix($path);
331+
328332
try {
329333
$object = $this->getObject($path);
330334

@@ -366,7 +370,9 @@ protected function getObject(string $path = ''): StorageAttributes
366370
$directory = pathinfo($path, PATHINFO_DIRNAME);
367371
$list = (new DirectoryListing($this->listContents($directory)))
368372
->filter(function (StorageAttributes $item) use ($path) {
369-
return $item->path() === $path;
373+
$itemPath = $this->prependPrefix(Util::normalizePath($item->path()));
374+
375+
return $itemPath === $path;
370376
})->toArray();
371377

372378
if (count($list) === 1) {
@@ -388,6 +394,8 @@ protected function getObject(string $path = ''): StorageAttributes
388394
*/
389395
public function lastModified(string $path): FileAttributes
390396
{
397+
$path = $this->prependPrefix($path);
398+
391399
try {
392400
return $this->getObject($path);
393401
} catch (UnableToReadFile $e) {
@@ -403,6 +411,8 @@ public function lastModified(string $path): FileAttributes
403411
*/
404412
public function fileSize(string $path): FileAttributes
405413
{
414+
$path = $this->prependPrefix($path);
415+
406416
try {
407417
return $this->getObject($path);
408418
} catch (UnableToReadFile $e) {
@@ -418,8 +428,8 @@ public function fileSize(string $path): FileAttributes
418428
*/
419429
public function move(string $source, string $destination, Config $config): void
420430
{
421-
$this->prependPrefix($source);
422-
$this->prependPrefix($destination);
431+
$source = $this->prependPrefix($source);
432+
$destination = $this->prependPrefix($destination);
423433

424434
try {
425435
$this->write($destination, $this->read($source), new Config());
@@ -435,7 +445,7 @@ public function move(string $source, string $destination, Config $config): void
435445
*/
436446
public function delete($path): void
437447
{
438-
$this->prependPrefix($path);
448+
$path = $this->prependPrefix($path);
439449

440450
try {
441451
$this->client->delete($path);
@@ -462,12 +472,16 @@ public function directoryExists(string $path): bool
462472
*/
463473
public function fileExists(string $path): bool
464474
{
475+
$path = $this->prependPrefix($path);
476+
465477
$list = new DirectoryListing($this->listContents(
466478
Util::splitPathIntoDirectoryAndFile($path)['dir']
467479
));
468480

469481
$count = $list->filter(function (StorageAttributes $item) use ($path) {
470-
return Util::normalizePath($item->path()) === Util::normalizePath($path);
482+
$itemPath = $this->prependPrefix(Util::normalizePath($item->path()));
483+
484+
return $itemPath === Util::normalizePath($path);
471485
})->toArray();
472486

473487
return (bool) count($count);
@@ -495,20 +509,20 @@ private static function parse_bunny_timestamp(string $timestamp): int
495509
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();
496510
}
497511

498-
private function prependPrefix(string &$path): void
512+
private function prependPrefix(string $path): string
499513
{
500514
if ($this->prefixPath === '') {
501-
return;
515+
return $path;
502516
}
503517

504518
if ($path === $this->prefixPath) {
505-
return;
519+
return $path;
506520
}
507521

508522
if (\str_starts_with($path, $this->prefixPath.'/')) {
509-
return;
523+
return $path;
510524
}
511525

512-
$path = $this->prefixPath.'/'.$path;
526+
return $this->prefixPath.'/'.$path;
513527
}
514528
}

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)