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;
11 use Illuminate\Filesystem\Filesystem as File;
12 use Illuminate\Http\Request;
13 use Illuminate\Validation\ValidationException;
15 class ImageController extends Controller
22 * ImageController constructor.
24 public function __construct(Image $image, File $file, ImageRepo $imageRepo)
26 $this->image = $image;
28 $this->imageRepo = $imageRepo;
32 * Provide an image file from storage.
34 * @throws NotFoundException
36 public function showImage(string $path)
38 $path = storage_path('uploads/images/' . $path);
39 if (!file_exists($path)) {
40 throw (new NotFoundException(trans('errors.image_not_found')))
41 ->setSubtitle(trans('errors.image_not_found_subtitle'))
42 ->setDetails(trans('errors.image_not_found_details'));
45 return response()->file($path);
49 * Update image details.
51 * @throws ImageUploadException
52 * @throws ValidationException
54 public function update(Request $request, string $id)
56 $this->validate($request, [
57 'name' => 'required|min:2|string',
60 $image = $this->imageRepo->getById($id);
61 $this->checkImagePermission($image);
62 $this->checkOwnablePermission('image-update', $image);
64 $image = $this->imageRepo->updateImageDetails($image, $request->all());
66 $this->imageRepo->loadThumbs($image);
68 return view('components.image-manager-form', [
70 'dependantPages' => null,
75 * Get the form for editing the given image.
79 public function edit(Request $request, string $id)
81 $image = $this->imageRepo->getById($id);
82 $this->checkImagePermission($image);
84 if ($request->has('delete')) {
85 $dependantPages = $this->imageRepo->getPagesUsingImage($image);
88 $this->imageRepo->loadThumbs($image);
90 return view('components.image-manager-form', [
92 'dependantPages' => $dependantPages ?? null,
97 * Deletes an image and all thumbnail/image files.
101 public function destroy(string $id)
103 $image = $this->imageRepo->getById($id);
104 $this->checkOwnablePermission('image-delete', $image);
105 $this->checkImagePermission($image);
107 $this->imageRepo->destroyImage($image);
113 * Check related page permission and ensure type is drawio or gallery.
115 protected function checkImagePermission(Image $image)
117 if ($image->type !== 'drawio' && $image->type !== 'gallery') {
118 $this->showPermissionError();
121 $relatedPage = $image->getPage();
123 $this->checkOwnablePermission('page-view', $relatedPage);