]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Images/ImageController.php
Default OpenID display name set to standard value
[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\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 Request $request
51      * @param integer $id
52      * @return \Illuminate\Http\JsonResponse
53      * @throws ImageUploadException
54      * @throws \Exception
55      */
56     public function update(Request $request, $id)
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      */
73     public function usage(int $id)
74     {
75         $image = $this->imageRepo->getById($id);
76         $this->checkImagePermission($image);
77
78         $pages = Page::visible()->where('html', 'like', '%' . $image->url . '%')->get(['id', 'name', 'slug', 'book_id']);
79         foreach ($pages as $page) {
80             $page->url = $page->getUrl();
81             $page->html = '';
82             $page->text = '';
83         }
84         $result = count($pages) > 0 ? $pages : false;
85
86         return response()->json($result);
87     }
88
89     /**
90      * Deletes an image and all thumbnail/image files
91      * @param int $id
92      * @return \Illuminate\Http\JsonResponse
93      * @throws \Exception
94      */
95     public function destroy($id)
96     {
97         $image = $this->imageRepo->getById($id);
98         $this->checkOwnablePermission('image-delete', $image);
99         $this->checkImagePermission($image);
100
101         $this->imageRepo->destroyImage($image);
102         return response()->json(trans('components.images_deleted'));
103     }
104
105     /**
106      * Check related page permission and ensure type is drawio or gallery.
107      * @param Image $image
108      */
109     protected function checkImagePermission(Image $image)
110     {
111         if ($image->type !== 'drawio' && $image->type !== 'gallery') {
112             $this->showPermissionError();
113         }
114
115         $relatedPage = $image->getPage();
116         if ($relatedPage) {
117             $this->checkOwnablePermission('page-view', $relatedPage);
118         }
119     }
120 }