]> BookStack Code Mirror - bookstack/blob - app/Repos/ImageRepo.php
Started work on revisions in image manager
[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 updateDrawing(Image $image, string $base64Uri)
164     {
165         return $this->imageService->updateImageFromBase64Uri($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 revisions, files and thumbnails.
187      * @param Image $image
188      * @return bool
189      * @throws \Exception
190      */
191     public function destroyImage(Image $image)
192     {
193         $this->imageService->destroy($image);
194         return true;
195     }
196
197
198     /**
199      * Load thumbnails onto an image object.
200      * @param Image $image
201      * @throws \BookStack\Exceptions\ImageUploadException
202      * @throws \Exception
203      */
204     protected function loadThumbs(Image $image)
205     {
206         $image->thumbs = [
207             'gallery' => $this->getThumbnail($image, 150, 150),
208             'display' => $this->getThumbnail($image, 840, 0, true)
209         ];
210     }
211
212     /**
213      * Get the thumbnail for an image.
214      * If $keepRatio is true only the width will be used.
215      * Checks the cache then storage to avoid creating / accessing the filesystem on every check.
216      * @param Image $image
217      * @param int $width
218      * @param int $height
219      * @param bool $keepRatio
220      * @return string
221      * @throws \BookStack\Exceptions\ImageUploadException
222      * @throws \Exception
223      */
224     public function getThumbnail(Image $image, $width = 220, $height = 220, $keepRatio = false)
225     {
226         try {
227             return $this->imageService->getThumbnail($image, $width, $height, $keepRatio);
228         } catch (\Exception $exception) {
229             return null;
230         }
231     }
232
233     /**
234      * Get the raw image data from an Image.
235      * @param Image $image
236      * @return null|string
237      */
238     public function getImageData(Image $image)
239     {
240         try {
241             return $this->imageService->getImageData($image);
242         } catch (\Exception $exception) {
243             return null;
244         }
245     }
246
247     /**
248      * Check if the provided image type is valid.
249      * @param $type
250      * @return bool
251      */
252     public function isValidType($type)
253     {
254         $validTypes = ['drawing', 'gallery', 'cover', 'system', 'user'];
255         return in_array($type, $validTypes);
256     }
257 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.