]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Images/ImageController.php
Merge branch 'v21.05.x'
[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 Exception;
11 use Illuminate\Filesystem\Filesystem as File;
12 use Illuminate\Http\Request;
13 use Illuminate\Validation\ValidationException;
14
15 class ImageController extends Controller
16 {
17     protected $image;
18     protected $file;
19     protected $imageRepo;
20
21     /**
22      * ImageController constructor.
23      */
24     public function __construct(Image $image, File $file, ImageRepo $imageRepo)
25     {
26         $this->image = $image;
27         $this->file = $file;
28         $this->imageRepo = $imageRepo;
29     }
30
31     /**
32      * Provide an image file from storage.
33      *
34      * @throws NotFoundException
35      */
36     public function showImage(string $path)
37     {
38         $path = storage_path('uploads/images/' . $path);
39         if (!file_exists($path)) {
40             throw (new NotFoundException(trans('errors.image_not_found')))
41                 ->setSubtitle(trans('errors.image_not_found_subtitle'))
42                 ->setDetails(trans('errors.image_not_found_details'));
43         }
44
45         return response()->file($path);
46     }
47
48     /**
49      * Update image details.
50      *
51      * @throws ImageUploadException
52      * @throws ValidationException
53      */
54     public function update(Request $request, string $id)
55     {
56         $this->validate($request, [
57             'name' => 'required|min:2|string',
58         ]);
59
60         $image = $this->imageRepo->getById($id);
61         $this->checkImagePermission($image);
62         $this->checkOwnablePermission('image-update', $image);
63
64         $image = $this->imageRepo->updateImageDetails($image, $request->all());
65
66         $this->imageRepo->loadThumbs($image);
67
68         return view('components.image-manager-form', [
69             'image'          => $image,
70             'dependantPages' => null,
71         ]);
72     }
73
74     /**
75      * Get the form for editing the given image.
76      *
77      * @throws Exception
78      */
79     public function edit(Request $request, string $id)
80     {
81         $image = $this->imageRepo->getById($id);
82         $this->checkImagePermission($image);
83
84         if ($request->has('delete')) {
85             $dependantPages = $this->imageRepo->getPagesUsingImage($image);
86         }
87
88         $this->imageRepo->loadThumbs($image);
89
90         return view('components.image-manager-form', [
91             'image'          => $image,
92             'dependantPages' => $dependantPages ?? null,
93         ]);
94     }
95
96     /**
97      * Deletes an image and all thumbnail/image files.
98      *
99      * @throws Exception
100      */
101     public function destroy(string $id)
102     {
103         $image = $this->imageRepo->getById($id);
104         $this->checkOwnablePermission('image-delete', $image);
105         $this->checkImagePermission($image);
106
107         $this->imageRepo->destroyImage($image);
108
109         return response('');
110     }
111
112     /**
113      * Check related page permission and ensure type is drawio or gallery.
114      */
115     protected function checkImagePermission(Image $image)
116     {
117         if ($image->type !== 'drawio' && $image->type !== 'gallery') {
118             $this->showPermissionError();
119         }
120
121         $relatedPage = $image->getPage();
122         if ($relatedPage) {
123             $this->checkOwnablePermission('page-view', $relatedPage);
124         }
125     }
126 }