mirror of
https://github.com/BookStackApp/BookStack.git
synced 2024-12-13 22:43:39 +08:00
92cfde495e
Reduced import data will now be stored on the import itself, instead of storing a set of totals.
93 lines
2.4 KiB
PHP
93 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace BookStack\Exports\ZipExports;
|
|
|
|
use BookStack\Exceptions\ZipExportException;
|
|
use BookStack\Exports\ZipExports\Models\ZipExportBook;
|
|
use BookStack\Exports\ZipExports\Models\ZipExportChapter;
|
|
use BookStack\Exports\ZipExports\Models\ZipExportModel;
|
|
use BookStack\Exports\ZipExports\Models\ZipExportPage;
|
|
use ZipArchive;
|
|
|
|
class ZipExportReader
|
|
{
|
|
protected ZipArchive $zip;
|
|
protected bool $open = false;
|
|
|
|
public function __construct(
|
|
protected string $zipPath,
|
|
) {
|
|
$this->zip = new ZipArchive();
|
|
}
|
|
|
|
/**
|
|
* @throws ZipExportException
|
|
*/
|
|
protected function open(): void
|
|
{
|
|
if ($this->open) {
|
|
return;
|
|
}
|
|
|
|
// Validate file exists
|
|
if (!file_exists($this->zipPath) || !is_readable($this->zipPath)) {
|
|
throw new ZipExportException(trans('errors.import_zip_cant_read'));
|
|
}
|
|
|
|
// Validate file is valid zip
|
|
$opened = $this->zip->open($this->zipPath, ZipArchive::RDONLY);
|
|
if ($opened !== true) {
|
|
throw new ZipExportException(trans('errors.import_zip_cant_read'));
|
|
}
|
|
|
|
$this->open = true;
|
|
}
|
|
|
|
public function close(): void
|
|
{
|
|
if ($this->open) {
|
|
$this->zip->close();
|
|
$this->open = false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @throws ZipExportException
|
|
*/
|
|
public function readData(): array
|
|
{
|
|
$this->open();
|
|
|
|
// Validate json data exists, including metadata
|
|
$jsonData = $this->zip->getFromName('data.json') ?: '';
|
|
$importData = json_decode($jsonData, true);
|
|
if (!$importData) {
|
|
throw new ZipExportException(trans('errors.import_zip_cant_decode_data'));
|
|
}
|
|
|
|
return $importData;
|
|
}
|
|
|
|
public function fileExists(string $fileName): bool
|
|
{
|
|
return $this->zip->statName("files/{$fileName}") !== false;
|
|
}
|
|
|
|
/**
|
|
* @throws ZipExportException
|
|
*/
|
|
public function decodeDataToExportModel(): ZipExportBook|ZipExportChapter|ZipExportPage
|
|
{
|
|
$data = $this->readData();
|
|
if (isset($data['book'])) {
|
|
return ZipExportBook::fromArray($data['book']);
|
|
} else if (isset($data['chapter'])) {
|
|
return ZipExportChapter::fromArray($data['chapter']);
|
|
} else if (isset($data['page'])) {
|
|
return ZipExportPage::fromArray($data['page']);
|
|
}
|
|
|
|
throw new ZipExportException("Could not identify content in ZIP file data.");
|
|
}
|
|
}
|