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