]> BookStack Code Mirror - bookstack/blobdiff - app/Uploads/Controllers/ImageController.php
Thumbnails: Added OOM handling and regen endpoint
[bookstack] / app / Uploads / Controllers / ImageController.php
index 2c611c515bff8746f87c868c0d5ec5f3dce1c0e4..edf1533fad7d8a81c41b6bd5691409486c7dc945 100644 (file)
@@ -8,6 +8,7 @@ use BookStack\Http\Controller;
 use BookStack\Uploads\Image;
 use BookStack\Uploads\ImageRepo;
 use BookStack\Uploads\ImageService;
+use BookStack\Util\OutOfMemoryHandler;
 use Exception;
 use Illuminate\Http\Request;
 use Illuminate\Validation\ValidationException;
@@ -44,7 +45,7 @@ class ImageController extends Controller
      */
     public function update(Request $request, string $id)
     {
-        $this->validate($request, [
+        $data = $this->validate($request, [
             'name' => ['required', 'min:2', 'string'],
         ]);
 
@@ -52,9 +53,7 @@ class ImageController extends Controller
         $this->checkImagePermission($image);
         $this->checkOwnablePermission('image-update', $image);
 
-        $image = $this->imageRepo->updateImageDetails($image, $request->all());
-
-        $this->imageRepo->loadThumbs($image);
+        $image = $this->imageRepo->updateImageDetails($image, $data);
 
         return view('pages.parts.image-manager-form', [
             'image'          => $image,
@@ -99,7 +98,7 @@ class ImageController extends Controller
             $dependantPages = $this->imageRepo->getPagesUsingImage($image);
         }
 
-        $this->imageRepo->loadThumbs($image);
+        $this->imageRepo->loadThumbs($image, false);
 
         return view('pages.parts.image-manager-form', [
             'image'          => $image,
@@ -123,6 +122,24 @@ class ImageController extends Controller
         return response('');
     }
 
+    /**
+     * Rebuild the thumbnails for the given image.
+     */
+    public function rebuildThumbnails(string $id)
+    {
+        $image = $this->imageRepo->getById($id);
+        $this->checkImagePermission($image);
+        $this->checkOwnablePermission('image-update', $image);
+
+        new OutOfMemoryHandler(function () {
+            return $this->jsonError(trans('errors.image_thumbnail_memory_limit'));
+        });
+
+        $this->imageRepo->loadThumbs($image, true);
+
+        return response(trans('components.image_rebuild_thumbs_success'));
+    }
+
     /**
      * Check related page permission and ensure type is drawio or gallery.
      */