]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Images/ImageController.php
Added force option for update-url command
[bookstack] / app / Http / Controllers / Images / ImageController.php
1 <?php
2
3 namespace BookStack\Http\Controllers\Images;
4
5 use BookStack\Exceptions\ImageUploadException;
6 use BookStack\Exceptions\NotFoundException;
7 use BookStack\Http\Controllers\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     protected ImageRepo $imageRepo;
18     protected ImageService $imageService;
19
20     public function __construct(ImageRepo $imageRepo, ImageService $imageService)
21     {
22         $this->imageRepo = $imageRepo;
23         $this->imageService = $imageService;
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      * @throws ImageUploadException
46      * @throws ValidationException
47      */
48     public function update(Request $request, string $id)
49     {
50         $this->validate($request, [
51             'name' => ['required', 'min:2', 'string'],
52         ]);
53
54         $image = $this->imageRepo->getById($id);
55         $this->checkImagePermission($image);
56         $this->checkOwnablePermission('image-update', $image);
57
58         $image = $this->imageRepo->updateImageDetails($image, $request->all());
59
60         $this->imageRepo->loadThumbs($image);
61
62         return view('pages.parts.image-manager-form', [
63             'image'          => $image,
64             'dependantPages' => null,
65         ]);
66     }
67
68     /**
69      * Get the form for editing the given image.
70      *
71      * @throws Exception
72      */
73     public function edit(Request $request, string $id)
74     {
75         $image = $this->imageRepo->getById($id);
76         $this->checkImagePermission($image);
77
78         if ($request->has('delete')) {
79             $dependantPages = $this->imageRepo->getPagesUsingImage($image);
80         }
81
82         $this->imageRepo->loadThumbs($image);
83
84         return view('pages.parts.image-manager-form', [
85             'image'          => $image,
86             'dependantPages' => $dependantPages ?? null,
87         ]);
88     }
89
90     /**
91      * Deletes an image and all thumbnail/image files.
92      *
93      * @throws Exception
94      */
95     public function destroy(string $id)
96     {
97         $image = $this->imageRepo->getById($id);
98         $this->checkOwnablePermission('image-delete', $image);
99         $this->checkImagePermission($image);
100
101         $this->imageRepo->destroyImage($image);
102
103         return response('');
104     }
105
106     /**
107      * Check related page permission and ensure type is drawio or gallery.
108      */
109     protected function checkImagePermission(Image $image)
110     {
111         if ($image->type !== 'drawio' && $image->type !== 'gallery') {
112             $this->showPermissionError();
113         }
114
115         $relatedPage = $image->getPage();
116         if ($relatedPage) {
117             $this->checkOwnablePermission('page-view', $relatedPage);
118         }
119     }
120 }