1 <?php namespace BookStack\Http\Controllers\Images;
3 use BookStack\Exceptions\ImageUploadException;
4 use BookStack\Exceptions\NotFoundException;
5 use BookStack\Http\Controllers\Controller;
6 use BookStack\Uploads\Image;
7 use BookStack\Uploads\ImageRepo;
9 use Illuminate\Filesystem\Filesystem as File;
10 use Illuminate\Http\Request;
11 use Illuminate\Validation\ValidationException;
13 class ImageController extends Controller
20 * ImageController constructor.
22 public function __construct(Image $image, File $file, ImageRepo $imageRepo)
24 $this->image = $image;
26 $this->imageRepo = $imageRepo;
30 * Provide an image file from storage.
31 * @throws NotFoundException
33 public function showImage(string $path)
35 $path = storage_path('uploads/images/' . $path);
36 if (!file_exists($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 response()->file($path);
47 * 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);
64 return view('components.image-manager-form', [
66 'dependantPages' => null,
71 * Get the form for editing the given image.
74 public function edit(Request $request, string $id)
76 $image = $this->imageRepo->getById($id);
77 $this->checkImagePermission($image);
79 if ($request->has('delete')) {
80 $dependantPages = $this->imageRepo->getPagesUsingImage($image);
83 $this->imageRepo->loadThumbs($image);
84 return view('components.image-manager-form', [
86 'dependantPages' => $dependantPages ?? null,
91 * Deletes an image and all thumbnail/image files
94 public function destroy(string $id)
96 $image = $this->imageRepo->getById($id);
97 $this->checkOwnablePermission('image-delete', $image);
98 $this->checkImagePermission($image);
100 $this->imageRepo->destroyImage($image);
105 * Check related page permission and ensure type is drawio or gallery.
107 protected function checkImagePermission(Image $image)
109 if ($image->type !== 'drawio' && $image->type !== 'gallery') {
110 $this->showPermissionError();
113 $relatedPage = $image->getPage();
115 $this->checkOwnablePermission('page-view', $relatedPage);