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;
12 use Illuminate\Http\Request;
13 use Illuminate\Validation\ValidationException;
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 * @throws ImageUploadException
43 * @throws ValidationException
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');
78 $this->imageRepo->updateImageFile($image, $file);
79 } catch (ImageUploadException $exception) {
80 return $this->jsonError($exception->getMessage());
87 * Get the form for editing the given image.
91 public function edit(Request $request, string $id)
93 $image = $this->imageRepo->getById($id);
94 $this->checkImagePermission($image);
96 if ($request->has('delete')) {
97 $dependantPages = $this->imageRepo->getPagesUsingImage($image);
100 $this->imageRepo->loadThumbs($image, false);
102 return view('pages.parts.image-manager-form', [
104 'dependantPages' => $dependantPages ?? null,
109 * Deletes an image and all thumbnail/image files.
113 public function destroy(string $id)
115 $image = $this->imageRepo->getById($id);
116 $this->checkOwnablePermission('image-delete', $image);
117 $this->checkImagePermission($image);
119 $this->imageRepo->destroyImage($image);
125 * Check related page permission and ensure type is drawio or gallery.
127 protected function checkImagePermission(Image $image)
129 if ($image->type !== 'drawio' && $image->type !== 'gallery') {
130 $this->showPermissionError();
133 $relatedPage = $image->getPage();
135 $this->checkOwnablePermission('page-view', $relatedPage);