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