diff --git a/readme.md b/readme.md index a40b2e1..e8f37c4 100644 --- a/readme.md +++ b/readme.md @@ -16,13 +16,6 @@ To install `flysystem-bunnycdn`, require the package with no version constraint. composer require platformcommunity/flysystem-bunnycdn "*" ``` -For **Flysystem v2**, use the v2 version of `flysystem-bunnycdn`. - -```bash -composer require platformcommunity/flysystem-bunnycdn "^2.0" -``` - - ## Usage ```php @@ -82,10 +75,21 @@ For a full region list, please visit the [BunnyCDN API documentation page](https ### List of Regions ```php +# Europe BunnyCDNRegion::FALKENSTEIN = 'de'; +BunnyCDNRegion::STOCKHOLM = 'se'; + +# United Kingdom +BunnyCDNRegion::UNITED_KINGDOM = 'uk'; + +# USA BunnyCDNRegion::NEW_YORK = 'ny'; BunnyCDNRegion::LOS_ANGELAS = 'la'; + +# SEA BunnyCDNRegion::SINGAPORE = 'sg'; + +# Oceania BunnyCDNRegion::SYDNEY = 'syd'; ``` diff --git a/src/BunnyCDNAdapter.php b/src/BunnyCDNAdapter.php index 517042a..cbe5331 100644 --- a/src/BunnyCDNAdapter.php +++ b/src/BunnyCDNAdapter.php @@ -6,6 +6,7 @@ use League\Flysystem\Adapter\Polyfill\NotSupportingVisibilityTrait; use League\Flysystem\Adapter\Polyfill\StreamedCopyTrait; use League\Flysystem\Adapter\Polyfill\StreamedTrait; +use League\Flysystem\Adapter\Polyfill\StreamedWritingTrait; use League\Flysystem\Config; use League\Flysystem\Exception; use League\Flysystem\FileNotFoundException; @@ -19,7 +20,7 @@ class BunnyCDNAdapter extends AbstractAdapter { use NotSupportingVisibilityTrait; use StreamedCopyTrait; - use StreamedTrait; + use StreamedWritingTrait; /** * Pull Zone URL @@ -179,6 +180,22 @@ public function read($path) } } + /** + * Reads a file as a stream. + * @param string $path + * @return array|false + */ + public function readStream($path) + { + try { + return [ + 'stream' => $this->client->stream($path) + ]; + } catch (\Exception $e) { + return false; + } + } + /** * @param string $directory * @param bool $recursive diff --git a/src/BunnyCDNClient.php b/src/BunnyCDNClient.php index 9fb2b4e..1982fb9 100644 --- a/src/BunnyCDNClient.php +++ b/src/BunnyCDNClient.php @@ -28,14 +28,18 @@ public function __construct(string $storage_zone_name, string $api_key, string $ private static function get_base_url($region): string { switch ($region) { - case 'ny': + case BunnyCDNRegion::NEW_YORK: return 'https://ny.storage.bunnycdn.com/'; - case 'la': + case BunnyCDNRegion::LOS_ANGELAS: return 'https://la.storage.bunnycdn.com/'; - case 'sg': + case BunnyCDNRegion::SINGAPORE: return 'https://sg.storage.bunnycdn.com/'; - case 'syd': + case BunnyCDNRegion::SYDNEY: return 'https://syd.storage.bunnycdn.com/'; + case BunnyCDNRegion::UNITED_KINGDOM: + return 'https://uk.storage.bunnycdn.com/'; + case BunnyCDNRegion::STOCKHOLM: + return 'https://se.storage.bunnycdn.com/'; default: return 'https://storage.bunnycdn.com/'; } @@ -106,6 +110,37 @@ public function download(string $path): string } } + /** + * @param string $path + * @return resource|null + * @throws BunnyCDNException + * @throws NotFoundException + */ + public function stream(string $path) + { + try { + return $this->client->request( + 'GET', + self::get_base_url($this->region) . Util::normalizePath('/' . $this->storage_zone_name . '/').$path, + array_merge_recursive([ + 'stream' => true, + 'headers' => [ + 'Accept' => '*/*', + 'AccessKey' => $this->api_key, # Honestly... Why do I have to specify this twice... @BunnyCDN + ] + ]) + )->getBody()->detach(); + // @codeCoverageIgnoreStart + } catch (GuzzleException $e) { + if($e->getCode() === 404) { + throw new NotFoundException($e->getMessage()); + } else { + throw new BunnyCDNException($e->getMessage()); + } + } + // @codeCoverageIgnoreEnd + } + /** * @param string $path * @param $contents diff --git a/src/BunnyCDNRegion.php b/src/BunnyCDNRegion.php index db2c6a6..7db1871 100644 --- a/src/BunnyCDNRegion.php +++ b/src/BunnyCDNRegion.php @@ -5,8 +5,12 @@ class BunnyCDNRegion { public const FALKENSTEIN = 'de'; + public const STOCKHOLM = 'se'; public const NEW_YORK = 'ny'; public const LOS_ANGELAS = 'la'; public const SINGAPORE = 'sg'; public const SYDNEY = 'syd'; + public const UNITED_KINGDOM = 'uk'; + + public const DEFAULT = self::FALKENSTEIN; } \ No newline at end of file diff --git a/tests/FlysystemTestSuite.php b/tests/FlysystemTestSuite.php index cb1a67a..6013c94 100644 --- a/tests/FlysystemTestSuite.php +++ b/tests/FlysystemTestSuite.php @@ -65,6 +65,10 @@ protected static function createFilesystemAdapter() return $filesystem->read($path); }); + $mock_client->shouldReceive('stream')->andReturnUsing(function($path) use ($filesystem) { + return $filesystem->readStream($path); + }); + $mock_client->shouldReceive('upload')->andReturnUsing(function($path, $contents) use ($filesystem) { try { $filesystem->write($path, $contents); @@ -379,4 +383,24 @@ public function it_cant_get_public_url() $this->assertEquals(self::PULLZONE_URL . 'testing/test.txt', $adapter->getUrl('/testing/test.txt')); } + + /** + * Fix issue where `fopen` complains when opening downloaded image file#20 + * https://github.com/PlatformCommunity/flysystem-bunnycdn/pull/20 + * + * Seems to not be an issue out of v1, only v2 & v3 + * @throws FileNotFoundException + */ + public function test_regression_pr_20() + { + $image = base64_decode("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z/C/HgAGgwJ/lK3Q6wAAAABJRU5ErkJggg=="); + $this->givenItHasFile('path.png', $image); + + $filesystem = new Filesystem($this->adapter); + + $stream = $filesystem->readStream('path.png'); + + $this->assertIsResource($stream); + $this->assertEquals($image, stream_get_contents($stream)); + } }