]> BookStack Code Mirror - bookstack/blob - app/Repos/ImageRepo.php
734254b3d1348ede6d8c069c841c4635e6634c54
[bookstack] / app / Repos / ImageRepo.php
1 <?php namespace BookStack\Repos;
2
3
4 use BookStack\Image;
5 use BookStack\Page;
6 use BookStack\Services\ImageService;
7 use BookStack\Services\PermissionService;
8 use Illuminate\Contracts\Filesystem\FileNotFoundException;
9 use Setting;
10 use Symfony\Component\HttpFoundation\File\UploadedFile;
11
12 class ImageRepo
13 {
14
15     protected $image;
16     protected $imageService;
17     protected $restrictionService;
18     protected $page;
19
20     /**
21      * ImageRepo constructor.
22      * @param Image $image
23      * @param ImageService $imageService
24      * @param PermissionService $permissionService
25      * @param Page $page
26      */
27     public function __construct(Image $image, ImageService $imageService, PermissionService $permissionService, Page $page)
28     {
29         $this->image = $image;
30         $this->imageService = $imageService;
31         $this->restrictionService = $permissionService;
32         $this->page = $page;
33     }
34
35
36     /**
37      * Get an image with the given id.
38      * @param $id
39      * @return mixed
40      */
41     public function getById($id)
42     {
43         return $this->image->findOrFail($id);
44     }
45
46     /**
47      * Execute a paginated query, returning in a standard format.
48      * Also runs the query through the restriction system.
49      * @param $query
50      * @param int $page
51      * @param int $pageSize
52      * @return array
53      */
54     private function returnPaginated($query, $page = 0, $pageSize = 24)
55     {
56         $images = $this->restrictionService->filterRelatedPages($query, 'images', 'uploaded_to');
57         $images = $images->orderBy('created_at', 'desc')->skip($pageSize * $page)->take($pageSize + 1)->get();
58         $hasMore = count($images) > $pageSize;
59
60         $returnImages = $images->take(24);
61         $returnImages->each(function ($image) {
62             $this->loadThumbs($image);
63         });
64
65         return [
66             'images'  => $returnImages,
67             'hasMore' => $hasMore
68         ];
69     }
70
71     /**
72      * Gets a load images paginated, filtered by image type.
73      * @param string $type
74      * @param int $page
75      * @param int $pageSize
76      * @param bool|int $userFilter
77      * @return array
78      */
79     public function getPaginatedByType($type, $page = 0, $pageSize = 24, $userFilter = false)
80     {
81         $images = $this->image->where('type', '=', strtolower($type));
82
83         if ($userFilter !== false) {
84             $images = $images->where('created_by', '=', $userFilter);
85         }
86
87         return $this->returnPaginated($images, $page, $pageSize);
88     }
89
90     /**
91      * Search for images by query, of a particular type.
92      * @param string $type
93      * @param int $page
94      * @param int $pageSize
95      * @param string $searchTerm
96      * @return array
97      */
98     public function searchPaginatedByType($type, $page = 0, $pageSize = 24, $searchTerm)
99     {
100         $images = $this->image->where('type', '=', strtolower($type))->where('name', 'LIKE', '%' . $searchTerm . '%');
101         return $this->returnPaginated($images, $page, $pageSize);
102     }
103
104     /**
105      * Get gallery images with a particular filter criteria such as
106      * being within the current book or page.
107      * @param int $pagination
108      * @param int $pageSize
109      * @param $filter
110      * @param $pageId
111      * @return array
112      */
113     public function getGalleryFiltered($pagination = 0, $pageSize = 24, $filter, $pageId)
114     {
115         $images = $this->image->where('type', '=', 'gallery');
116
117         $page = $this->page->findOrFail($pageId);
118
119         if ($filter === 'page') {
120             $images = $images->where('uploaded_to', '=', $page->id);
121         } elseif ($filter === 'book') {
122             $validPageIds = $page->book->pages->pluck('id')->toArray();
123             $images = $images->whereIn('uploaded_to', $validPageIds);
124         }
125
126         return $this->returnPaginated($images, $pagination, $pageSize);
127     }
128
129     /**
130      * Save a new image into storage and return the new image.
131      * @param UploadedFile $uploadFile
132      * @param  string $type
133      * @param int $uploadedTo
134      * @return Image
135      * @throws \BookStack\Exceptions\ImageUploadException
136      * @throws \Exception
137      */
138     public function saveNew(UploadedFile $uploadFile, $type, $uploadedTo = 0)
139     {
140         $image = $this->imageService->saveNewFromUpload($uploadFile, $type, $uploadedTo);
141         $this->loadThumbs($image);
142         return $image;
143     }
144
145     /**
146      * Save a drawing the the database;
147      * @param string $base64Uri
148      * @param int $uploadedTo
149      * @return Image
150      * @throws \BookStack\Exceptions\ImageUploadException
151      */
152     public function saveDrawing(string $base64Uri, int $uploadedTo)
153     {
154         $name = 'Drawing-' . user()->getShortName(40) . '-' . strval(time()) . '.png';
155         $image = $this->imageService->saveNewFromBase64Uri($base64Uri, $name, 'drawing', $uploadedTo);
156         return $image;
157     }
158
159     /**
160      * Update the details of an image via an array of properties.
161      * @param Image $image
162      * @param array $updateDetails
163      * @return Image
164      * @throws \BookStack\Exceptions\ImageUploadException
165      * @throws \Exception
166      */
167     public function updateImageDetails(Image $image, $updateDetails)
168     {
169         $image->fill($updateDetails);
170         $image->save();
171         $this->loadThumbs($image);
172         return $image;
173     }
174
175
176     /**
177      * Destroys an Image object along with its files and thumbnails.
178      * @param Image $image
179      * @return bool
180      */
181     public function destroyImage(Image $image)
182     {
183         $this->imageService->destroyImage($image);
184         return true;
185     }
186
187
188     /**
189      * Load thumbnails onto an image object.
190      * @param Image $image
191      * @throws \BookStack\Exceptions\ImageUploadException
192      * @throws \Exception
193      */
194     private function loadThumbs(Image $image)
195     {
196         $image->thumbs = [
197             'gallery' => $this->getThumbnail($image, 150, 150),
198             'display' => $this->getThumbnail($image, 840, 0, true)
199         ];
200     }
201
202     /**
203      * Get the thumbnail for an image.
204      * If $keepRatio is true only the width will be used.
205      * Checks the cache then storage to avoid creating / accessing the filesystem on every check.
206      *
207      * @param Image $image
208      * @param int $width
209      * @param int $height
210      * @param bool $keepRatio
211      * @return string
212      * @throws \BookStack\Exceptions\ImageUploadException
213      * @throws \Exception
214      */
215     public function getThumbnail(Image $image, $width = 220, $height = 220, $keepRatio = false)
216     {
217         try {
218             return $this->imageService->getThumbnail($image, $width, $height, $keepRatio);
219         } catch (FileNotFoundException $exception) {
220             $image->delete();
221             return [];
222         }
223     }
224
225     /**
226      * Get the raw image data from an Image.
227      * @param Image $image
228      * @return null|string
229      */
230     public function getImageData(Image $image)
231     {
232         try {
233             return $this->imageService->getImageData($image);
234         } catch (\Exception $exception) {
235             return null;
236         }
237     }
238
239
240 }