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