Skip to content

Commit 2f201af

Browse files
authored
Merge pull request #48 from tinect/removePathPrefixing
feat: remove own implementation for prefixing path, add support for PublicUrlGenerator
2 parents cb9f076 + aa7ded3 commit 2f201af

File tree

3 files changed

+95
-87
lines changed

3 files changed

+95
-87
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
}
1010
],
1111
"require": {
12-
"league/flysystem": "^3.0",
12+
"league/flysystem": "^3.7",
1313
"ext-json": "*",
1414
"guzzlehttp/guzzle": "^7.4",
1515
"league/mime-type-detection": "^1.11"
@@ -19,6 +19,7 @@
1919
"mockery/mockery": "^1.3",
2020
"league/flysystem-adapter-test-utilities": "^3",
2121
"league/flysystem-memory": "^3.0",
22+
"league/flysystem-path-prefixing": "^3.3",
2223
"fzaninotto/faker": "^1.5",
2324
"laravel/pint": "^0.2.3"
2425
},

src/BunnyCDNAdapter.php

Lines changed: 35 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace PlatformCommunity\Flysystem\BunnyCDN;
44

55
use Exception;
6+
use League\Flysystem\CalculateChecksumFromStream;
7+
use League\Flysystem\ChecksumProvider;
68
use League\Flysystem\Config;
79
use League\Flysystem\DirectoryAttributes;
810
use League\Flysystem\DirectoryListing;
@@ -21,13 +23,16 @@
2123
use League\Flysystem\UnableToRetrieveMetadata;
2224
use League\Flysystem\UnableToSetVisibility;
2325
use League\Flysystem\UnableToWriteFile;
26+
use League\Flysystem\UrlGeneration\PublicUrlGenerator;
2427
use League\Flysystem\Visibility;
2528
use League\MimeTypeDetection\FinfoMimeTypeDetector;
2629
use RuntimeException;
2730
use TypeError;
2831

29-
class BunnyCDNAdapter implements FilesystemAdapter
32+
class BunnyCDNAdapter implements FilesystemAdapter, PublicUrlGenerator, ChecksumProvider
3033
{
34+
use CalculateChecksumFromStream;
35+
3136
/**
3237
* Pull Zone URL
3338
*
@@ -40,21 +45,18 @@ class BunnyCDNAdapter implements FilesystemAdapter
4045
*/
4146
private BunnyCDNClient $client;
4247

43-
/**
44-
* @var string
45-
*/
46-
private string $prefixPath;
47-
4848
/**
4949
* @param BunnyCDNClient $client
5050
* @param string $pullzone_url
51-
* @param string $prefixPath
5251
*/
53-
public function __construct(BunnyCDNClient $client, string $pullzone_url = '', string $prefixPath = '')
52+
public function __construct(BunnyCDNClient $client, string $pullzone_url = '')
5453
{
5554
$this->client = $client;
5655
$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+
}
5860
}
5961

