]> BookStack Code Mirror - bookstack/blob - app/Exports/ZipExports/ZipExportBuilder.php
ZIP Exports: Changed the instance id mechanism
[bookstack] / app / Exports / ZipExports / ZipExportBuilder.php
1 <?php
2
3 namespace BookStack\Exports\ZipExports;
4
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Chapter;
7 use BookStack\Entities\Models\Page;
8 use BookStack\Exceptions\ZipExportException;
9 use BookStack\Exports\ZipExports\Models\ZipExportBook;
10 use BookStack\Exports\ZipExports\Models\ZipExportChapter;
11 use BookStack\Exports\ZipExports\Models\ZipExportPage;
12 use ZipArchive;
13
14 class ZipExportBuilder
15 {
16     protected array $data = [];
17
18     public function __construct(
19         protected ZipExportFiles $files,
20         protected ZipExportReferences $references,
21     ) {
22     }
23
24     /**
25      * @throws ZipExportException
26      */
27     public function buildForPage(Page $page): string
28     {
29         $exportPage = ZipExportPage::fromModel($page, $this->files);
30         $this->data['page'] = $exportPage;
31
32         $this->references->addPage($exportPage);
33
34         return $this->build();
35     }
36
37     /**
38      * @throws ZipExportException
39      */
40     public function buildForChapter(Chapter $chapter): string
41     {
42         $exportChapter = ZipExportChapter::fromModel($chapter, $this->files);
43         $this->data['chapter'] = $exportChapter;
44
45         $this->references->addChapter($exportChapter);
46
47         return $this->build();
48     }
49
50     /**
51      * @throws ZipExportException
52      */
53     public function buildForBook(Book $book): string
54     {
55         $exportBook = ZipExportBook::fromModel($book, $this->files);
56         $this->data['book'] = $exportBook;
57
58         $this->references->addBook($exportBook);
59
60         return $this->build();
61     }
62
63     /**
64      * @throws ZipExportException
65      */
66     protected function build(): string
67     {
68         $this->references->buildReferences($this->files);
69
70         $this->data['exported_at'] = date(DATE_ATOM);
71         $this->data['instance'] = [
72             'id'      => setting('instance-id', ''),
73             'version' => trim(file_get_contents(base_path('version'))),
74         ];
75
76         $zipFile = tempnam(sys_get_temp_dir(), 'bszip-');
77         $zip = new ZipArchive();
78         $opened = $zip->open($zipFile, ZipArchive::CREATE);
79         if ($opened !== true) {
80             throw new ZipExportException('Failed to create zip file for export.');
81         }
82
83         $zip->addFromString('data.json', json_encode($this->data));
84         $zip->addEmptyDir('files');
85
86         $toRemove = [];
87         $this->files->extractEach(function ($filePath, $fileRef) use ($zip, &$toRemove) {
88             $zip->addFile($filePath, "files/$fileRef");
89             $toRemove[] = $filePath;
90         });
91
92         $zip->close();
93
94         foreach ($toRemove as $file) {
95             unlink($file);
96         }
97
98         return $zipFile;
99     }
100 }