]> BookStack Code Mirror - bookstack/blob - app/Uploads/Controllers/ImageController.php
Thumbnails: Added OOM handling and regen endpoint
[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 use Illuminate\Validation\ValidationException;
15
16 class ImageController extends Controller
17 {
18     public function __construct(
19         protected ImageRepo $imageRepo,
20         protected ImageService $imageService
21     ) {
22     }
23
24     /**
25      * Provide an image file from storage.
26      *
27      * @throws NotFoundException
28      */
29     public function showImage(string $path)
30     {
31         if (!$this->imageService->pathAccessibleInLocalSecure($path)) {
32             throw (new NotFoundException(trans('errors.image_not_found')))
33                 ->setSubtitle(trans('errors.image_not_found_subtitle'))
34                 ->setDetails(trans('errors.image_not_found_details'));
35         }
36
37         return $this->imageService->streamImageFromStorageResponse('gallery', $path);
38     }
39
40     /**
41      * Update image details.
42      *
43      * @throws ImageUploadException
44      * @throws ValidationException
45      */
46     public function update(Request $request, string $id)
47     {
48         $data = $this->validate($request, [
49             'name' => ['required', 'min:2', 'string'],
50         ]);
51
52         $image = $this->imageRepo->getById($id);
53         $this->checkImagePermission($image);
54         $this->checkOwnablePermission('image-update', $image);
55
56         $image = $this->imageRepo->updateImageDetails($image, $data);
57
58         return view('pages.parts.image-manager-form', [
59             'image'          => $image,
60             'dependantPages' => null,
61         ]);
62     }
63
64     /**
65      * Update the file for an existing image.
66      */
67     public function updateFile(Request $request, string $id)
68     {
69         $this->validate($request, [
70             'file' => ['required', 'file', ...$this->getImageValidationRules()],
71         ]);
72
73         $image = $this->imageRepo->getById($id);
74         $this->checkImagePermission($image);
75         $this->checkOwnablePermission('image-update', $image);
76         $file = $request->file('file');
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 }