3 namespace BookStack\Uploads\Controllers;
5 use BookStack\Exceptions\ImageUploadException;
6 use BookStack\Exceptions\NotFoundException;
7 use BookStack\Http\Controller;
8 use BookStack\Uploads\Image;
9 use BookStack\Uploads\ImageRepo;
10 use BookStack\Uploads\ImageService;
11 use BookStack\Util\OutOfMemoryHandler;
13 use Illuminate\Http\Request;
14 use Illuminate\Validation\ValidationException;
16 class ImageController extends Controller
18 public function __construct(
19 protected ImageRepo $imageRepo,
20 protected ImageService $imageService
25 * Provide an image file from storage.
27 * @throws NotFoundException
29 public function showImage(string $path)
31 if (!$this->imageService->pathAccessibleInLocalSecure($path)) {
32 throw (new NotFoundException(trans('errors.image_not_found')))
33 ->setSubtitle(trans('errors.image_not_found_subtitle'))
34 ->setDetails(trans('errors.image_not_found_details'));
37 return $this->imageService->streamImageFromStorageResponse('gallery', $path);
41 * Update image details.
43 * @throws ImageUploadException
44 * @throws ValidationException
46 public function update(Request $request, string $id)
48 $data = $this->validate($request, [
49 'name' => ['required', 'min:2', 'string'],
52 $image = $this->imageRepo->getById($id);
53 $this->checkImagePermission($image);
54 $this->checkOwnablePermission('image-update', $image);
56 $image = $this->imageRepo->updateImageDetails($image, $data);
58 return view('pages.parts.image-manager-form', [
60 'dependantPages' => null,
65 * Update the file for an existing image.
67 public function updateFile(Request $request, string $id)
69 $this->validate($request, [
70 'file' => ['required', 'file', ...$this->getImageValidationRules()],
73 $image = $this->imageRepo->getById($id);
74 $this->checkImagePermission($image);
75 $this->checkOwnablePermission('image-update', $image);
76 $file = $request->file('file');
79 $this->imageRepo->updateImageFile($image, $file);
80 } catch (ImageUploadException $exception) {
81 return $this->jsonError($exception->getMessage());
88 * Get the form for editing the given image.
92 public function edit(Request $request, string $id)
94 $image = $this->imageRepo->getById($id);
95 $this->checkImagePermission($image);
97 if ($request->has('delete')) {
98 $dependantPages = $this->imageRepo->getPagesUsingImage($image);
101 $this->imageRepo->loadThumbs($image, false);
103 return view('pages.parts.image-manager-form', [
105 'dependantPages' => $dependantPages ?? null,
110 * Deletes an image and all thumbnail/image files.
114 public function destroy(string $id)
116 $image = $this->imageRepo->getById($id);
117 $this->checkOwnablePermission('image-delete', $image);
118 $this->checkImagePermission($image);
120 $this->imageRepo->destroyImage($image);
126 * Rebuild the thumbnails for the given image.
128 public function rebuildThumbnails(string $id)
130 $image = $this->imageRepo->getById($id);
131 $this->checkImagePermission($image);
132 $this->checkOwnablePermission('image-update', $image);
134 new OutOfMemoryHandler(function () {
135 return $this->jsonError(trans('errors.image_thumbnail_memory_limit'));
138 $this->imageRepo->loadThumbs($image, true);
140 return response(trans('components.image_rebuild_thumbs_success'));
144 * Check related page permission and ensure type is drawio or gallery.
146 protected function checkImagePermission(Image $image)
148 if ($image->type !== 'drawio' && $image->type !== 'gallery') {
149 $this->showPermissionError();
152 $relatedPage = $image->getPage();
154 $this->checkOwnablePermission('page-view', $relatedPage);