NOW MANAGED ON CODEBERG
Images: Updated to create thumbnails at specific events
Dan Browncc0827ff2898
diff --git a/app/Uploads/Controllers/GalleryImageController.php b/app/Uploads/Controllers/GalleryImageController.phpindex 33d3dd74c..02e58faf5 100644--- a/app/Uploads/Controllers/GalleryImageController.php+++ b/app/Uploads/Controllers/GalleryImageController.php@@ -6,6 +6,8 @@ use BookStack\Exceptions\ImageUploadException;use BookStack\Http\Controller;use BookStack\Uploads\ImageRepo;use Illuminate\Http\Request;+use Illuminate\Support\Facades\App;+use Illuminate\Support\Facades\Log;use Illuminate\Validation\ValidationException;class GalleryImageController extends Controllerdiff --git a/app/Uploads/Controllers/ImageController.php b/app/Uploads/Controllers/ImageController.phpindex 2c611c515..0f88b376e 100644--- a/app/Uploads/Controllers/ImageController.php+++ b/app/Uploads/Controllers/ImageController.php@@ -44,7 +44,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 +52,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 +97,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,diff --git a/app/Uploads/Controllers/ImageGalleryApiController.php b/app/Uploads/Controllers/ImageGalleryApiController.phpindex 0d35d2905..efdff5be4 100644--- a/app/Uploads/Controllers/ImageGalleryApiController.php+++ b/app/Uploads/Controllers/ImageGalleryApiController.php@@ -130,7 +130,7 @@ class ImageGalleryApiController extends ApiController*/protected function formatForSingleResponse(Image $image): array{- $this->imageRepo->loadThumbs($image);+ $this->imageRepo->loadThumbs($image, false);$data = $image->toArray();$data['created_by'] = $image->createdBy;$data['updated_by'] = $image->updatedBy;diff --git a/app/Uploads/Image.php b/app/Uploads/Image.phpindex 9f571693a..5291dac05 100644--- a/app/Uploads/Image.php+++ b/app/Uploads/Image.php@@ -45,11 +45,11 @@ class Image extends Model}/**- * Get a thumbnail for this image.+ * Get an (already existing) thumbnail for this image.** @throws \Exception*/- public function getThumb(?int $width, ?int $height, bool $keepRatio = false): string+ public function getThumb(?int $width, ?int $height, bool $keepRatio = false): ?string{return app()->make(ImageService::class)->getThumbnail($this, $width, $height, $keepRatio);}diff --git a/app/Uploads/ImageRepo.php b/app/Uploads/ImageRepo.phpindex 5507933f3..8a770da78 100644--- a/app/Uploads/ImageRepo.php+++ b/app/Uploads/ImageRepo.php@@ -29,14 +29,14 @@ class ImageRepo* Execute a paginated query, returning in a standard format.* Also runs the query through the restriction system.*/- private function returnPaginated($query, $page = 1, $pageSize = 24): array+ private function returnPaginated(Builder $query, int $page = 1, int $pageSize = 24): array{$images = $query->orderBy('created_at', 'desc')->skip($pageSize * ($page - 1))->take($pageSize + 1)->get();$hasMore = count($images) > $pageSize;$returnImages = $images->take($pageSize);$returnImages->each(function (Image $image) {- $this->loadThumbs($image);+ $this->loadThumbs($image, false);});return [@@ -119,7 +119,7 @@ class ImageRepo$image = $this->imageService->saveNewFromUpload($uploadFile, $type, $uploadedTo, $resizeWidth, $resizeHeight, $keepRatio);if ($type !== 'system') {- $this->loadThumbs($image);+ $this->loadThumbs($image, true);}return $image;@@ -133,7 +133,7 @@ class ImageRepopublic function saveNewFromData(string $imageName, string $imageData, string $type, int $uploadedTo = 0): Image{$image = $this->imageService->saveNew($imageName, $imageData, $type, $uploadedTo);- $this->loadThumbs($image);+ $this->loadThumbs($image, true);return $image;}@@ -160,7 +160,7 @@ class ImageRepo$image->fill($updateDetails);$image->updated_by = user()->id;$image->save();- $this->loadThumbs($image);+ $this->loadThumbs($image, false);return $image;}@@ -179,6 +179,7 @@ class ImageRepo$image->updated_by = user()->id;$image->touch();$image->save();+$this->imageService->replaceExistingFromUpload($image->path, $image->type, $file);$this->loadThumbs($image, true);}@@ -215,11 +216,11 @@ class ImageRepo/*** Load thumbnails onto an image object.*/- public function loadThumbs(Image $image, bool $forceCreate = false): void+ public function loadThumbs(Image $image, bool $shouldCreate): void{$image->setAttribute('thumbs', [- 'gallery' => $this->getThumbnail($image, 150, 150, false, $forceCreate),- 'display' => $this->getThumbnail($image, 1680, null, true, $forceCreate),+ 'gallery' => $this->getThumbnail($image, 150, 150, false, $shouldCreate),+ 'display' => $this->getThumbnail($image, 1680, null, true, $shouldCreate),]);}@@ -228,10 +229,10 @@ class ImageRepo* If $keepRatio is true only the width will be used.* Checks the cache then storage to avoid creating / accessing the filesystem on every check.*/- protected function getThumbnail(Image $image, ?int $width, ?int $height, bool $keepRatio, bool $forceCreate): ?string+ protected function getThumbnail(Image $image, ?int $width, ?int $height, bool $keepRatio, bool $shouldCreate): ?string{try {- return $this->imageService->getThumbnail($image, $width, $height, $keepRatio, $forceCreate);+ return $this->imageService->getThumbnail($image, $width, $height, $keepRatio, $shouldCreate);} catch (Exception $exception) {return null;}diff --git a/app/Uploads/ImageService.php b/app/Uploads/ImageService.phpindex 66596a57f..5db8defd3 100644--- a/app/Uploads/ImageService.php+++ b/app/Uploads/ImageService.php@@ -259,7 +259,7 @@ class ImageService$initialHeader = substr($imageData, 0, strpos($imageData, 'IDAT'));- return strpos($initialHeader, 'acTL') !== false;+ return str_contains($initialHeader, 'acTL');}/**@@ -268,9 +268,8 @@ class ImageService* Checks the cache then storage to avoid creating / accessing the filesystem on every check.** @throws Exception- * @throws InvalidArgumentException*/- public function getThumbnail(Image $image, ?int $width, ?int $height, bool $keepRatio = false, bool $forceCreate = false): string+ public function getThumbnail(Image $image, ?int $width, ?int $height, bool $keepRatio = false, bool $shouldCreate = false): ?string{// Do not resize GIF images where we're not croppingif ($keepRatio && $this->isGif($image)) {@@ -285,13 +284,13 @@ class ImageService// Return path if in cache$cachedThumbPath = $this->cache->get($thumbCacheKey);- if ($cachedThumbPath && !$forceCreate) {+ if ($cachedThumbPath && !$shouldCreate) {return $this->getPublicUrl($cachedThumbPath);}// If thumbnail has already been generated, serve that and cache path$storage = $this->getStorageDisk($image->type);- if (!$forceCreate && $storage->exists($this->adjustPathForStorageDisk($thumbFilePath, $image->type))) {+ if (!$shouldCreate && $storage->exists($this->adjustPathForStorageDisk($thumbFilePath, $image->type))) {$this->cache->put($thumbCacheKey, $thumbFilePath, 60 * 60 * 72);return $this->getPublicUrl($thumbFilePath);@@ -306,6 +305,10 @@ class ImageServicereturn $this->getPublicUrl($image->path);}+ if (!$shouldCreate) {+ return null;+ }+// If not in cache and thumbnail does not exist, generate thumb and cache path$thumbData = $this->resizeImage($imageData, $width, $height, $keepRatio);$this->saveImageDataInPublicSpace($storage, $this->adjustPathForStorageDisk($thumbFilePath, $image->type), $thumbData);