]> BookStack Code Mirror - bookstack/commitdiff
ZIP Exports: Fixed reference handling for images development
authorDan Brown <redacted>
Mon, 11 Aug 2025 13:19:48 +0000 (14:19 +0100)
committerDan Brown <redacted>
Mon, 11 Aug 2025 13:19:48 +0000 (14:19 +0100)
Recent changes could mean missed references for images in non-page
locations. This fixes that, and tries to ensure images are used if we
already have a page-based image as part of the ZIP, otherwise ensure we
have a page as part of the export to attach the image to.

app/Exports/ZipExports/ZipExportReferences.php

index b9f356a6a5ea9e87c78695d50bcf1342d0d4509a..b21248ffd8c4d5b4e65c37d43cc5ac5835da43d7 100644 (file)
@@ -17,17 +17,17 @@ use BookStack\Uploads\Image;
 
 class ZipExportReferences
 {
-    /** @var ZipExportPage[] */
+    /** @var array<int, ZipExportPage> */
     protected array $pages = [];
-    /** @var ZipExportChapter[] */
+    /** @var array<int, ZipExportChapter> */
     protected array $chapters = [];
-    /** @var ZipExportBook[] */
+    /** @var array<int, ZipExportBook> */
     protected array $books = [];
 
-    /** @var ZipExportAttachment[] */
+    /** @var array<int, ZipExportAttachment> */
     protected array $attachments = [];
 
-    /** @var ZipExportImage[] */
+    /** @var array<int, ZipExportImage> */
     protected array $images = [];
 
     public function __construct(
@@ -134,11 +134,12 @@ class ZipExportReferences
 
             // Find and include images if in visibility
             $page = $model->getPage();
-            if ($page && userCan('view', $page) && $exportModel instanceof ZipExportPage) {
+            $pageExportModel = $this->pages[$page->id] ?? ($exportModel instanceof ZipExportPage ? $exportModel : null);
+            if (isset($this->images[$model->id]) || ($page && $pageExportModel && userCan('view', $page))) {
                 if (!isset($this->images[$model->id])) {
                     $exportImage = ZipExportImage::fromModel($model, $files);
                     $this->images[$model->id] = $exportImage;
-                    $exportModel->images[] = $exportImage;
+                    $pageExportModel->images[] = $exportImage;
                 }
                 return "[[bsexport:image:{$model->id}]]";
             }