3 namespace BookStack\Http\Controllers\Images;
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
18 protected $imageService;
21 * ImageController constructor.
23 public function __construct(ImageRepo $imageRepo, ImageService $imageService)
25 $this->imageRepo = $imageRepo;
26 $this->imageService = $imageService;
30 * Provide an image file from storage.
32 * @throws NotFoundException
34 public function showImage(string $path)
36 if (!$this->imageService->pathExistsInLocalSecure($path)) {
37 throw (new NotFoundException(trans('errors.image_not_found')))
38 ->setSubtitle(trans('errors.image_not_found_subtitle'))
39 ->setDetails(trans('errors.image_not_found_details'));
42 return $this->imageService->streamImageFromStorageResponse('gallery', $path);
46 * Update image details.
48 * @throws ImageUploadException
49 * @throws ValidationException
51 public function update(Request $request, string $id)
53 $this->validate($request, [
54 'name' => ['required', 'min:2', 'string'],
57 $image = $this->imageRepo->getById($id);
58 $this->checkImagePermission($image);
59 $this->checkOwnablePermission('image-update', $image);
61 $image = $this->imageRepo->updateImageDetails($image, $request->all());
63 $this->imageRepo->loadThumbs($image);
65 return view('pages.parts.image-manager-form', [
67 'dependantPages' => null,
72 * Get the form for editing the given image.
76 public function edit(Request $request, string $id)
78 $image = $this->imageRepo->getById($id);
79 $this->checkImagePermission($image);
81 if ($request->has('delete')) {
82 $dependantPages = $this->imageRepo->getPagesUsingImage($image);
85 $this->imageRepo->loadThumbs($image);
87 return view('pages.parts.image-manager-form', [
89 'dependantPages' => $dependantPages ?? null,
94 * Deletes an image and all thumbnail/image files.
98 public function destroy(string $id)
100 $image = $this->imageRepo->getById($id);
101 $this->checkOwnablePermission('image-delete', $image);
102 $this->checkImagePermission($image);
104 $this->imageRepo->destroyImage($image);
110 * Check related page permission and ensure type is drawio or gallery.
112 protected function checkImagePermission(Image $image)
114 if ($image->type !== 'drawio' && $image->type !== 'gallery') {
115 $this->showPermissionError();
118 $relatedPage = $image->getPage();
120 $this->checkOwnablePermission('page-view', $relatedPage);