1 <?php namespace BookStack\Http\Controllers\Images;
3 use BookStack\Exceptions\ImageUploadException;
4 use BookStack\Http\Controllers\Controller;
5 use BookStack\Uploads\Image;
6 use BookStack\Uploads\ImageRepo;
8 use Illuminate\Filesystem\Filesystem as File;
9 use Illuminate\Http\Request;
10 use Illuminate\Validation\ValidationException;
12 class ImageController extends Controller
19 * ImageController constructor.
21 public function __construct(Image $image, File $file, ImageRepo $imageRepo)
23 $this->image = $image;
25 $this->imageRepo = $imageRepo;
29 * Provide an image file from storage.
31 public function showImage(string $path)
33 $path = storage_path('uploads/images/' . $path);
34 if (!file_exists($path)) {
38 return response()->file($path);
43 * Update image details
44 * @throws ImageUploadException
45 * @throws ValidationException
47 public function update(Request $request, string $id)
49 $this->validate($request, [
50 'name' => 'required|min:2|string'
53 $image = $this->imageRepo->getById($id);
54 $this->checkImagePermission($image);
55 $this->checkOwnablePermission('image-update', $image);
57 $image = $this->imageRepo->updateImageDetails($image, $request->all());
59 $this->imageRepo->loadThumbs($image);
60 return view('components.image-manager-form', [
62 'dependantPages' => null,
67 * Get the form for editing the given image.
70 public function edit(Request $request, string $id)
72 $image = $this->imageRepo->getById($id);
73 $this->checkImagePermission($image);
75 if ($request->has('delete')) {
76 $dependantPages = $this->imageRepo->getPagesUsingImage($image);
79 $this->imageRepo->loadThumbs($image);
80 return view('components.image-manager-form', [
82 'dependantPages' => $dependantPages ?? null,
87 * Deletes an image and all thumbnail/image files
90 public function destroy(string $id)
92 $image = $this->imageRepo->getById($id);
93 $this->checkOwnablePermission('image-delete', $image);
94 $this->checkImagePermission($image);
96 $this->imageRepo->destroyImage($image);
101 * Check related page permission and ensure type is drawio or gallery.
103 protected function checkImagePermission(Image $image)
105 if ($image->type !== 'drawio' && $image->type !== 'gallery') {
106 $this->showPermissionError();
109 $relatedPage = $image->getPage();
111 $this->checkOwnablePermission('page-view', $relatedPage);