3 namespace BookStack\Uploads\Controllers;
5 use BookStack\Exceptions\ImageUploadException;
6 use BookStack\Exceptions\NotFoundException;
7 use BookStack\Http\Controllers\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 protected ImageRepo $imageRepo;
18 protected ImageService $imageService;
20 public function __construct(ImageRepo $imageRepo, ImageService $imageService)
22 $this->imageRepo = $imageRepo;
23 $this->imageService = $imageService;
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 * @throws ImageUploadException
46 * @throws ValidationException
48 public function update(Request $request, string $id)
50 $this->validate($request, [
51 'name' => ['required', 'min:2', 'string'],
54 $image = $this->imageRepo->getById($id);
55 $this->checkImagePermission($image);
56 $this->checkOwnablePermission('image-update', $image);
58 $image = $this->imageRepo->updateImageDetails($image, $request->all());
60 $this->imageRepo->loadThumbs($image);
62 return view('pages.parts.image-manager-form', [
64 'dependantPages' => null,
69 * Get the form for editing the given image.
73 public function edit(Request $request, string $id)
75 $image = $this->imageRepo->getById($id);
76 $this->checkImagePermission($image);
78 if ($request->has('delete')) {
79 $dependantPages = $this->imageRepo->getPagesUsingImage($image);
82 $this->imageRepo->loadThumbs($image);
84 return view('pages.parts.image-manager-form', [
86 'dependantPages' => $dependantPages ?? null,
91 * Deletes an image and all thumbnail/image files.
95 public function destroy(string $id)
97 $image = $this->imageRepo->getById($id);
98 $this->checkOwnablePermission('image-delete', $image);
99 $this->checkImagePermission($image);
101 $this->imageRepo->destroyImage($image);
107 * Check related page permission and ensure type is drawio or gallery.
109 protected function checkImagePermission(Image $image)
111 if ($image->type !== 'drawio' && $image->type !== 'gallery') {
112 $this->showPermissionError();
115 $relatedPage = $image->getPage();
117 $this->checkOwnablePermission('page-view', $relatedPage);