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;
15 class ImageController extends Controller
17 public function __construct(
18 protected ImageRepo $imageRepo,
19 protected ImageService $imageService
24 * Provide an image file from storage.
26 * @throws NotFoundException
28 public function showImage(string $path)
30 if (!$this->imageService->pathAccessibleInLocalSecure($path)) {
31 throw (new NotFoundException(trans('errors.image_not_found')))
32 ->setSubtitle(trans('errors.image_not_found_subtitle'))
33 ->setDetails(trans('errors.image_not_found_details'));
36 return $this->imageService->streamImageFromStorageResponse('gallery', $path);
40 * Update image details.
42 public function update(Request $request, string $id)
44 $data = $this->validate($request, [
45 'name' => ['required', 'min:2', 'string'],
48 $image = $this->imageRepo->getById($id);
49 $this->checkImagePermission($image);
50 $this->checkOwnablePermission('image-update', $image);
52 $image = $this->imageRepo->updateImageDetails($image, $data);
54 return view('pages.parts.image-manager-form', [
56 'dependantPages' => null,
61 * Update the file for an existing image.
63 public function updateFile(Request $request, string $id)
65 $this->validate($request, [
66 'file' => ['required', 'file', ...$this->getImageValidationRules()],
69 $image = $this->imageRepo->getById($id);
70 $this->checkImagePermission($image);
71 $this->checkOwnablePermission('image-update', $image);
72 $file = $request->file('file');
74 new OutOfMemoryHandler(function () {
75 return $this->jsonError(trans('errors.image_upload_memory_limit'));
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);