Improve caching, performance and add dry-run

This commit is contained in:
René Preuß
2021-07-22 16:42:01 +02:00
parent 53cff7d805
commit 820287dd2c
13 changed files with 342 additions and 40 deletions

View File

@@ -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, ['/'])) {