3 namespace BookStack\Uploads;
5 use BookStack\Auth\Permissions\PermissionService;
6 use BookStack\Entities\Models\Page;
7 use BookStack\Exceptions\ImageUploadException;
9 use Illuminate\Database\Eloquent\Builder;
10 use Symfony\Component\HttpFoundation\File\UploadedFile;
14 protected $imageService;
15 protected $restrictionService;
18 * ImageRepo constructor.
20 public function __construct(ImageService $imageService, PermissionService $permissionService) {
21 $this->imageService = $imageService;
22 $this->restrictionService = $permissionService;
26 * Get an image with the given id.
28 public function getById($id): Image
30 return Image::query()->findOrFail($id);
34 * Execute a paginated query, returning in a standard format.
35 * Also runs the query through the restriction system.
37 private function returnPaginated($query, $page = 1, $pageSize = 24): array
39 $images = $query->orderBy('created_at', 'desc')->skip($pageSize * ($page - 1))->take($pageSize + 1)->get();
40 $hasMore = count($images) > $pageSize;
42 $returnImages = $images->take($pageSize);
43 $returnImages->each(function (Image $image) {
44 $this->loadThumbs($image);
48 'images' => $returnImages,
49 'has_more' => $hasMore,
54 * Fetch a list of images in a paginated format, filtered by image type.
55 * Can be filtered by uploaded to and also by name.
57 public function getPaginatedByType(
61 int $uploadedTo = null,
62 string $search = null,
63 callable $whereClause = null
65 $imageQuery = Image::query()->where('type', '=', strtolower($type));
67 if ($uploadedTo !== null) {
68 $imageQuery = $imageQuery->where('uploaded_to', '=', $uploadedTo);
71 if ($search !== null) {
72 $imageQuery = $imageQuery->where('name', 'LIKE', '%' . $search . '%');
75 // Filter by page access
76 $imageQuery = $this->restrictionService->filterRelatedEntity(Page::class, $imageQuery, 'images', 'uploaded_to');
78 if ($whereClause !== null) {
79 $imageQuery = $imageQuery->where($whereClause);
82 return $this->returnPaginated($imageQuery, $page, $pageSize);
86 * Get paginated gallery images within a specific page or book.
88 public function getEntityFiltered(
90 string $filterType = null,
93 int $uploadedTo = null,
96 /** @var Page $contextPage */
97 $contextPage = Page::visible()->findOrFail($uploadedTo);
100 if ($filterType === 'book' || $filterType === 'page') {
101 $parentFilter = function (Builder $query) use ($filterType, $contextPage) {
102 if ($filterType === 'page') {
103 $query->where('uploaded_to', '=', $contextPage->id);
104 } elseif ($filterType === 'book') {
105 $validPageIds = $contextPage->book->pages()->visible()->get(['id'])->pluck('id')->toArray();
106 $query->whereIn('uploaded_to', $validPageIds);
111 return $this->getPaginatedByType($type, $page, $pageSize, null, $search, $parentFilter);
115 * Save a new image into storage and return the new image.
117 * @throws ImageUploadException
119 public function saveNew(UploadedFile $uploadFile, string $type, int $uploadedTo = 0, int $resizeWidth = null, int $resizeHeight = null, bool $keepRatio = true): Image
121 $image = $this->imageService->saveNewFromUpload($uploadFile, $type, $uploadedTo, $resizeWidth, $resizeHeight, $keepRatio);
122 $this->loadThumbs($image);
128 * Save a new image from an existing image data string.
130 * @throws ImageUploadException
132 public function saveNewFromData(string $imageName, string $imageData, string $type, int $uploadedTo = 0): Image
134 $image = $this->imageService->saveNew($imageName, $imageData, $type, $uploadedTo);
135 $this->loadThumbs($image);
141 * Save a drawing in the database.
143 * @throws ImageUploadException
145 public function saveDrawing(string $base64Uri, int $uploadedTo): Image
147 $name = 'Drawing-' . user()->id . '-' . time() . '.png';
149 return $this->imageService->saveNewFromBase64Uri($base64Uri, $name, 'drawio', $uploadedTo);
153 * Update the details of an image via an array of properties.
157 public function updateImageDetails(Image $image, $updateDetails): Image
159 $image->fill($updateDetails);
161 $this->loadThumbs($image);
167 * Destroys an Image object along with its revisions, files and thumbnails.
171 public function destroyImage(Image $image = null): void
174 $this->imageService->destroy($image);
179 * Destroy all images of a certain type.
183 public function destroyByType(string $imageType): void
185 $images = Image::query()->where('type', '=', $imageType)->get();
186 foreach ($images as $image) {
187 $this->destroyImage($image);
192 * Load thumbnails onto an image object.
194 public function loadThumbs(Image $image): void
196 $image->setAttribute('thumbs', [
197 'gallery' => $this->getThumbnail($image, 150, 150, false),
198 'display' => $this->getThumbnail($image, 1680, null, true),
203 * Get the thumbnail for an image.
204 * If $keepRatio is true only the width will be used.
205 * Checks the cache then storage to avoid creating / accessing the filesystem on every check.
207 protected function getThumbnail(Image $image, ?int $width, ?int $height, bool $keepRatio): ?string
210 return $this->imageService->getThumbnail($image, $width, $height, $keepRatio);
211 } catch (Exception $exception) {
217 * Get the raw image data from an Image.
219 public function getImageData(Image $image): ?string
222 return $this->imageService->getImageData($image);
223 } catch (Exception $exception) {
229 * Get the user visible pages using the given image.
231 public function getPagesUsingImage(Image $image): array
233 $pages = Page::visible()
234 ->where('html', 'like', '%' . $image->url . '%')
235 ->get(['id', 'name', 'slug', 'book_id']);
237 foreach ($pages as $page) {
238 $page->url = $page->getUrl();
241 return $pages->all();