Skip to content

Commit

Permalink
Updated adapter with readStream support
Browse files Browse the repository at this point in the history
  • Loading branch information
sifex committed Mar 16, 2022
1 parent 6474adc commit a1bc9ed
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 12 deletions.
18 changes: 11 additions & 7 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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';
```

Expand Down
19 changes: 18 additions & 1 deletion src/BunnyCDNAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,7 +20,7 @@ class BunnyCDNAdapter extends AbstractAdapter
{
use NotSupportingVisibilityTrait;
use StreamedCopyTrait;
use StreamedTrait;
use StreamedWritingTrait;

/**
* Pull Zone URL
Expand Down Expand Up @@ -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
Expand Down
43 changes: 39 additions & 4 deletions src/BunnyCDNClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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/';
}
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/BunnyCDNRegion.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
24 changes: 24 additions & 0 deletions tests/FlysystemTestSuite.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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));
}
}

0 comments on commit a1bc9ed

Please sign in to comment.