3 namespace Tests\Exports;
5 use BookStack\Exports\Import;
6 use Illuminate\Http\UploadedFile;
11 public static function importFromData(array $importData, array $zipData, array $files = []): Import
13 if (isset($zipData['book'])) {
14 $importData['type'] = 'book';
15 } else if (isset($zipData['chapter'])) {
16 $importData['type'] = 'chapter';
17 } else if (isset($zipData['page'])) {
18 $importData['type'] = 'page';
21 $import = Import::factory()->create($importData);
22 $zip = static::zipUploadFromData($zipData, $files);
23 $targetPath = storage_path($import->path);
24 $targetDir = dirname($targetPath);
26 if (!file_exists($targetDir)) {
30 rename($zip->getRealPath(), $targetPath);
35 public static function deleteZipForImport(Import $import): void
37 $path = storage_path($import->path);
38 if (file_exists($path)) {
43 public static function zipUploadFromData(array $data, array $files = []): UploadedFile
45 $zipFile = tempnam(sys_get_temp_dir(), 'bstest-');
47 $zip = new ZipArchive();
48 $zip->open($zipFile, ZipArchive::CREATE);
49 $zip->addFromString('data.json', json_encode($data));
51 foreach ($files as $name => $file) {
52 $zip->addFile($file, "files/$name");
57 return new UploadedFile($zipFile, 'upload.zip', 'application/zip', null, true);