]> BookStack Code Mirror - bookstack/blob - app/Uploads/Controllers/ImageController.php
Vectors: Added command to regenerate for all
[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\Exceptions\NotifyException;
8 use BookStack\Http\Controller;
9 use BookStack\Uploads\Image;
10 use BookStack\Uploads\ImageRepo;
11 use BookStack\Uploads\ImageResizer;
12 use BookStack\Uploads\ImageService;
13 use BookStack\Util\OutOfMemoryHandler;
14 use Exception;
15 use Illuminate\Http\Request;
16
17 class ImageController extends Controller
18 {
19     public function __construct(
20         protected ImageRepo $imageRepo,
21         protected ImageService $imageService,
22         protected ImageResizer $imageResizer,
23     ) {
24     }
25
26     /**
27      * Provide an image file from storage.
28      *
29      * @throws NotFoundException
30      */
31     public function showImage(string $path)
32     {
33         if (!$this->imageService->pathAccessibleInLocalSecure($path)) {
34             throw (new NotFoundException(trans('errors.image_not_found')))
35                 ->setSubtitle(trans('errors.image_not_found_subtitle'))
36                 ->setDetails(trans('errors.image_not_found_details'));
37         }
38
39         return $this->imageService->streamImageFromStorageResponse('gallery', $path);
40     }
41
42     /**
43      * Update image details.
44      */
45     public function update(Request $request, string $id)
46     {
47         $data = $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, $data);
56
57         return view('pages.parts.image-manager-form', [
58             'image'          => $image,
59             'dependantPages' => null,
60         ]);
61     }
62
63     /**
64      * Update the file for an existing image.
65      */
66     public function updateFile(Request $request, string $id)
67     {
68         $this->validate($request, [
69             'file' => ['required', 'file', ...$this->getImageValidationRules()],
70         ]);
71
72         $image = $this->imageRepo->getById($id);
73         $this->checkImagePermission($image);
74         $this->checkOwnablePermission('image-update', $image);
75         $file = $request->file('file');
76
77         new OutOfMemoryHandler(function () {
78             return $this->jsonError(trans('errors.image_upload_memory_limit'));
79         });
80
81         try {
82             $this->imageRepo->updateImageFile($image, $file);
83         } catch (ImageUploadException $exception) {
84             return $this->jsonError($exception->getMessage());
85         }
86
87         return response('');
88     }
89
90     /**
91      * Get the form for editing the given image.
92      *
93      * @throws Exception
94      */
95     public function edit(Request $request, string $id)
96     {
97         $image = $this->imageRepo->getById($id);
98         $this->checkImagePermission($image);
99
100         if ($request->has('delete')) {
101             $dependantPages = $this->imageRepo->getPagesUsingImage($image);
102         }
103
104         $viewData = [
105             'image'          => $image,
106             'dependantPages' => $dependantPages ?? null,
107             'warning'        => '',
108         ];
109
110         new OutOfMemoryHandler(function () use ($viewData) {
111             $viewData['warning'] = trans('errors.image_thumbnail_memory_limit');
112             return response()->view('pages.parts.image-manager-form', $viewData);
113         });
114
115         $this->imageResizer->loadGalleryThumbnailsForImage($image, false);
116
117         return view('pages.parts.image-manager-form', $viewData);
118     }
119
120     /**
121      * Deletes an image and all thumbnail/image files.
122      *
123      * @throws Exception
124      */
125     public function destroy(string $id)
126     {
127         $image = $this->imageRepo->getById($id);
128         $this->checkOwnablePermission('image-delete', $image);
129         $this->checkImagePermission($image);
130
131         $this->imageRepo->destroyImage($image);
132
133         return response('');
134     }
135
136     /**
137      * Rebuild the thumbnails for the given image.
138      */
139     public function rebuildThumbnails(string $id)
140     {
141         $image = $this->imageRepo->getById($id);
142         $this->checkImagePermission($image);
143         $this->checkOwnablePermission('image-update', $image);
144
145         new OutOfMemoryHandler(function () {
146             return $this->jsonError(trans('errors.image_thumbnail_memory_limit'));
147         });
148
149         $this->imageResizer->loadGalleryThumbnailsForImage($image, true);
150
151         return response(trans('components.image_rebuild_thumbs_success'));
152     }
153
154     /**
155      * Check related page permission and ensure type is drawio or gallery.
156      * @throws NotifyException
157      */
158     protected function checkImagePermission(Image $image): void
159     {
160         if ($image->type !== 'drawio' && $image->type !== 'gallery') {
161             $this->showPermissionError();
162         }
163
164         $relatedPage = $image->getPage();
165         if ($relatedPage) {
166             $this->checkOwnablePermission('page-view', $relatedPage);
167         }
168     }
169 }