]> BookStack Code Mirror - bookstack/blob - app/Uploads/Controllers/ImageController.php
Images: Updated to create thumbnails at specific events
[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         $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         try {
78             $this->imageRepo->updateImageFile($image, $file);
79         } catch (ImageUploadException $exception) {
80             return $this->jsonError($exception->getMessage());
81         }
82
83         return response('');
84     }
85
86     /**
87      * Get the form for editing the given image.
88      *
89      * @throws Exception
90      */
91     public function edit(Request $request, string $id)
92     {
93         $image = $this->imageRepo->getById($id);
94         $this->checkImagePermission($image);
95
96         if ($request->has('delete')) {
97             $dependantPages = $this->imageRepo->getPagesUsingImage($image);
98         }
99
100         $this->imageRepo->loadThumbs($image, false);
101
102         return view('pages.parts.image-manager-form', [
103             'image'          => $image,
104             'dependantPages' => $dependantPages ?? null,
105         ]);
106     }
107
108     /**
109      * Deletes an image and all thumbnail/image files.
110      *
111      * @throws Exception
112      */
113     public function destroy(string $id)
114     {
115         $image = $this->imageRepo->getById($id);
116         $this->checkOwnablePermission('image-delete', $image);
117         $this->checkImagePermission($image);
118
119         $this->imageRepo->destroyImage($image);
120
121         return response('');
122     }
123
124     /**
125      * Check related page permission and ensure type is drawio or gallery.
126      */
127     protected function checkImagePermission(Image $image)
128     {
129         if ($image->type !== 'drawio' && $image->type !== 'gallery') {
130             $this->showPermissionError();
131         }
132
133         $relatedPage = $image->getPage();
134         if ($relatedPage) {
135             $this->checkOwnablePermission('page-view', $relatedPage);
136         }
137     }
138 }