]> BookStack Code Mirror - bookstack/blob - app/Uploads/ImageRepo.php
Added locale and text direction to html templates
[bookstack] / app / Uploads / ImageRepo.php
1 <?php namespace BookStack\Uploads;
2
3 use BookStack\Auth\Permissions\PermissionService;
4 use BookStack\Entities\Page;
5 use Illuminate\Database\Eloquent\Builder;
6 use Symfony\Component\HttpFoundation\File\UploadedFile;
7
8 class ImageRepo
9 {
10
11     protected $image;
12     protected $imageService;
13     protected $restrictionService;
14     protected $page;
15
16     /**
17      * ImageRepo constructor.
18      * @param Image $image
19      * @param ImageService $imageService
20      * @param \BookStack\Auth\Permissions\PermissionService $permissionService
21      * @param \BookStack\Entities\Page $page
22      */
23     public function __construct(
24         Image $image,
25         ImageService $imageService,
26         PermissionService $permissionService,
27         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 Image
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      * @param bool $filterOnPage
53      * @return array
54      */
55     private function returnPaginated($query, $page = 1, $pageSize = 24)
56     {
57         $images = $query->orderBy('created_at', 'desc')->skip($pageSize * ($page - 1))->take($pageSize + 1)->get();
58         $hasMore = count($images) > $pageSize;
59
60         $returnImages = $images->take($pageSize);
61         $returnImages->each(function ($image) {
62             $this->loadThumbs($image);
63         });
64
65         return [
66             'images'  => $returnImages,
67             'has_more' => $hasMore
68         ];
69     }
70
71     /**
72      * Fetch a list of images in a paginated format, filtered by image type.
73      * Can be filtered by uploaded to and also by name.
74      * @param string $type
75      * @param int $page
76      * @param int $pageSize
77      * @param int $uploadedTo
78      * @param string|null $search
79      * @param callable|null $whereClause
80      * @return array
81      */
82     public function getPaginatedByType(
83         string $type,
84         int $page = 0,
85         int $pageSize = 24,
86         int $uploadedTo = null,
87         string $search = null,
88         callable $whereClause = null
89     ) {
90         $imageQuery = $this->image->newQuery()->where('type', '=', strtolower($type));
91
92         if ($uploadedTo !== null) {
93             $imageQuery = $imageQuery->where('uploaded_to', '=', $uploadedTo);
94         }
95
96         if ($search !== null) {
97             $imageQuery = $imageQuery->where('name', 'LIKE', '%' . $search . '%');
98         }
99
100         // Filter by page access
101         $imageQuery = $this->restrictionService->filterRelatedEntity('page', $imageQuery, 'images', 'uploaded_to');
102
103         if ($whereClause !== null) {
104             $imageQuery = $imageQuery->where($whereClause);
105         }
106
107         return $this->returnPaginated($imageQuery, $page, $pageSize);
108     }
109
110     /**
111      * Get paginated gallery images within a specific page or book.
112      * @param string $type
113      * @param string $filterType
114      * @param int $page
115      * @param int $pageSize
116      * @param int|null $uploadedTo
117      * @param string|null $search
118      * @return array
119      */
120     public function getEntityFiltered(
121         string $type,
122         string $filterType = null,
123         int $page = 0,
124         int $pageSize = 24,
125         int $uploadedTo = null,
126         string $search = null
127     ) {
128         $contextPage = $this->page->findOrFail($uploadedTo);
129         $parentFilter = null;
130
131         if ($filterType === 'book' || $filterType === 'page') {
132             $parentFilter = function (Builder $query) use ($filterType, $contextPage) {
133                 if ($filterType === 'page') {
134                     $query->where('uploaded_to', '=', $contextPage->id);
135                 } elseif ($filterType === 'book') {
136                     $validPageIds = $contextPage->book->pages()->get(['id'])->pluck('id')->toArray();
137                     $query->whereIn('uploaded_to', $validPageIds);
138                 }
139             };
140         }
141
142         return $this->getPaginatedByType($type, $page, $pageSize, null, $search, $parentFilter);
143     }
144
145     /**
146      * Save a new image into storage and return the new image.
147      * @param UploadedFile $uploadFile
148      * @param string $type
149      * @param int $uploadedTo
150      * @param int|null $resizeWidth
151      * @param int|null $resizeHeight
152      * @param bool $keepRatio
153      * @return Image
154      * @throws \BookStack\Exceptions\ImageUploadException
155      */
156     public function saveNew(UploadedFile $uploadFile, $type, $uploadedTo = 0, int $resizeWidth = null, int $resizeHeight = null, bool $keepRatio = true)
157     {
158         $image = $this->imageService->saveNewFromUpload($uploadFile, $type, $uploadedTo, $resizeWidth, $resizeHeight, $keepRatio);
159         $this->loadThumbs($image);
160         return $image;
161     }
162
163     /**
164      * Save a drawing the the database;
165      * @param string $base64Uri
166      * @param int $uploadedTo
167      * @return Image
168      * @throws \BookStack\Exceptions\ImageUploadException
169      */
170     public function saveDrawing(string $base64Uri, int $uploadedTo)
171     {
172         $name = 'Drawing-' . user()->getShortName(40) . '-' . strval(time()) . '.png';
173         $image = $this->imageService->saveNewFromBase64Uri($base64Uri, $name, 'drawio', $uploadedTo);
174         return $image;
175     }
176
177
178     /**
179      * Update the details of an image via an array of properties.
180      * @param Image $image
181      * @param array $updateDetails
182      * @return Image
183      * @throws \BookStack\Exceptions\ImageUploadException
184      * @throws \Exception
185      */
186     public function updateImageDetails(Image $image, $updateDetails)
187     {
188         $image->fill($updateDetails);
189         $image->save();
190         $this->loadThumbs($image);
191         return $image;
192     }
193
194
195     /**
196      * Destroys an Image object along with its revisions, files and thumbnails.
197      * @param Image $image
198      * @return bool
199      * @throws \Exception
200      */
201     public function destroyImage(Image $image = null)
202     {
203         if ($image) {
204             $this->imageService->destroy($image);
205         }
206         return true;
207     }
208
209     /**
210      * Destroy all images of a certain type.
211      * @param string $imageType
212      * @throws \Exception
213      */
214     public function destroyByType(string $imageType)
215     {
216         $images = $this->image->where('type', '=', $imageType)->get();
217         foreach ($images as $image) {
218             $this->destroyImage($image);
219         }
220     }
221
222
223     /**
224      * Load thumbnails onto an image object.
225      * @param Image $image
226      * @throws \BookStack\Exceptions\ImageUploadException
227      * @throws \Exception
228      */
229     protected function loadThumbs(Image $image)
230     {
231         $image->thumbs = [
232             'gallery' => $this->getThumbnail($image, 150, 150, false),
233             'display' => $this->getThumbnail($image, 1680, null, true)
234         ];
235     }
236
237     /**
238      * Get the thumbnail for an image.
239      * If $keepRatio is true only the width will be used.
240      * Checks the cache then storage to avoid creating / accessing the filesystem on every check.
241      * @param Image $image
242      * @param int $width
243      * @param int $height
244      * @param bool $keepRatio
245      * @return string
246      * @throws \BookStack\Exceptions\ImageUploadException
247      * @throws \Exception
248      */
249     protected function getThumbnail(Image $image, $width = 220, $height = 220, $keepRatio = false)
250     {
251         try {
252             return $this->imageService->getThumbnail($image, $width, $height, $keepRatio);
253         } catch (\Exception $exception) {
254             return null;
255         }
256     }
257
258     /**
259      * Get the raw image data from an Image.
260      * @param Image $image
261      * @return null|string
262      */
263     public function getImageData(Image $image)
264     {
265         try {
266             return $this->imageService->getImageData($image);
267         } catch (\Exception $exception) {
268             return null;
269         }
270     }
271
272     /**
273      * Get the validation rules for image files.
274      * @return string
275      */
276     public function getImageValidationRules()
277     {
278         return 'image_extension|no_double_extension|mimes:jpeg,png,gif,bmp,webp,tiff';
279     }
280 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.