3 namespace BookStack\Uploads\Controllers;
5 use BookStack\Exceptions\ImageUploadException;
6 use BookStack\Exceptions\NotFoundException;
7 use BookStack\Exceptions\NotifyException;
8 use BookStack\Http\Controller;
9 use BookStack\Uploads\Image;
10 use BookStack\Uploads\ImageRepo;
11 use BookStack\Uploads\ImageResizer;
12 use BookStack\Uploads\ImageService;
13 use BookStack\Util\OutOfMemoryHandler;
15 use Illuminate\Http\Request;
17 class ImageController extends Controller
19 public function __construct(
20 protected ImageRepo $imageRepo,
21 protected ImageService $imageService,
22 protected ImageResizer $imageResizer,
27 * Provide an image file from storage.
29 * @throws NotFoundException
31 public function showImage(string $path)
33 if (!$this->imageService->pathAccessibleInLocalSecure($path)) {
34 throw (new NotFoundException(trans('errors.image_not_found')))
35 ->setSubtitle(trans('errors.image_not_found_subtitle'))
36 ->setDetails(trans('errors.image_not_found_details'));
39 return $this->imageService->streamImageFromStorageResponse('gallery', $path);
43 * Update image details.
45 public function update(Request $request, string $id)
47 $data = $this->validate($request, [
48 'name' => ['required', 'min:2', 'string'],
51 $image = $this->imageRepo->getById($id);
52 $this->checkImagePermission($image);
53 $this->checkOwnablePermission('image-update', $image);
55 $image = $this->imageRepo->updateImageDetails($image, $data);
57 return view('pages.parts.image-manager-form', [
59 'dependantPages' => null,
64 * Update the file for an existing image.
66 public function updateFile(Request $request, string $id)
68 $this->validate($request, [
69 'file' => ['required', 'file', ...$this->getImageValidationRules()],
72 $image = $this->imageRepo->getById($id);
73 $this->checkImagePermission($image);
74 $this->checkOwnablePermission('image-update', $image);
75 $file = $request->file('file');
77 new OutOfMemoryHandler(function () {
78 return $this->jsonError(trans('errors.image_upload_memory_limit'));
82 $this->imageRepo->updateImageFile($image, $file);
83 } catch (ImageUploadException $exception) {
84 return $this->jsonError($exception->getMessage());
91 * Get the form for editing the given image.
95 public function edit(Request $request, string $id)
97 $image = $this->imageRepo->getById($id);
98 $this->checkImagePermission($image);
100 if ($request->has('delete')) {
101 $dependantPages = $this->imageRepo->getPagesUsingImage($image);
106 'dependantPages' => $dependantPages ?? null,
110 new OutOfMemoryHandler(function () use ($viewData) {
111 $viewData['warning'] = trans('errors.image_thumbnail_memory_limit');
112 return response()->view('pages.parts.image-manager-form', $viewData);
115 $this->imageResizer->loadGalleryThumbnailsForImage($image, false);
117 return view('pages.parts.image-manager-form', $viewData);
121 * Deletes an image and all thumbnail/image files.
125 public function destroy(string $id)
127 $image = $this->imageRepo->getById($id);
128 $this->checkOwnablePermission('image-delete', $image);
129 $this->checkImagePermission($image);
131 $this->imageRepo->destroyImage($image);
137 * Rebuild the thumbnails for the given image.
139 public function rebuildThumbnails(string $id)
141 $image = $this->imageRepo->getById($id);
142 $this->checkImagePermission($image);
143 $this->checkOwnablePermission('image-update', $image);
145 new OutOfMemoryHandler(function () {
146 return $this->jsonError(trans('errors.image_thumbnail_memory_limit'));
149 $this->imageResizer->loadGalleryThumbnailsForImage($image, true);
151 return response(trans('components.image_rebuild_thumbs_success'));
155 * Check related page permission and ensure type is drawio or gallery.
156 * @throws NotifyException
158 protected function checkImagePermission(Image $image): void
160 if ($image->type !== 'drawio' && $image->type !== 'gallery') {
161 $this->showPermissionError();
164 $relatedPage = $image->getPage();
166 $this->checkOwnablePermission('page-view', $relatedPage);