Skip to content

Commit 3e3ffa8

Browse files
Merge pull request #45 from itiden/feature/store-creation-metadata-in-backup
Add ability to store zip meta as comment on archive
2 parents bd9f603 + f9428bf commit 3e3ffa8

File tree

6 files changed

+59
-9
lines changed

6 files changed

+59
-9
lines changed

src/Backuper.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ public function backup(): BackupDto
4141
$zipper->encrypt($password);
4242
}
4343

44+
$zipper->addMeta('created_at', now()->toIso8601String());
45+
46+
$zipMeta = $this->resolveMetaFromZip($zipper);
47+
4448
$zipper->close();
4549

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

54-
$zipper->getMeta()->each(function ($meta, $key) use ($metadata) {
55-
if (isset($meta['skipped'])) {
56-
$metadata->addSkippedPipe($key, $meta['skipped']);
57-
}
58+
$zipMeta->each(fn ($meta, $key) => match ($key) {
59+
'skipped' => $meta->each(fn (string $reason, string $pipe) => $metadata->addSkippedPipe($pipe, $reason)),
5860
});
5961

6062
event(new BackupCreated($backup));
@@ -75,6 +77,21 @@ public function backup(): BackupDto
7577
}
7678
}
7779

80+
private function resolveMetaFromZip(Zipper $zip)
81+
{
82+
$metadata = collect([
83+
'skipped' => collect(),
84+
]);
85+
86+
$zip->getMeta()->each(function ($meta, $key) use ($metadata) {
87+
if (isset($meta['skipped'])) {
88+
$metadata->get('skipped')->put($key, $meta['skipped']);
89+
}
90+
});
91+
92+
return $metadata;
93+
}
94+
7895
/**
7996
* Remove oldest backups when max backups is exceeded if it's present.
8097
*/

src/Contracts/Repositories/BackupRepository.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ interface BackupRepository
1111
{
1212
/**
1313
* Get all backups.
14+
*
15+
* @return Collection<BackupDto>
1416
*/
1517
public function all(): Collection;
1618

src/Http/Resources/MetadataResource.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Itiden\Backup\Http\Resources;
66

77
use Illuminate\Http\Resources\Json\JsonResource;
8+
use Itiden\Backup\DataTransferObjects\SkippedPipeDto;
89

910
/**
1011
* @mixin \Itiden\Backup\Models\Metadata
@@ -17,7 +18,10 @@ public function toArray($request)
1718
'created_by' => $this->getCreatedBy(),
1819
'downloads' => $this->getDownloads(),
1920
'restores' => $this->getRestores(),
20-
'skipped_pipes' => $this->getSkippedPipes(),
21+
'skipped_pipes' => $this->getSkippedPipes()->map(fn (SkippedPipeDto $pipe) => [
22+
'pipe' => $pipe->pipe::getKey(),
23+
'reason' => $pipe->reason,
24+
]),
2125
];
2226
}
2327
}

src/Support/Zipper.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,16 @@ public function getArchive(): ZipArchive
110110
/**
111111
* Add some data so that it can be extracted later.
112112
*/
113-
public function addMeta(string $key, array $meta): self
113+
public function addMeta(string $key, array|string $meta): self
114114
{
115-
$current = $this->meta[$key] ?? [];
116-
$this->meta[$key] = array_merge($current, $meta);
115+
if (is_array($meta)) {
116+
$current = $this->meta[$key] ?? [];
117+
$this->meta[$key] = array_merge($current, $meta);
118+
} else {
119+
$this->meta[$key] = $meta;
120+
}
121+
122+
$this->zip->setArchiveComment(comment: json_encode($this->meta));
117123

118124
return $this;
119125
}
@@ -125,6 +131,10 @@ public function addMeta(string $key, array $meta): self
125131
*/
126132
public function getMeta(): Collection
127133
{
134+
if ($comment = $this->zip->getArchiveComment()) {
135+
$this->meta = json_decode($comment, true);
136+
}
137+
128138
return collect($this->meta);
129139
}
130140
}

tests/Unit/PipeTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@
5757

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

60-
$zipper->close();
6160

6261
expect($zipper->getMeta())->toHaveKey(Users::class);
6362
expect($zipper->getMeta()[Users::class])->toHaveKey('skipped', 'No users found.');
63+
64+
$zipper->close();
6465
});

tests/Unit/ZipperTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,19 @@
121121
expect(File::allFiles($unzip)[0]->getRelativePathname())->toBe("test.txt");
122122
expect(File::get($unzip . '/test.txt'))->toBe('test');
123123
});
124+
125+
it('can write meta to zip', function () {
126+
$target = storage_path('test.zip');
127+
128+
Zipper::open($target)
129+
->addFromString('test.txt', 'test')
130+
->addMeta('test', 'test')
131+
->close();
132+
133+
$zip = Zipper::open($target, true);
134+
135+
expect($zip->getMeta())->toHaveKey('test');
136+
expect($zip->getMeta())->get('test')->toBe('test');
137+
138+
$zip->close();
139+
});

0 commit comments

Comments
 (0)