]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Images/ImageController.php
Added support for Pascal language
[bookstack] / app / Http / Controllers / Images / ImageController.php
1 <?php namespace BookStack\Http\Controllers\Images;
2
3 use BookStack\Entities\Repos\EntityRepo;
4 use BookStack\Exceptions\ImageUploadException;
5 use BookStack\Http\Controllers\Controller;
6 use BookStack\Repos\PageRepo;
7 use BookStack\Uploads\Image;
8 use BookStack\Uploads\ImageRepo;
9 use Illuminate\Filesystem\Filesystem as File;
10 use Illuminate\Http\Request;
11
12 class ImageController extends Controller
13 {
14     protected $image;
15     protected $file;
16     protected $imageRepo;
17
18     /**
19      * ImageController constructor.
20      * @param Image $image
21      * @param File $file
22      * @param ImageRepo $imageRepo
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      * @param string $path
35      * @return mixed
36      */
37     public function showImage(string $path)
38     {
39         $path = storage_path('uploads/images/' . $path);
40         if (!file_exists($path)) {
41             abort(404);
42         }
43
44         return response()->file($path);
45     }
46
47
48     /**
49      * Update image details
50      * @param integer $id
51      * @param Request $request
52      * @return \Illuminate\Http\JsonResponse
53      * @throws ImageUploadException
54      * @throws \Exception
55      */
56     public function update($id, Request $request)
57     {
58         $this->validate($request, [
59             'name' => 'required|min:2|string'
60         ]);
61
62         $image = $this->imageRepo->getById($id);
63         $this->checkImagePermission($image);
64         $this->checkOwnablePermission('image-update', $image);
65
66         $image = $this->imageRepo->updateImageDetails($image, $request->all());
67         return response()->json($image);
68     }
69
70     /**
71      * Show the usage of an image on pages.
72      * @param \BookStack\Entities\Repos\EntityRepo $entityRepo
73      * @param $id
74      * @return \Illuminate\Http\JsonResponse
75      */
76     public function usage(EntityRepo $entityRepo, $id)
77     {
78         $image = $this->imageRepo->getById($id);
79         $this->checkImagePermission($image);
80         $pageSearch = $entityRepo->searchForImage($image->url);
81         return response()->json($pageSearch);
82     }
83
84     /**
85      * Deletes an image and all thumbnail/image files
86      * @param int $id
87      * @return \Illuminate\Http\JsonResponse
88      * @throws \Exception
89      */
90     public function destroy($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()->json(trans('components.images_deleted'));
98     }
99
100     /**
101      * Check related page permission and ensure type is drawio or gallery.
102      * @param Image $image
103      */
104     protected function checkImagePermission(Image $image)
105     {
106         if ($image->type !== 'drawio' && $image->type !== 'gallery') {
107             $this->showPermissionError();
108         }
109
110         $relatedPage = $image->getPage();
111         if ($relatedPage) {
112             $this->checkOwnablePermission('page-view', $relatedPage);
113         }
114     }
115 }