1 <?php namespace BookStack\Uploads;
3 use BookStack\Auth\Permissions\PermissionService;
4 use BookStack\Entities\Page;
5 use Illuminate\Database\Eloquent\Builder;
6 use Symfony\Component\HttpFoundation\File\UploadedFile;
12 protected $imageService;
13 protected $restrictionService;
17 * ImageRepo constructor.
19 * @param ImageService $imageService
20 * @param \BookStack\Auth\Permissions\PermissionService $permissionService
21 * @param \BookStack\Entities\Page $page
23 public function __construct(
25 ImageService $imageService,
26 PermissionService $permissionService,
30 $this->image = $image;
31 $this->imageService = $imageService;
32 $this->restrictionService = $permissionService;
38 * Get an image with the given id.
42 public function getById($id)
44 return $this->image->findOrFail($id);
48 * Execute a paginated query, returning in a standard format.
49 * Also runs the query through the restriction system.
52 * @param int $pageSize
53 * @param bool $filterOnPage
56 private function returnPaginated($query, $page = 1, $pageSize = 24)
58 $images = $query->orderBy('created_at', 'desc')->skip($pageSize * ($page - 1))->take($pageSize + 1)->get();
59 $hasMore = count($images) > $pageSize;
61 $returnImages = $images->take($pageSize);
62 $returnImages->each(function ($image) {
63 $this->loadThumbs($image);
67 'images' => $returnImages,
68 'has_more' => $hasMore
73 * Fetch a list of images in a paginated format, filtered by image type.
74 * Can be filtered by uploaded to and also by name.
77 * @param int $pageSize
78 * @param int $uploadedTo
79 * @param string|null $search
80 * @param callable|null $whereClause
83 public function getPaginatedByType(
87 int $uploadedTo = null,
88 string $search = null,
89 callable $whereClause = null
92 $imageQuery = $this->image->newQuery()->where('type', '=', strtolower($type));
94 if ($uploadedTo !== null) {
95 $imageQuery = $imageQuery->where('uploaded_to', '=', $uploadedTo);
98 if ($search !== null) {
99 $imageQuery = $imageQuery->where('name', 'LIKE', '%' . $search . '%');
102 // Filter by page access
103 $imageQuery = $this->restrictionService->filterRelatedEntity('page', $imageQuery, 'images', 'uploaded_to');
105 if ($whereClause !== null) {
106 $imageQuery = $imageQuery->where($whereClause);
109 return $this->returnPaginated($imageQuery, $page, $pageSize);
113 * Get paginated gallery images within a specific page or book.
114 * @param string $type
115 * @param string $filterType
117 * @param int $pageSize
118 * @param int|null $uploadedTo
119 * @param string|null $search
122 public function getEntityFiltered(
124 string $filterType = null,
127 int $uploadedTo = null,
128 string $search = null
131 $contextPage = $this->page->findOrFail($uploadedTo);
132 $parentFilter = null;
134 if ($filterType === 'book' || $filterType === 'page') {
135 $parentFilter = function(Builder $query) use ($filterType, $contextPage) {
136 if ($filterType === 'page') {
137 $query->where('uploaded_to', '=', $contextPage->id);
138 } elseif ($filterType === 'book') {
139 $validPageIds = $contextPage->book->pages()->get(['id'])->pluck('id')->toArray();
140 $query->whereIn('uploaded_to', $validPageIds);
145 return $this->getPaginatedByType($type, $page, $pageSize, null, $search, $parentFilter);
149 * Save a new image into storage and return the new image.
150 * @param UploadedFile $uploadFile
151 * @param string $type
152 * @param int $uploadedTo
153 * @param int|null $resizeWidth
154 * @param int|null $resizeHeight
155 * @param bool $keepRatio
157 * @throws \BookStack\Exceptions\ImageUploadException
159 public function saveNew(UploadedFile $uploadFile, $type, $uploadedTo = 0, int $resizeWidth = null, int $resizeHeight = null, bool $keepRatio = true)
161 $image = $this->imageService->saveNewFromUpload($uploadFile, $type, $uploadedTo, $resizeWidth, $resizeHeight, $keepRatio);
162 $this->loadThumbs($image);
167 * Save a drawing the the database;
168 * @param string $base64Uri
169 * @param int $uploadedTo
171 * @throws \BookStack\Exceptions\ImageUploadException
173 public function saveDrawing(string $base64Uri, int $uploadedTo)
175 $name = 'Drawing-' . user()->getShortName(40) . '-' . strval(time()) . '.png';
176 $image = $this->imageService->saveNewFromBase64Uri($base64Uri, $name, 'drawio', $uploadedTo);
182 * Update the details of an image via an array of properties.
183 * @param Image $image
184 * @param array $updateDetails
186 * @throws \BookStack\Exceptions\ImageUploadException
189 public function updateImageDetails(Image $image, $updateDetails)
191 $image->fill($updateDetails);
193 $this->loadThumbs($image);
199 * Destroys an Image object along with its revisions, files and thumbnails.
200 * @param Image $image
204 public function destroyImage(Image $image = null)
207 $this->imageService->destroy($image);
213 * Destroy all images of a certain type.
214 * @param string $imageType
217 public function destroyByType(string $imageType)
219 $images = $this->image->where('type', '=', $imageType)->get();
220 foreach ($images as $image) {
221 $this->destroyImage($image);
227 * Load thumbnails onto an image object.
228 * @param Image $image
229 * @throws \BookStack\Exceptions\ImageUploadException
232 protected function loadThumbs(Image $image)
235 'gallery' => $this->getThumbnail($image, 150, 150, false),
236 'display' => $this->getThumbnail($image, 840, null, true)
241 * Get the thumbnail for an image.
242 * If $keepRatio is true only the width will be used.
243 * Checks the cache then storage to avoid creating / accessing the filesystem on every check.
244 * @param Image $image
247 * @param bool $keepRatio
249 * @throws \BookStack\Exceptions\ImageUploadException
252 protected function getThumbnail(Image $image, $width = 220, $height = 220, $keepRatio = false)
255 return $this->imageService->getThumbnail($image, $width, $height, $keepRatio);
256 } catch (\Exception $exception) {
262 * Get the raw image data from an Image.
263 * @param Image $image
264 * @return null|string
266 public function getImageData(Image $image)
269 return $this->imageService->getImageData($image);
270 } catch (\Exception $exception) {
276 * Get the validation rules for image files.
279 public function getImageValidationRules()
281 return 'image_extension|no_double_extension|mimes:jpeg,png,gif,bmp,webp,tiff';