Skip to content

Commit 2a08191

Browse files
committed
Updated adapter with readStream support
Fixes #19 and #20
1 parent cbe9f5c commit 2a08191

File tree

7 files changed

+179
-18
lines changed

7 files changed

+179
-18
lines changed

readme.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,21 @@ For a full region list, please visit the [BunnyCDN API documentation page](https
7575
### List of Regions
7676

7777
```php
78+
# Europe
7879
BunnyCDNRegion::FALKENSTEIN = 'de';
80+
BunnyCDNRegion::STOCKHOLM = 'se';
81+
82+
# United Kingdom
83+
BunnyCDNRegion::UNITED_KINGDOM = 'uk';
84+
85+
# USA
7986
BunnyCDNRegion::NEW_YORK = 'ny';
8087
BunnyCDNRegion::LOS_ANGELAS = 'la';
88+
89+
# SEA
8190
BunnyCDNRegion::SINGAPORE = 'sg';
91+
92+
# Oceania
8293
BunnyCDNRegion::SYDNEY = 'syd';
8394
```
8495

src/BunnyCDNAdapter.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,11 @@ public function copy($source, $destination, Config $config): void
6060
{
6161
try {
6262
$this->write($destination, $this->read($source), new Config());
63+
// @codeCoverageIgnoreStart
6364
} catch (UnableToReadFile|UnableToWriteFile $exception) {
6465
throw UnableToCopyFile::fromLocationTo($source, $destination, $exception);
6566
}
67+
// @codeCoverageIgnoreEnd
6668
}
6769

6870
/**
@@ -74,9 +76,11 @@ public function write($path, $contents, Config $config): void
7476
{
7577
try {
7678
$this->client->upload($path, $contents);
79+
// @codeCoverageIgnoreStart
7780
} catch (Exceptions\BunnyCDNException $e) {
7881
throw UnableToWriteFile::atLocation($path, $e->getMessage());
7982
}
83+
// @codeCoverageIgnoreEnd
8084

8185
}
8286

@@ -88,9 +92,11 @@ public function read($path): string
8892
{
8993
try {
9094
return $this->client->download($path);
95+
// @codeCoverageIgnoreStart
9196
} catch (Exceptions\BunnyCDNException $e) {
9297
throw UnableToReadFile::fromLocation($path, $e->getMessage());
9398
}
99+
// @codeCoverageIgnoreEnd
94100
}
95101

96102
/**
@@ -102,9 +108,11 @@ public function listContents(string $path = '', bool $deep = false): iterable
102108
{
103109
try {
104110
$entries = $this->client->list($path);
111+
// @codeCoverageIgnoreStart
105112
} catch (Exceptions\BunnyCDNException $e) {
106113
throw UnableToRetrieveMetadata::create($path, 'folder', $e->getMessage());
107114
}
115+
// @codeCoverageIgnoreEnd
108116

109117
foreach ($entries as $item) {
110118
yield $this->normalizeObject($item);
@@ -187,15 +195,12 @@ public function writeStream($path, $contents, Config $config): void
187195
/**
188196
* @param $path
189197
* @return resource
198+
* @throws Exceptions\BunnyCDNException
199+
* @throws Exceptions\NotFoundException
190200
*/
191201
public function readStream($path)
192202
{
193-
/** @var resource $readStream */
194-
$readStream = fopen('data://text/plain,' . $this->read($path),'r');
195-
196-
rewind($readStream);
197-
198-
return $readStream;
203+
return $this->client->stream($path);
199204
}
200205

201206
/**
@@ -208,10 +213,11 @@ public function deleteDirectory(string $path): void
208213
$this->client->delete(
209214
rtrim($path, '/') . '/'
210215
);
216+
// @codeCoverageIgnoreStart
211217
} catch (Exceptions\BunnyCDNException $e) {
212218
throw UnableToDeleteDirectory::atLocation($path, $e->getMessage());
213219
}
214-
220+
// @codeCoverageIgnoreEnd
215221
}
216222

217223
/**
@@ -222,13 +228,15 @@ public function createDirectory(string $path, Config $config): void
222228
{
223229
try {
224230
$this->client->make_directory($path);
231+
// @codeCoverageIgnoreStart
225232
} catch (Exceptions\BunnyCDNException $e) {
226233
# Lol apparently this is "idempotent" but there's an exception... Sure whatever..
227234
match ($e->getMessage()) {
228235
'Directory already exists' => "",
229236
default => throw UnableToCreateDirectory::atLocation($path, $e->getMessage())
230237
};
231238
}
239+
// @codeCoverageIgnoreEnd
232240

233241
}
234242

@@ -256,6 +264,7 @@ public function visibility(string $path): FileAttributes
256264
/**
257265
* @throws UnableToRetrieveMetadata
258266
* @throws FilesystemException
267+
* @codeCoverageIgnore
259268
*/
260269
public function mimeType(string $path): FileAttributes
261270
{
@@ -351,11 +360,13 @@ public function delete($path): void
351360
{
352361
try {
353362
$this->client->delete($path);
363+
// @codeCoverageIgnoreStart
354364
} catch (Exceptions\BunnyCDNException $e) {
355365
if(!str_contains($e->getMessage(), '404')) { # Urgh
356366
throw UnableToDeleteFile::atLocation($path, $e->getMessage());
357367
}
358368
}
369+
// @codeCoverageIgnoreEnd
359370

360371
}
361372

@@ -388,6 +399,7 @@ public function fileExists(string $path): bool
388399
* getURL method for Laravel users who want to use BunnyCDN's PullZone to retrieve a public URL
389400
* @param string $path
390401
* @return string
402+
* @codeCoverageIgnore
391403
*/
392404
public function getUrl(string $path): string
393405
{

src/BunnyCDNClient.php

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ public function __construct(string $storage_zone_name, string $api_key, string $
2828
private static function get_base_url($region): string
2929
{
3030
return match($region) {
31-
'ny' => 'https://ny.storage.bunnycdn.com/',
32-
'la' => 'https://la.storage.bunnycdn.com/',
33-
'sg' => 'https://sg.storage.bunnycdn.com/',
34-
'syd' => 'https://syd.storage.bunnycdn.com/',
31+
BunnyCDNRegion::NEW_YORK => 'https://ny.storage.bunnycdn.com/',
32+
BunnyCDNRegion::LOS_ANGELAS => 'https://la.storage.bunnycdn.com/',
33+
BunnyCDNRegion::SINGAPORE => 'https://sg.storage.bunnycdn.com/',
34+
BunnyCDNRegion::SYDNEY => 'https://syd.storage.bunnycdn.com/',
35+
BunnyCDNRegion::UNITED_KINGDOM => 'https://uk.storage.bunnycdn.com/',
36+
BunnyCDNRegion::STOCKHOLM => 'https://se.storage.bunnycdn.com/',
3537
default => 'https://storage.bunnycdn.com/'
3638
};
3739
}
@@ -73,15 +75,16 @@ public function list(string $path): array
7375
return array_map(function($bunny_cdn_item) {
7476
return $bunny_cdn_item;
7577
}, $listing);
78+
// @codeCoverageIgnoreStart
7679
} catch (GuzzleException $e) {
7780
throw match ($e->getCode()) {
7881
404 => new NotFoundException($e->getMessage()),
7982
default => new BunnyCDNException($e->getMessage())
8083
};
8184
}
85+
// @codeCoverageIgnoreEnd
8286
}
8387

84-
8588
/**
8689
* @param string $path
8790
* @return mixed
@@ -92,12 +95,44 @@ public function download(string $path): string
9295
{
9396
try {
9497
return $this->request($path . '?download');
98+
// @codeCoverageIgnoreStart
99+
} catch (GuzzleException $e) {
100+
throw match ($e->getCode()) {
101+
404 => new NotFoundException($e->getMessage()),
102+
default => new BunnyCDNException($e->getMessage())
103+
};
104+
}
105+
// @codeCoverageIgnoreEnd
106+
}
107+
108+
/**
109+
* @param string $path
110+
* @return resource|null
111+
* @throws BunnyCDNException
112+
* @throws NotFoundException
113+
*/
114+
public function stream(string $path)
115+
{
116+
try {
117+
return $this->client->request(
118+
'GET',
119+
self::get_base_url($this->region) . Util::normalizePath('/' . $this->storage_zone_name . '/').$path,
120+
array_merge_recursive([
121+
'stream' => true,
122+
'headers' => [
123+
'Accept' => '*/*',
124+
'AccessKey' => $this->api_key, # Honestly... Why do I have to specify this twice... @BunnyCDN
125+
]
126+
])
127+
)->getBody()->detach();
128+
// @codeCoverageIgnoreStart
95129
} catch (GuzzleException $e) {
96130
throw match ($e->getCode()) {
97131
404 => new NotFoundException($e->getMessage()),
98132
default => new BunnyCDNException($e->getMessage())
99133
};
100134
}
135+
// @codeCoverageIgnoreEnd
101136
}
102137

103138
/**
@@ -115,11 +150,13 @@ public function upload(string $path, $contents): mixed
115150
],
116151
'body' => $contents
117152
]);
153+
// @codeCoverageIgnoreStart
118154
} catch (GuzzleException $e) {
119155
throw match ($e->getCode()) {
120156
default => new BunnyCDNException($e->getMessage())
121157
};
122158
}
159+
// @codeCoverageIgnoreEnd
123160
}
124161

125162
/**
@@ -135,12 +172,14 @@ public function make_directory(string $path): mixed
135172
'Content-Length' => 0
136173
],
137174
]);
175+
// @codeCoverageIgnoreStart
138176
} catch (GuzzleException $e) {
139177
throw match ($e->getCode()) {
140178
400 => new BunnyCDNException('Directory already exists'),
141179
default => new BunnyCDNException($e->getMessage())
142180
};
143181
}
182+
// @codeCoverageIgnoreEnd
144183
}
145184

146185
/**
@@ -153,12 +192,14 @@ public function delete(string $path): mixed
153192
{
154193
try {
155194
return $this->request($path, 'DELETE');
195+
// @codeCoverageIgnoreStart
156196
} catch (GuzzleException $e) {
157197
throw match ($e->getCode()) {
158198
404 => new NotFoundException($e->getMessage()),
159199
400 => new DirectoryNotEmptyException($e->getMessage()),
160200
default => new BunnyCDNException($e->getMessage())
161201
};
162202
}
203+
// @codeCoverageIgnoreEnd
163204
}
164205
}

src/BunnyCDNRegion.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55
class BunnyCDNRegion
66
{
77
public const FALKENSTEIN = 'de';
8+
public const STOCKHOLM = 'se';
89
public const NEW_YORK = 'ny';
910
public const LOS_ANGELAS = 'la';
1011
public const SINGAPORE = 'sg';
1112
public const SYDNEY = 'syd';
13+
public const UNITED_KINGDOM = 'uk';
14+
15+
public const DEFAULT = self::FALKENSTEIN;
1216
}

src/Exceptions/BunnyCDNException.php

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

33
namespace PlatformCommunity\Flysystem\BunnyCDN\Exceptions;
44

5-
class BunnyCDNException extends \Exception
5+
use Exception;
6+
7+
class BunnyCDNException extends Exception
68
{
79

810
}

0 commit comments

Comments
 (0)