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