]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Images/ImageController.php
Automatic Restored Revision Changelog Summary Text
[bookstack] / app / Http / Controllers / Images / ImageController.php
1 <?php namespace BookStack\Http\Controllers\Images;
2
3 use BookStack\Entities\Page;
4 use BookStack\Exceptions\ImageUploadException;
5 use BookStack\Http\Controllers\Controller;
6 use BookStack\Entities\Repos\PageRepo;
7 use BookStack\Uploads\Image;
8 use BookStack\Uploads\ImageRepo;
9 use Exception;
10 use Illuminate\Filesystem\Filesystem as File;
11 use Illuminate\Http\JsonResponse;
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         parent::__construct();
30     }
31
32     /**
33      * Provide an image file from storage.
34      */
35     public function showImage(string $path)
36     {
37         $path = storage_path('uploads/images/' . $path);
38         if (!file_exists($path)) {
39             abort(404);
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 }