]> BookStack Code Mirror - bookstack/blob - app/Repos/ImageRepo.php
Added drawing update ability
[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      * Replace the image content of a drawing.
161      * @param Image $image
162      * @param string $base64Uri
163      * @return Image
164      * @throws \BookStack\Exceptions\ImageUploadException
165      */
166     public function replaceDrawingContent(Image $image, string $base64Uri)
167     {
168         return $this->imageService->replaceImageDataFromBase64Uri($image, $base64Uri);
169     }
170
171     /**
172      * Update the details of an image via an array of properties.
173      * @param Image $image
174      * @param array $updateDetails
175      * @return Image
176      * @throws \BookStack\Exceptions\ImageUploadException
177      * @throws \Exception
178      */
179     public function updateImageDetails(Image $image, $updateDetails)
180     {
181         $image->fill($updateDetails);
182         $image->save();
183         $this->loadThumbs($image);
184         return $image;
185     }
186
187
188     /**
189      * Destroys an Image object along with its files and thumbnails.
190      * @param Image $image
191      * @return bool
192      */
193     public function destroyImage(Image $image)
194     {
195         $this->imageService->destroyImage($image);
196         return true;
197     }
198
199
200     /**
201      * Load thumbnails onto an image object.
202      * @param Image $image
203      * @throws \BookStack\Exceptions\ImageUploadException
204      * @throws \Exception
205      */
206     private function loadThumbs(Image $image)
207     {
208         $image->thumbs = [
209             'gallery' => $this->getThumbnail($image, 150, 150),
210             'display' => $this->getThumbnail($image, 840, 0, true)
211         ];
212     }
213
214     /**
215      * Get the thumbnail for an image.
216      * If $keepRatio is true only the width will be used.
217      * Checks the cache then storage to avoid creating / accessing the filesystem on every check.
218      *
219      * @param Image $image
220      * @param int $width
221      * @param int $height
222      * @param bool $keepRatio
223      * @return string
224      * @throws \BookStack\Exceptions\ImageUploadException
225      * @throws \Exception
226      */
227     public function getThumbnail(Image $image, $width = 220, $height = 220, $keepRatio = false)
228     {
229         try {
230             return $this->imageService->getThumbnail($image, $width, $height, $keepRatio);
231         } catch (FileNotFoundException $exception) {
232             $image->delete();
233             return [];
234         }
235     }
236
237     /**
238      * Get the raw image data from an Image.
239      * @param Image $image
240      * @return null|string
241      */
242     public function getImageData(Image $image)
243     {
244         try {
245             return $this->imageService->getImageData($image);
246         } catch (\Exception $exception) {
247             return null;
248         }
249     }
250
251
252 }