22
33namespace BookStack \Exports ;
44
5+ use BookStack \Exceptions \FileUploadException ;
6+ use BookStack \Exceptions \ZipExportException ;
57use BookStack \Exceptions \ZipValidationException ;
8+ use BookStack \Exports \ZipExports \Models \ZipExportBook ;
9+ use BookStack \Exports \ZipExports \Models \ZipExportChapter ;
10+ use BookStack \Exports \ZipExports \Models \ZipExportPage ;
611use BookStack \Exports \ZipExports \ZipExportReader ;
712use BookStack \Exports \ZipExports \ZipExportValidator ;
813use BookStack \Uploads \FileStorage ;
@@ -41,6 +46,11 @@ public function findVisible(int $id): Import
4146 return $ query ->findOrFail ($ id );
4247 }
4348
49+ /**
50+ * @throws FileUploadException
51+ * @throws ZipValidationException
52+ * @throws ZipExportException
53+ */
4454 public function storeFromUpload (UploadedFile $ file ): Import
4555 {
4656 $ zipPath = $ file ->getRealPath ();
@@ -50,15 +60,23 @@ public function storeFromUpload(UploadedFile $file): Import
5060 throw new ZipValidationException ($ errors );
5161 }
5262
53- $ zipEntityInfo = (new ZipExportReader ($ zipPath ))->getEntityInfo ();
63+ $ reader = new ZipExportReader ($ zipPath );
64+ $ exportModel = $ reader ->decodeDataToExportModel ();
65+
5466 $ import = new Import ();
55- $ import ->name = $ zipEntityInfo ['name ' ];
56- $ import ->book_count = $ zipEntityInfo ['book_count ' ];
57- $ import ->chapter_count = $ zipEntityInfo ['chapter_count ' ];
58- $ import ->page_count = $ zipEntityInfo ['page_count ' ];
67+ $ import ->type = match (get_class ($ exportModel )) {
68+ ZipExportPage::class => 'page ' ,
69+ ZipExportChapter::class => 'chapter ' ,
70+ ZipExportBook::class => 'book ' ,
71+ };
72+
73+ $ import ->name = $ exportModel ->name ;
5974 $ import ->created_by = user ()->id ;
6075 $ import ->size = filesize ($ zipPath );
6176
77+ $ exportModel ->metadataOnly ();
78+ $ import ->metadata = json_encode ($ exportModel );
79+
6280 $ path = $ this ->storage ->uploadFile (
6381 $ file ,
6482 'uploads/files/imports/ ' ,
@@ -72,6 +90,13 @@ public function storeFromUpload(UploadedFile $file): Import
7290 return $ import ;
7391 }
7492
93+ public function runImport (Import $ import , ?string $ parent = null )
94+ {
95+ // TODO - Download import zip (if needed)
96+ // TODO - Validate zip file again
97+ // TODO - Check permissions before (create for main item, create for children, create for related items [image, attachments])
98+ }
99+
75100 public function deleteImport (Import $ import ): void
76101 {
77102 $ this ->storage ->delete ($ import ->path );
0 commit comments