]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Images/ImageController.php
Added 404 response for non-existing setting categories
[bookstack] / app / Http / Controllers / Images / ImageController.php
1 <?php
2
3 namespace BookStack\Http\Controllers\Images;
4
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;
11 use Exception;
12 use Illuminate\Http\Request;
13 use Illuminate\Validation\ValidationException;
14
15 class ImageController extends Controller
16 {
17     protected $imageRepo;
18     protected $imageService;
19
20     /**
21      * ImageController constructor.
22      */
23     public function __construct(ImageRepo $imageRepo, ImageService $imageService)
24     {
25         $this->imageRepo = $imageRepo;
26         $this->imageService = $imageService;
27     }
28
29     /**
30      * Provide an image file from storage.
31      *
32      * @throws NotFoundException
33      */
34     public function showImage(string $path)
35     {
36         if (!$this->imageService->pathExistsInLocalSecure($path)) {
37             throw (new NotFoundException(trans('errors.image_not_found')))
38                 ->setSubtitle(trans('errors.image_not_found_subtitle'))
39                 ->setDetails(trans('errors.image_not_found_details'));
40         }
41
42         return $this->imageService->streamImageFromStorageResponse('gallery', $path);
43     }
44
45     /**
46      * Update image details.
47      *
48      * @throws ImageUploadException
49      * @throws ValidationException
50      */
51     public function update(Request $request, string $id)
52     {
53         $this->validate($request, [
54             'name' => ['required', 'min:2', 'string'],
55         ]);
56
57         $image = $this->imageRepo->getById($id);
58         $this->checkImagePermission($image);
59         $this->checkOwnablePermission('image-update', $image);
60
61         $image = $this->imageRepo->updateImageDetails($image, $request->all());
62
63         $this->imageRepo->loadThumbs($image);
64
65         return view('pages.parts.image-manager-form', [
66             'image'          => $image,
67             'dependantPages' => null,
68         ]);
69     }
70
71     /**
72      * Get the form for editing the given image.
73      *
74      * @throws Exception
75      */
76     public function edit(Request $request, string $id)
77     {
78         $image = $this->imageRepo->getById($id);
79         $this->checkImagePermission($image);
80
81         if ($request->has('delete')) {
82             $dependantPages = $this->imageRepo->getPagesUsingImage($image);
83         }
84
85         $this->imageRepo->loadThumbs($image);
86
87         return view('pages.parts.image-manager-form', [
88             'image'          => $image,
89             'dependantPages' => $dependantPages ?? null,
90         ]);
91     }
92
93     /**
94      * Deletes an image and all thumbnail/image files.
95      *
96      * @throws Exception
97      */
98     public function destroy(string $id)
99     {
100         $image = $this->imageRepo->getById($id);
101         $this->checkOwnablePermission('image-delete', $image);
102         $this->checkImagePermission($image);
103
104         $this->imageRepo->destroyImage($image);
105
106         return response('');
107     }
108
109     /**
110      * Check related page permission and ensure type is drawio or gallery.
111      */
112     protected function checkImagePermission(Image $image)
113     {
114         if ($image->type !== 'drawio' && $image->type !== 'gallery') {
115             $this->showPermissionError();
116         }
117
118         $relatedPage = $image->getPage();
119         if ($relatedPage) {
120             $this->checkOwnablePermission('page-view', $relatedPage);
121         }
122     }
123 }