mirror of
https://github.com/bitinflow/bunny-cli.git
synced 2026-03-13 21:55:54 +00:00
Improve caching, performance and add dry-run
This commit is contained in:
@@ -2,26 +2,35 @@
|
||||
|
||||
namespace App\Bunny\Filesystem;
|
||||
|
||||
use App\Bunny\Filesystem\Exceptions\FileNotFoundException;
|
||||
use App\Bunny\Filesystem\Exceptions\FilesystemException;
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Exception\ClientException;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use GuzzleHttp\Exception\RequestException;
|
||||
use GuzzleHttp\Promise\PromiseInterface;
|
||||
use GuzzleHttp\RequestOptions;
|
||||
use Illuminate\Console\OutputStyle;
|
||||
use Illuminate\Support\Str;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
class EdgeStorage
|
||||
{
|
||||
private Client $client;
|
||||
private EdgeStorageCache $storageCache;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->storageCache = new EdgeStorageCache($this);
|
||||
$this->client = new Client([
|
||||
'base_uri' => sprintf('https://%s', config('bunny.storage.hostname')),
|
||||
'http_errors' => false,
|
||||
'headers' => [
|
||||
'User-Agent' => 'BunnyCLI/0.1',
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
public function allFiles(string $path, callable $advance = null, &$results = array())
|
||||
public function allFiles(string $path, callable $advance = null, &$results = array()): array
|
||||
{
|
||||
$promise = $this->client->getAsync(self::normalizePath($path, true), [
|
||||
RequestOptions::HEADERS => [
|
||||
@@ -48,8 +57,7 @@ class EdgeStorage
|
||||
}
|
||||
},
|
||||
function (RequestException $e) {
|
||||
echo $e->getMessage() . "\n";
|
||||
echo $e->getRequest()->getMethod();
|
||||
throw FilesystemException::fromPrevious($e);
|
||||
}
|
||||
);
|
||||
|
||||
@@ -58,14 +66,42 @@ class EdgeStorage
|
||||
return $results;
|
||||
}
|
||||
|
||||
public function put(LocalFile $file, string $local, string $edge): PromiseInterface
|
||||
/**
|
||||
* @throws FilesystemException
|
||||
*/
|
||||
public function get(EdgeFile $file): string
|
||||
{
|
||||
try {
|
||||
$response = $this->client->get(self::normalizePath($file->getFilename(), $file->isDirectory()), [
|
||||
RequestOptions::HEADERS => [
|
||||
'AccessKey' => config('bunny.storage.password'),
|
||||
],
|
||||
]);
|
||||
} catch (ClientException $exception) {
|
||||
throw FilesystemException::fromResponse($exception->getResponse());
|
||||
} catch (GuzzleException $exception) {
|
||||
throw FilesystemException::fromPrevious($exception);
|
||||
}
|
||||
|
||||
if ($response->getStatusCode() === 404) {
|
||||
throw FileNotFoundException::fromFile($file);
|
||||
}
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw FilesystemException::fromResponse($response);
|
||||
}
|
||||
|
||||
return $response->getBody()->getContents();
|
||||
}
|
||||
|
||||
public function put(LocalFile $file, string $local = '', string $edge = ''): PromiseInterface
|
||||
{
|
||||
return $this->client->putAsync(self::normalizePath($file->getFilename($local, $edge), $file->isDirectory()), [
|
||||
RequestOptions::HEADERS => [
|
||||
'AccessKey' => config('bunny.storage.password'),
|
||||
'Checksum' => $file->getChecksum(),
|
||||
],
|
||||
RequestOptions::BODY => fopen($file->getFilename(), 'r'),
|
||||
RequestOptions::BODY => $file->getResource(),
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -83,6 +119,11 @@ class EdgeStorage
|
||||
return $this->client;
|
||||
}
|
||||
|
||||
public function getStorageCache(): EdgeStorageCache
|
||||
{
|
||||
return $this->storageCache;
|
||||
}
|
||||
|
||||
private static function normalizePath(string $filename, bool $isDirectory): string
|
||||
{
|
||||
if (!Str::startsWith($filename, ['/'])) {
|
||||
|
||||
Reference in New Issue
Block a user