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\Filesystem\Filesystem as File;
13 use Illuminate\Filesystem\FilesystemAdapter;
14 use Illuminate\Http\Request;
15 use Illuminate\Support\Facades\Storage;
16 use Illuminate\Validation\ValidationException;
17 use League\Flysystem\Util;
19 class ImageController extends Controller
24 protected $imageService;
27 * ImageController constructor.
29 public function __construct(Image $image, File $file, ImageRepo $imageRepo, ImageService $imageService)
31 $this->image = $image;
33 $this->imageRepo = $imageRepo;
34 $this->imageService = $imageService;
38 * Provide an image file from storage.
40 * @throws NotFoundException
42 public function showImage(string $path)
44 if (!$this->imageService->pathExistsInLocalSecure($path)) {
45 throw (new NotFoundException(trans('errors.image_not_found')))
46 ->setSubtitle(trans('errors.image_not_found_subtitle'))
47 ->setDetails(trans('errors.image_not_found_details'));
50 return $this->imageService->streamImageFromStorageResponse('gallery', $path);
54 * Update image details.
56 * @throws ImageUploadException
57 * @throws ValidationException
59 public function update(Request $request, string $id)
61 $this->validate($request, [
62 'name' => 'required|min:2|string',
65 $image = $this->imageRepo->getById($id);
66 $this->checkImagePermission($image);
67 $this->checkOwnablePermission('image-update', $image);
69 $image = $this->imageRepo->updateImageDetails($image, $request->all());
71 $this->imageRepo->loadThumbs($image);
73 return view('pages.parts.image-manager-form', [
75 'dependantPages' => null,
80 * Get the form for editing the given image.
84 public function edit(Request $request, string $id)
86 $image = $this->imageRepo->getById($id);
87 $this->checkImagePermission($image);
89 if ($request->has('delete')) {
90 $dependantPages = $this->imageRepo->getPagesUsingImage($image);
93 $this->imageRepo->loadThumbs($image);
95 return view('pages.parts.image-manager-form', [
97 'dependantPages' => $dependantPages ?? null,
102 * Deletes an image and all thumbnail/image files.
106 public function destroy(string $id)
108 $image = $this->imageRepo->getById($id);
109 $this->checkOwnablePermission('image-delete', $image);
110 $this->checkImagePermission($image);
112 $this->imageRepo->destroyImage($image);
118 * Check related page permission and ensure type is drawio or gallery.
120 protected function checkImagePermission(Image $image)
122 if ($image->type !== 'drawio' && $image->type !== 'gallery') {
123 $this->showPermissionError();
126 $relatedPage = $image->getPage();
128 $this->checkOwnablePermission('page-view', $relatedPage);