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)
22 $this->imageService = $imageService;
23 $this->restrictionService = $permissionService;
27 * Get an image with the given id.
29 public function getById($id): Image
31 return Image::query()->findOrFail($id);
35 * Execute a paginated query, returning in a standard format.
36 * Also runs the query through the restriction system.
38 private function returnPaginated($query, $page = 1, $pageSize = 24): array
40 $images = $query->orderBy('created_at', 'desc')->skip($pageSize * ($page - 1))->take($pageSize + 1)->get();
41 $hasMore = count($images) > $pageSize;
43 $returnImages = $images->take($pageSize);
44 $returnImages->each(function (Image $image) {
45 $this->loadThumbs($image);
49 'images' => $returnImages,
50 'has_more' => $hasMore,
55 * Fetch a list of images in a paginated format, filtered by image type.
56 * Can be filtered by uploaded to and also by name.
58 public function getPaginatedByType(
62 int $uploadedTo = null,
63 string $search = null,
64 callable $whereClause = null
66 $imageQuery = Image::query()->where('type', '=', strtolower($type));
68 if ($uploadedTo !== null) {
69 $imageQuery = $imageQuery->where('uploaded_to', '=', $uploadedTo);
72 if ($search !== null) {
73 $imageQuery = $imageQuery->where('name', 'LIKE', '%' . $search . '%');
76 // Filter by page access
77 $imageQuery = $this->restrictionService->filterRelatedEntity(Page::class, $imageQuery, 'images', 'uploaded_to');
79 if ($whereClause !== null) {
80 $imageQuery = $imageQuery->where($whereClause);
83 return $this->returnPaginated($imageQuery, $page, $pageSize);
87 * Get paginated gallery images within a specific page or book.
89 public function getEntityFiltered(
91 string $filterType = null,
94 int $uploadedTo = null,
97 /** @var Page $contextPage */
98 $contextPage = Page::visible()->findOrFail($uploadedTo);
101 if ($filterType === 'book' || $filterType === 'page') {
102 $parentFilter = function (Builder $query) use ($filterType, $contextPage) {
103 if ($filterType === 'page') {
104 $query->where('uploaded_to', '=', $contextPage->id);
105 } elseif ($filterType === 'book') {
106 $validPageIds = $contextPage->book->pages()
110 $query->whereIn('uploaded_to', $validPageIds);
115 return $this->getPaginatedByType($type, $page, $pageSize, null, $search, $parentFilter);
119 * Save a new image into storage and return the new image.
121 * @throws ImageUploadException
123 public function saveNew(UploadedFile $uploadFile, string $type, int $uploadedTo = 0, int $resizeWidth = null, int $resizeHeight = null, bool $keepRatio = true): Image
125 $image = $this->imageService->saveNewFromUpload($uploadFile, $type, $uploadedTo, $resizeWidth, $resizeHeight, $keepRatio);
126 $this->loadThumbs($image);
132 * Save a new image from an existing image data string.
134 * @throws ImageUploadException
136 public function saveNewFromData(string $imageName, string $imageData, string $type, int $uploadedTo = 0): Image
138 $image = $this->imageService->saveNew($imageName, $imageData, $type, $uploadedTo);
139 $this->loadThumbs($image);
145 * Save a drawing in the database.
147 * @throws ImageUploadException
149 public function saveDrawing(string $base64Uri, int $uploadedTo): Image
151 $name = 'Drawing-' . user()->id . '-' . time() . '.png';
153 return $this->imageService->saveNewFromBase64Uri($base64Uri, $name, 'drawio', $uploadedTo);
157 * Update the details of an image via an array of properties.
161 public function updateImageDetails(Image $image, $updateDetails): Image
163 $image->fill($updateDetails);
165 $this->loadThumbs($image);
171 * Destroys an Image object along with its revisions, files and thumbnails.
175 public function destroyImage(Image $image = null): void
178 $this->imageService->destroy($image);
183 * Destroy all images of a certain type.
187 public function destroyByType(string $imageType): void
189 $images = Image::query()->where('type', '=', $imageType)->get();
190 foreach ($images as $image) {
191 $this->destroyImage($image);
196 * Load thumbnails onto an image object.
198 public function loadThumbs(Image $image): void
200 $image->setAttribute('thumbs', [
201 'gallery' => $this->getThumbnail($image, 150, 150, false),
202 'display' => $this->getThumbnail($image, 1680, null, true),
207 * Get the thumbnail for an image.
208 * If $keepRatio is true only the width will be used.
209 * Checks the cache then storage to avoid creating / accessing the filesystem on every check.
211 protected function getThumbnail(Image $image, ?int $width, ?int $height, bool $keepRatio): ?string
214 return $this->imageService->getThumbnail($image, $width, $height, $keepRatio);
215 } catch (Exception $exception) {
221 * Get the raw image data from an Image.
223 public function getImageData(Image $image): ?string
226 return $this->imageService->getImageData($image);
227 } catch (Exception $exception) {
233 * Get the user visible pages using the given image.
235 public function getPagesUsingImage(Image $image): array
237 $pages = Page::visible()
238 ->where('html', 'like', '%' . $image->url . '%')
239 ->get(['id', 'name', 'slug', 'book_id']);
241 foreach ($pages as $page) {
242 $page->setAttribute('url', $page->getUrl());
245 return $pages->all();