]> BookStack Code Mirror - bookstack/blob - app/Exports/ImportRepo.php
c8157967bc3783a3f3abf02471e67819deda8b05
[bookstack] / app / Exports / ImportRepo.php
1 <?php
2
3 namespace BookStack\Exports;
4
5 use BookStack\Exceptions\ZipValidationException;
6 use BookStack\Exports\ZipExports\ZipExportReader;
7 use BookStack\Exports\ZipExports\ZipExportValidator;
8 use BookStack\Uploads\FileStorage;
9 use Symfony\Component\HttpFoundation\File\UploadedFile;
10
11 class ImportRepo
12 {
13     public function __construct(
14         protected FileStorage $storage,
15     ) {
16     }
17
18     public function storeFromUpload(UploadedFile $file): Import
19     {
20         $zipPath = $file->getRealPath();
21
22         $errors = (new ZipExportValidator($zipPath))->validate();
23         if ($errors) {
24             throw new ZipValidationException($errors);
25         }
26
27         $zipEntityInfo = (new ZipExportReader($zipPath))->getEntityInfo();
28         $import = new Import();
29         $import->name = $zipEntityInfo['name'];
30         $import->book_count = $zipEntityInfo['book_count'];
31         $import->chapter_count = $zipEntityInfo['chapter_count'];
32         $import->page_count = $zipEntityInfo['page_count'];
33         $import->created_by = user()->id;
34         $import->size = filesize($zipPath);
35
36         $path = $this->storage->uploadFile(
37             $file,
38             'uploads/files/imports/',
39             '',
40             'zip'
41         );
42
43         $import->path = $path;
44         $import->save();
45
46         return $import;
47     }
48 }