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,
29 $this->image = $image;
30 $this->imageService = $imageService;
31 $this->restrictionService = $permissionService;
37 * Get an image with the given id.
41 public function getById($id)
43 return $this->image->findOrFail($id);
47 * Execute a paginated query, returning in a standard format.
48 * Also runs the query through the restriction system.
51 * @param int $pageSize
52 * @param bool $filterOnPage
55 private function returnPaginated($query, $page = 1, $pageSize = 24)
57 $images = $query->orderBy('created_at', 'desc')->skip($pageSize * ($page - 1))->take($pageSize + 1)->get();
58 $hasMore = count($images) > $pageSize;
60 $returnImages = $images->take($pageSize);
61 $returnImages->each(function ($image) {
62 $this->loadThumbs($image);
66 'images' => $returnImages,
67 'has_more' => $hasMore
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.
76 * @param int $pageSize
77 * @param int $uploadedTo
78 * @param string|null $search
79 * @param callable|null $whereClause
82 public function getPaginatedByType(
86 int $uploadedTo = null,
87 string $search = null,
88 callable $whereClause = null
90 $imageQuery = $this->image->newQuery()->where('type', '=', strtolower($type));
92 if ($uploadedTo !== null) {
93 $imageQuery = $imageQuery->where('uploaded_to', '=', $uploadedTo);
96 if ($search !== null) {
97 $imageQuery = $imageQuery->where('name', 'LIKE', '%' . $search . '%');
100 // Filter by page access
101 $imageQuery = $this->restrictionService->filterRelatedEntity('page', $imageQuery, 'images', 'uploaded_to');
103 if ($whereClause !== null) {
104 $imageQuery = $imageQuery->where($whereClause);
107 return $this->returnPaginated($imageQuery, $page, $pageSize);
111 * Get paginated gallery images within a specific page or book.
112 * @param string $type
113 * @param string $filterType
115 * @param int $pageSize
116 * @param int|null $uploadedTo
117 * @param string|null $search
120 public function getEntityFiltered(
122 string $filterType = null,
125 int $uploadedTo = null,
126 string $search = null
128 $contextPage = $this->page->findOrFail($uploadedTo);
129 $parentFilter = null;
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);
142 return $this->getPaginatedByType($type, $page, $pageSize, null, $search, $parentFilter);
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
154 * @throws \BookStack\Exceptions\ImageUploadException
156 public function saveNew(UploadedFile $uploadFile, $type, $uploadedTo = 0, int $resizeWidth = null, int $resizeHeight = null, bool $keepRatio = true)
158 $image = $this->imageService->saveNewFromUpload($uploadFile, $type, $uploadedTo, $resizeWidth, $resizeHeight, $keepRatio);
159 $this->loadThumbs($image);
164 * Save a drawing the the database;
165 * @param string $base64Uri
166 * @param int $uploadedTo
168 * @throws \BookStack\Exceptions\ImageUploadException
170 public function saveDrawing(string $base64Uri, int $uploadedTo)
172 $name = 'Drawing-' . user()->getShortName(40) . '-' . strval(time()) . '.png';
173 $image = $this->imageService->saveNewFromBase64Uri($base64Uri, $name, 'drawio', $uploadedTo);
179 * Update the details of an image via an array of properties.
180 * @param Image $image
181 * @param array $updateDetails
183 * @throws \BookStack\Exceptions\ImageUploadException
186 public function updateImageDetails(Image $image, $updateDetails)
188 $image->fill($updateDetails);
190 $this->loadThumbs($image);
196 * Destroys an Image object along with its revisions, files and thumbnails.
197 * @param Image $image
201 public function destroyImage(Image $image = null)
204 $this->imageService->destroy($image);
210 * Destroy all images of a certain type.
211 * @param string $imageType
214 public function destroyByType(string $imageType)
216 $images = $this->image->where('type', '=', $imageType)->get();
217 foreach ($images as $image) {
218 $this->destroyImage($image);
224 * Load thumbnails onto an image object.
225 * @param Image $image
226 * @throws \BookStack\Exceptions\ImageUploadException
229 protected function loadThumbs(Image $image)
232 'gallery' => $this->getThumbnail($image, 150, 150, false),
233 'display' => $this->getThumbnail($image, 1680, null, true)
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
244 * @param bool $keepRatio
246 * @throws \BookStack\Exceptions\ImageUploadException
249 protected function getThumbnail(Image $image, $width = 220, $height = 220, $keepRatio = false)
252 return $this->imageService->getThumbnail($image, $width, $height, $keepRatio);
253 } catch (\Exception $exception) {
259 * Get the raw image data from an Image.
260 * @param Image $image
261 * @return null|string
263 public function getImageData(Image $image)
266 return $this->imageService->getImageData($image);
267 } catch (\Exception $exception) {
273 * Get the validation rules for image files.
276 public function getImageValidationRules()
278 return 'image_extension|no_double_extension|mimes:jpeg,png,gif,bmp,webp,tiff';