6062
/**
@@ -65,9 +67,6 @@ public function __construct(BunnyCDNClient $client, string $pullzone_url = '', s
6567
*/
6668
public function copy($source, $destination, Config $config): void
6769
{
68-
$source = $this->prependPrefix($source);
69-
$destination = $this->prependPrefix($destination);
70-
7170
try {
7271
$this->write($destination, $this->read($source), new Config());
7372
// @codeCoverageIgnoreStart
@@ -84,8 +83,6 @@ public function copy($source, $destination, Config $config): void
8483
*/
8584
public function write($path, $contents, Config $config): void
8685
{
87-
$path = $this->prependPrefix($path);
88-
8986
try {
9087
$this->client->upload($path, $contents);
9188
// @codeCoverageIgnoreStart
@@ -101,8 +98,6 @@ public function write($path, $contents, Config $config): void
10198
*/
10299
public function read($path): string
103100
{
104-
$path = $this->prependPrefix($path);
105-
106101
try {
107102
return $this->client->download($path);
108103
// @codeCoverageIgnoreStart
@@ -117,10 +112,8 @@ public function read($path): string
117112
* @param bool $deep
118113
* @return iterable
119114
*/
120-
public function listContents(string $path = '', bool $deep = false): iterable
115+
public function listContents(string $path, bool $deep): iterable
121116
{
122-
$path = $this->prependPrefix($path);
123-
124117
try {
125118
$entries = $this->client->list($path);
126119
// @codeCoverageIgnoreStart
@@ -147,8 +140,6 @@ public function listContents(string $path = '', bool $deep = false): iterable
147140
*/
148141
protected function normalizeObject(array $bunny_file_array): StorageAttributes
149142
{
150-
$bunny_file_array['Path'] = $this->replaceFirst($this->prependPrefix(''), '', $bunny_file_array['Path']);
151-
152143
return match ($bunny_file_array['IsDirectory']) {
153144
true => new DirectoryAttributes(
154145
Util::normalizePath(
@@ -206,8 +197,6 @@ private function extractExtraMetadata(array $bunny_file_array): array
206197
*/
207198
public function detectMimeType(string $path): string
208199
{
209-
$path = $this->prependPrefix($path);
210-
211200
try {
212201
$detector = new FinfoMimeTypeDetector();
213202
$mimeType = $detector->detectMimeTypeFromPath($path);
@@ -230,8 +219,6 @@ public function detectMimeType(string $path): string
230219
*/
231220
public function writeStream($path, $contents, Config $config): void
232221
{
233-
$path = $this->prependPrefix($path);
234-
235222
$this->write($path, stream_get_contents($contents), $config);
236223
}
237224

@@ -243,8 +230,6 @@ public function writeStream($path, $contents, Config $config): void
243230
*/
244231
public function readStream($path)
245232
{
246-
$path = $this->prependPrefix($path);
247-
248233
try {
249234
return $this->client->stream($path);
250235
// @codeCoverageIgnoreStart
@@ -260,8 +245,6 @@ public function readStream($path)
260245
*/
261246
public function deleteDirectory(string $path): void
262247
{
263-
$path = $this->prependPrefix($path);
264-
265248
try {
266249
$this->client->delete(
267250
rtrim($path, '/').'/'
@@ -279,8 +262,6 @@ public function deleteDirectory(string $path): void
279262
*/
280263
public function createDirectory(string $path, Config $config): void
281264
{
282-
$path = $this->prependPrefix($path);
283-
284265
try {
285266
$this->client->make_directory($path);
286267
// @codeCoverageIgnoreStart
@@ -300,8 +281,6 @@ public function createDirectory(string $path, Config $config): void
300281
*/
301282
public function setVisibility(string $path, string $visibility): void
302283
{
303-
$path = $this->prependPrefix($path);
304-
305284
throw UnableToSetVisibility::atLocation($path, 'BunnyCDN does not support visibility');
306285
}
307286

@@ -310,8 +289,6 @@ public function setVisibility(string $path, string $visibility): void
310289
*/
311290
public function visibility(string $path): FileAttributes
312291
{
313-
$path = $this->prependPrefix($path);
314-
315292
try {
316293
return new FileAttributes($this->getObject($path)->path(), null, $this->pullzone_url ? 'public' : 'private');
317294
} catch (UnableToReadFile|TypeError $e) {
@@ -327,8 +304,6 @@ public function visibility(string $path): FileAttributes
327304
*/
328305
public function mimeType(string $path): FileAttributes
329306
{
330-
$path = $this->prependPrefix($path);
331-
332307
try {
333308
$object = $this->getObject($path);
334309

@@ -368,11 +343,9 @@ public function mimeType(string $path): FileAttributes
368343
protected function getObject(string $path = ''): StorageAttributes
369344
{
370345
$directory = pathinfo($path, PATHINFO_DIRNAME);
371-
$list = (new DirectoryListing($this->listContents($directory)))
346+
$list = (new DirectoryListing($this->listContents($directory, false)))
372347
->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;
376349
})->toArray();
377350

378351
if (count($list) === 1) {
@@ -394,8 +367,6 @@ protected function getObject(string $path = ''): StorageAttributes
394367
*/
395368
public function lastModified(string $path): FileAttributes
396369
{
397-
$path = $this->prependPrefix($path);
398-
399370
try {
400371
return $this->getObject($path);
401372
} catch (UnableToReadFile $e) {
@@ -411,8 +382,6 @@ public function lastModified(string $path): FileAttributes
411382
*/
412383
public function fileSize(string $path): FileAttributes
413384
{
414-
$path = $this->prependPrefix($path);
415-
416385
try {
417386
return $this->getObject($path);
418387
} catch (UnableToReadFile $e) {
@@ -428,9 +397,6 @@ public function fileSize(string $path): FileAttributes
428397
*/
429398
public function move(string $source, string $destination, Config $config): void
430399
{
431-
$source = $this->prependPrefix($source);
432-
$destination = $this->prependPrefix($destination);
433-
434400
try {
435401
$this->write($destination, $this->read($source), new Config());
436402
$this->delete($source);
@@ -445,8 +411,6 @@ public function move(string $source, string $destination, Config $config): void
445411
*/
446412
public function delete($path): void
447413
{
448-
$path = $this->prependPrefix($path);
449-
450414
try {
451415
$this->client->delete($path);
452416
// @codeCoverageIgnoreStart
@@ -472,30 +436,37 @@ public function directoryExists(string $path): bool
472436
*/
473437
public function fileExists(string $path): bool
474438
{
475-
$path = $this->prependPrefix($path);
476-
477439
$list = new DirectoryListing($this->listContents(
478-
Util::splitPathIntoDirectoryAndFile($path)['dir']
440+
Util::splitPathIntoDirectoryAndFile($path)['dir'],
441+
false
479442
));
480443

481444
$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);
485446
})->toArray();
486447

487448
return (bool) count($count);
488449
}
489450

490451
/**
491-
* getURL method for Laravel users who want to use BunnyCDN's PullZone to retrieve a public URL
452+
* @deprecated use publicUrl instead
492453
*
493454
* @param string $path
494455
* @return string
495456
* @codeCoverageIgnore
496457
* @noinspection PhpUnused
497458
*/
498459
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
499470
{
500471
if ($this->pullzone_url === '') {
501472
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
509480
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();
510481
}
511482

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
530484
{
531485
$position = strpos($subject, $search);
532486

533487
if ($position !== false) {
534-
return substr_replace($subject, $replace, $position, strlen($search));
488+
return (string) substr_replace($subject, $replace, $position, strlen($search));
535489
}
536490

537491
return $subject;
538492
}
493+
494+
public function checksum(string $path, Config $config): string
495+
{
496+
return $this->calculateChecksumFromStream($path, $config);
497+
}
539498
}

0 commit comments

Comments
 (0)