]> BookStack Code Mirror - bookstack/blob - app/Exports/ZipExportBuilder.php
ZIP Exports: Finished up format doc, move files, started builder
[bookstack] / app / Exports / ZipExportBuilder.php
1 <?php
2
3 namespace BookStack\Exports;
4
5 use BookStack\Entities\Models\Page;
6 use BookStack\Exceptions\ZipExportException;
7 use ZipArchive;
8
9 class ZipExportBuilder
10 {
11     protected array $data = [];
12
13     /**
14      * @throws ZipExportException
15      */
16     public function buildForPage(Page $page): string
17     {
18         $this->data['page'] = [
19             'id' => $page->id,
20         ];
21
22         return $this->build();
23     }
24
25     /**
26      * @throws ZipExportException
27      */
28     protected function build(): string
29     {
30         $this->data['exported_at'] = date(DATE_ATOM);
31         $this->data['instance'] = [
32             'version' => trim(file_get_contents(base_path('version'))),
33             'id_ciphertext' => encrypt('bookstack'),
34         ];
35
36         $zipFile = tempnam(sys_get_temp_dir(), 'bszip-');
37         $zip = new ZipArchive();
38         $opened = $zip->open($zipFile, ZipArchive::CREATE);
39         if ($opened !== true) {
40             throw new ZipExportException('Failed to create zip file for export.');
41         }
42
43         $zip->addFromString('data.json', json_encode($this->data));
44         $zip->addEmptyDir('files');
45
46         return $zipFile;
47     }
48 }