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