3 namespace BookStack\Exports;
5 use BookStack\Exceptions\ZipValidationException;
6 use BookStack\Exports\ZipExports\ZipExportReader;
7 use BookStack\Exports\ZipExports\ZipExportValidator;
8 use BookStack\Uploads\FileStorage;
9 use Illuminate\Database\Eloquent\Collection;
10 use Symfony\Component\HttpFoundation\File\UploadedFile;
14 public function __construct(
15 protected FileStorage $storage,
20 * @return Collection<Import>
22 public function getVisibleImports(): Collection
24 $query = Import::query();
26 if (!userCan('settings-manage')) {
27 $query->where('created_by', user()->id);
33 public function findVisible(int $id): Import
35 $query = Import::query();
37 if (!userCan('settings-manage')) {
38 $query->where('created_by', user()->id);
41 return $query->findOrFail($id);
44 public function storeFromUpload(UploadedFile $file): Import
46 $zipPath = $file->getRealPath();
48 $errors = (new ZipExportValidator($zipPath))->validate();
50 throw new ZipValidationException($errors);
53 $zipEntityInfo = (new ZipExportReader($zipPath))->getEntityInfo();
54 $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'];
59 $import->created_by = user()->id;
60 $import->size = filesize($zipPath);
62 $path = $this->storage->uploadFile(
64 'uploads/files/imports/',
69 $import->path = $path;
75 public function deleteImport(Import $import): void
77 $this->storage->delete($import->path);