]> BookStack Code Mirror - system-cli/blob - scripts/Services/BackupZip.php
ae82f650380bdd6220a1ff211360b73fed6f605a
[system-cli] / scripts / Services / BackupZip.php
1 <?php
2
3 namespace Cli\Services;
4
5 use ZipArchive;
6
7 class BackupZip
8 {
9     protected ZipArchive $zip;
10     public function __construct(
11         protected string $filePath
12     ) {
13         $this->zip = new ZipArchive();
14         $status = $this->zip->open($this->filePath);
15
16         if (!file_exists($this->filePath) || $status !== true) {
17             throw new \Exception("Could not open file [{$this->filePath}] as ZIP");
18         }
19     }
20
21     public function getContentsOverview(): array
22     {
23         return [
24             'env' => [
25                 'desc' => '.env Config File',
26                 'exists' => boolval($this->zip->statName('.env')),
27             ],
28             'themes' => [
29                 'desc' => 'Themes Folder',
30                 'exists' => boolval($this->zip->statName('themes')),
31             ],
32             'public-uploads' => [
33                 'desc' => 'Public File Uploads',
34                 'exists' => boolval($this->zip->statName('public/uploads')),
35             ],
36             'storage-uploads' => [
37                 'desc' => 'Private File Uploads',
38                 'exists' => boolval($this->zip->statName('storage/uploads')),
39             ],
40             'db' => [
41                 'desc' => 'Database Dump',
42                 'exists' => boolval($this->zip->statName('db.sql')),
43             ],
44         ];
45     }
46
47     public function extractInto(string $directoryPath): void
48     {
49         $result = $this->zip->extractTo($directoryPath);
50         if (!$result) {
51             throw new \Exception("Failed extraction of ZIP into [{$directoryPath}].");
52         }
53     }
54 }