1 <?php namespace BookStack\Http\Controllers\Images;
3 use BookStack\Entities\Repos\EntityRepo;
4 use BookStack\Exceptions\ImageUploadException;
5 use BookStack\Http\Controllers\Controller;
6 use BookStack\Repos\PageRepo;
7 use BookStack\Uploads\Image;
8 use BookStack\Uploads\ImageRepo;
9 use Illuminate\Filesystem\Filesystem as File;
10 use Illuminate\Http\Request;
12 class ImageController extends Controller
19 * ImageController constructor.
22 * @param ImageRepo $imageRepo
24 public function __construct(Image $image, File $file, ImageRepo $imageRepo)
26 $this->image = $image;
28 $this->imageRepo = $imageRepo;
29 parent::__construct();
33 * Provide an image file from storage.
37 public function showImage(string $path)
39 $path = storage_path('uploads/images/' . $path);
40 if (!file_exists($path)) {
44 return response()->file($path);
49 * Update image details
51 * @param Request $request
52 * @return \Illuminate\Http\JsonResponse
53 * @throws ImageUploadException
56 public function update($id, Request $request)
58 $this->validate($request, [
59 'name' => 'required|min:2|string'
62 $image = $this->imageRepo->getById($id);
63 $this->checkImagePermission($image);
64 $this->checkOwnablePermission('image-update', $image);
66 $image = $this->imageRepo->updateImageDetails($image, $request->all());
67 return response()->json($image);
71 * Show the usage of an image on pages.
72 * @param \BookStack\Entities\Repos\EntityRepo $entityRepo
74 * @return \Illuminate\Http\JsonResponse
76 public function usage(EntityRepo $entityRepo, $id)
78 $image = $this->imageRepo->getById($id);
79 $this->checkImagePermission($image);
80 $pageSearch = $entityRepo->searchForImage($image->url);
81 return response()->json($pageSearch);
85 * Deletes an image and all thumbnail/image files
87 * @return \Illuminate\Http\JsonResponse
90 public function destroy($id)
92 $image = $this->imageRepo->getById($id);
93 $this->checkOwnablePermission('image-delete', $image);
94 $this->checkImagePermission($image);
96 $this->imageRepo->destroyImage($image);
97 return response()->json(trans('components.images_deleted'));
101 * Check related page permission and ensure type is drawio or gallery.
102 * @param Image $image
104 protected function checkImagePermission(Image $image)
106 if ($image->type !== 'drawio' && $image->type !== 'gallery') {
107 $this->showPermissionError();
110 $relatedPage = $image->getPage();
112 $this->checkOwnablePermission('page-view', $relatedPage);