Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions src/Backuper.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public function backup(): BackupDto
$zipper->encrypt($password);
}

$zipper->addMeta('created_at', now()->toIso8601String());

$zipMeta = $this->resolveMetaFromZip($zipper);

$zipper->close();

$backup = $this->repository->add($temp_zip_path);
Expand All @@ -51,10 +55,8 @@ public function backup(): BackupDto
$metadata->setCreatedBy($user);
}

$zipper->getMeta()->each(function ($meta, $key) use ($metadata) {
if (isset($meta['skipped'])) {
$metadata->addSkippedPipe($key, $meta['skipped']);
}
$zipMeta->each(fn ($meta, $key) => match ($key) {
'skipped' => $meta->each(fn (string $reason, string $pipe) => $metadata->addSkippedPipe($pipe, $reason)),
});

event(new BackupCreated($backup));
Expand All @@ -75,6 +77,21 @@ public function backup(): BackupDto
}
}

private function resolveMetaFromZip(Zipper $zip)
{
$metadata = collect([
'skipped' => collect(),
]);

$zip->getMeta()->each(function ($meta, $key) use ($metadata) {
if (isset($meta['skipped'])) {
$metadata->get('skipped')->put($key, $meta['skipped']);
}
});

return $metadata;
}

/**
* Remove oldest backups when max backups is exceeded if it's present.
*/
Expand Down
2 changes: 2 additions & 0 deletions src/Contracts/Repositories/BackupRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ interface BackupRepository
{
/**
* Get all backups.
*
* @return Collection<BackupDto>
*/
public function all(): Collection;

Expand Down
6 changes: 5 additions & 1 deletion src/Http/Resources/MetadataResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Itiden\Backup\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;
use Itiden\Backup\DataTransferObjects\SkippedPipeDto;

/**
* @mixin \Itiden\Backup\Models\Metadata
Expand All @@ -17,7 +18,10 @@ public function toArray($request)
'created_by' => $this->getCreatedBy(),
'downloads' => $this->getDownloads(),
'restores' => $this->getRestores(),
'skipped_pipes' => $this->getSkippedPipes(),
'skipped_pipes' => $this->getSkippedPipes()->map(fn (SkippedPipeDto $pipe) => [
'pipe' => $pipe->pipe::getKey(),
'reason' => $pipe->reason,
]),
];
}
}
16 changes: 13 additions & 3 deletions src/Support/Zipper.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,16 @@ public function getArchive(): ZipArchive
/**
* Add some data so that it can be extracted later.
*/
public function addMeta(string $key, array $meta): self
public function addMeta(string $key, array|string $meta): self
{
$current = $this->meta[$key] ?? [];
$this->meta[$key] = array_merge($current, $meta);
if (is_array($meta)) {
$current = $this->meta[$key] ?? [];
$this->meta[$key] = array_merge($current, $meta);
} else {
$this->meta[$key] = $meta;
}

$this->zip->setArchiveComment(comment: json_encode($this->meta));

return $this;
}
Expand All @@ -125,6 +131,10 @@ public function addMeta(string $key, array $meta): self
*/
public function getMeta(): Collection
{
if ($comment = $this->zip->getArchiveComment()) {
$this->meta = json_decode($comment, true);
}

return collect($this->meta);
}
}
3 changes: 2 additions & 1 deletion tests/Unit/PipeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@

$pipe->backup(zip: $zipper, next: $callable);

$zipper->close();

expect($zipper->getMeta())->toHaveKey(Users::class);
expect($zipper->getMeta()[Users::class])->toHaveKey('skipped', 'No users found.');

$zipper->close();
});
16 changes: 16 additions & 0 deletions tests/Unit/ZipperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,19 @@
expect(File::allFiles($unzip)[0]->getRelativePathname())->toBe("test.txt");
expect(File::get($unzip . '/test.txt'))->toBe('test');
});

it('can write meta to zip', function () {
$target = storage_path('test.zip');

Zipper::open($target)
->addFromString('test.txt', 'test')
->addMeta('test', 'test')
->close();

$zip = Zipper::open($target, true);

expect($zip->getMeta())->toHaveKey('test');
expect($zip->getMeta())->get('test')->toBe('test');

$zip->close();
});
Loading