Improve lock file

This commit is contained in:
René Preuß
2021-07-22 17:28:09 +02:00
parent 820287dd2c
commit 4b9e117f1a
8 changed files with 105 additions and 18 deletions

View File

@@ -7,8 +7,8 @@ namespace App\Bunny\Filesystem;
class CompareOptions
{
const START = 'start';
const NO_SHA256_CACHE = 'no_sha256_cache';
const NO_SHA256_VERIFICATION = 'no_sha256_verification';
const NO_SHA256_GENERATION = 'no_sha256_generation';
const SHA256_NAME = 'sha256_name';
const LOCK_FILE = 'lock_file';
const DRY_RUN = 'dry-run';
}

View File

@@ -4,12 +4,14 @@ namespace App\Bunny\Filesystem;
use App\Bunny\Filesystem\Exceptions\FileNotFoundException;
use App\Bunny\Filesystem\Exceptions\FilesystemException;
use App\Bunny\Lock\Exceptions\LockException;
use App\Bunny\Lock\Lock;
use Psr\Http\Message\ResponseInterface;
class EdgeStorageCache
{
private EdgeStorage $edgeStorage;
private string $filename = '.well-known/bunny.sha256';
private string $filename = Lock::DEFAULT_FILENAME;
public function __construct(EdgeStorage $edgeStorage)
{
@@ -47,18 +49,22 @@ class EdgeStorageCache
return $response->getStatusCode() === 200;
}
/**
* @throws FileNotFoundException
* @throws LockException
*/
private function extract(string $contents): array
{
if (!$array = json_decode($contents, true)) {
throw new FileNotFoundException('Cannot parse cache file.');
}
$lock = Lock::parse($contents, $this->filename);
return array_map(fn(array $x) => EdgeFile::fromArray($x), $array);
return array_map(fn(array $x) => EdgeFile::fromArray($x), $lock->getFiles());
}
private function hydrate(array $files, string $search = '', string $replace = ''): string
{
return json_encode(array_map(fn(LocalFile $x) => $x->toArray($search, $replace), $files), JSON_PRETTY_PRINT);
return Lock::fromFiles(
array_map(fn(LocalFile $x) => $x->toArray($search, $replace), $files)
)->toString();
}
public function setFilename(string $filename)

View File

@@ -168,9 +168,9 @@ class FileCompare
*/
private function getEdgeFiles(array $options, string $edge, int $expectedMax): array
{
$this->edgeStorage->getStorageCache()->setFilename($options[CompareOptions::SHA256_NAME]);
$this->edgeStorage->getStorageCache()->setFilename($options[CompareOptions::LOCK_FILE]);
if ($options[CompareOptions::NO_SHA256_CACHE]) {
if ($options[CompareOptions::NO_SHA256_VERIFICATION]) {
return $this->getAllFilesRecursive($expectedMax, $edge);
}
@@ -180,7 +180,7 @@ class FileCompare
} catch (FileNotFoundException $exception) {
$this->command->warn(sprintf(
'⚠ Cannot fetch %s from storage due "%s". Using recursive fallback...',
$options[CompareOptions::SHA256_NAME],
$options[CompareOptions::LOCK_FILE],
$exception->getMessage()
));
return $this->getAllFilesRecursive($expectedMax, $edge);