1 <?php namespace BookStack\Http\Controllers\Images;
3 use BookStack\Entities\Page;
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
50 * @param Request $request
52 * @return \Illuminate\Http\JsonResponse
53 * @throws ImageUploadException
56 public function update(Request $request, $id)
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.
73 public function usage(int $id)
75 $image = $this->imageRepo->getById($id);
76 $this->checkImagePermission($image);
78 $pages = Page::visible()->where('html', 'like', '%' . $image->url . '%')->get(['id', 'name', 'slug', 'book_id']);
79 foreach ($pages as $page) {
80 $page->url = $page->getUrl();
84 $result = count($pages) > 0 ? $pages : false;
86 return response()->json($result);
90 * Deletes an image and all thumbnail/image files
92 * @return \Illuminate\Http\JsonResponse
95 public function destroy($id)
97 $image = $this->imageRepo->getById($id);
98 $this->checkOwnablePermission('image-delete', $image);
99 $this->checkImagePermission($image);
101 $this->imageRepo->destroyImage($image);
102 return response()->json(trans('components.images_deleted'));
106 * Check related page permission and ensure type is drawio or gallery.
107 * @param Image $image
109 protected function checkImagePermission(Image $image)
111 if ($image->type !== 'drawio' && $image->type !== 'gallery') {
112 $this->showPermissionError();
115 $relatedPage = $image->getPage();
117 $this->checkOwnablePermission('page-view', $relatedPage);