]> BookStack Code Mirror - bookstack/blob - app/Uploads/Controllers/ImageController.php
Images: Started refactor of image service
[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 BookStack\Util\OutOfMemoryHandler;
12 use Exception;
13 use Illuminate\Http\Request;
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     public function update(Request $request, string $id)
43     {
44         $data = $this->validate($request, [
45             'name' => ['required', 'min:2', 'string'],
46         ]);
47
48         $image = $this->imageRepo->getById($id);
49         $this->checkImagePermission($image);
50         $this->checkOwnablePermission('image-update', $image);
51
52         $image = $this->imageRepo->updateImageDetails($image, $data);
53
54         return view('pages.parts.image-manager-form', [
55             'image'          => $image,
56             'dependantPages' => null,
57         ]);
58     }
59
60     /**
61      * Update the file for an existing image.
62      */
63     public function updateFile(Request $request, string $id)
64     {
65         $this->validate($request, [
66             'file' => ['required', 'file', ...$this->getImageValidationRules()],
67         ]);
68
69         $image = $this->imageRepo->getById($id);
70         $this->checkImagePermission($image);
71         $this->checkOwnablePermission('image-update', $image);
72         $file = $request->file('file');
73
74         new OutOfMemoryHandler(function () {
75             return $this->jsonError(trans('errors.image_upload_memory_limit'));
76         });
77
78         try {
79             $this->imageRepo->updateImageFile($image, $file);
80         } catch (ImageUploadException $exception) {
81             return $this->jsonError($exception->getMessage());
82         }
83
84         return response('');
85     }
86
87     /**
88      * Get the form for editing the given image.
89      *
90      * @throws Exception
91      */
92     public function edit(Request $request, string $id)
93     {
94         $image = $this->imageRepo->getById($id);
95         $this->checkImagePermission($image);
96
97         if ($request->has('delete')) {
98             $dependantPages = $this->imageRepo->getPagesUsingImage($image);
99         }
100
101         $this->imageRepo->loadThumbs($image, false);
102
103         return view('pages.parts.image-manager-form', [
104             'image'          => $image,
105             'dependantPages' => $dependantPages ?? null,
106         ]);
107     }
108
109     /**
110      * Deletes an image and all thumbnail/image files.
111      *
112      * @throws Exception
113      */
114     public function destroy(string $id)
115     {
116         $image = $this->imageRepo->getById($id);
117         $this->checkOwnablePermission('image-delete', $image);
118         $this->checkImagePermission($image);
119
120         $this->imageRepo->destroyImage($image);
121
122         return response('');
123     }
124
125     /**
126      * Rebuild the thumbnails for the given image.
127      */
128     public function rebuildThumbnails(string $id)
129     {
130         $image = $this->imageRepo->getById($id);
131         $this->checkImagePermission($image);
132         $this->checkOwnablePermission('image-update', $image);
133
134         new OutOfMemoryHandler(function () {
135             return $this->jsonError(trans('errors.image_thumbnail_memory_limit'));
136         });
137
138         $this->imageRepo->loadThumbs($image, true);
139
140         return response(trans('components.image_rebuild_thumbs_success'));
141     }
142
143     /**
144      * Check related page permission and ensure type is drawio or gallery.
145      */
146     protected function checkImagePermission(Image $image)
147     {
148         if ($image->type !== 'drawio' && $image->type !== 'gallery') {
149             $this->showPermissionError();
150         }
151
152         $relatedPage = $image->getPage();
153         if ($relatedPage) {
154             $this->checkOwnablePermission('page-view', $relatedPage);
155         }
156     }
157 }