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 $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, $request->all());
57 $this->imageRepo->loadThumbs($image);
59 return view('pages.parts.image-manager-form', [
61 'dependantPages' => null,
66 * Update the file for an existing image.
68 public function updateFile(Request $request, string $id)
70 $this->validate($request, [
71 'file' => ['required', 'file', ...$this->getImageValidationRules()],
74 $image = $this->imageRepo->getById($id);
75 $this->checkImagePermission($image);
76 $this->checkOwnablePermission('image-update', $image);
77 $file = $request->file('file');
80 $this->imageRepo->updateImageFile($image, $file);
81 } catch (ImageUploadException $exception) {
82 return $this->jsonError($exception->getMessage());
89 * Get the form for editing the given image.
93 public function edit(Request $request, string $id)
95 $image = $this->imageRepo->getById($id);
96 $this->checkImagePermission($image);
98 if ($request->has('delete')) {
99 $dependantPages = $this->imageRepo->getPagesUsingImage($image);
102 $this->imageRepo->loadThumbs($image);
104 return view('pages.parts.image-manager-form', [
106 'dependantPages' => $dependantPages ?? null,
111 * Deletes an image and all thumbnail/image files.
115 public function destroy(string $id)
117 $image = $this->imageRepo->getById($id);
118 $this->checkOwnablePermission('image-delete', $image);
119 $this->checkImagePermission($image);
121 $this->imageRepo->destroyImage($image);
127 * Check related page permission and ensure type is drawio or gallery.
129 protected function checkImagePermission(Image $image)
131 if ($image->type !== 'drawio' && $image->type !== 'gallery') {
132 $this->showPermissionError();
135 $relatedPage = $image->getPage();
137 $this->checkOwnablePermission('page-view', $relatedPage);