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()->visible()->get(['id'])->pluck('id')->toArray();
107 $query->whereIn('uploaded_to', $validPageIds);
112 return $this->getPaginatedByType($type, $page, $pageSize, null, $search, $parentFilter);
116 * Save a new image into storage and return the new image.
118 * @throws ImageUploadException
120 public function saveNew(UploadedFile $uploadFile, string $type, int $uploadedTo = 0, int $resizeWidth = null, int $resizeHeight = null, bool $keepRatio = true): Image
122 $image = $this->imageService->saveNewFromUpload($uploadFile, $type, $uploadedTo, $resizeWidth, $resizeHeight, $keepRatio);
123 $this->loadThumbs($image);
129 * Save a new image from an existing image data string.
131 * @throws ImageUploadException
133 public function saveNewFromData(string $imageName, string $imageData, string $type, int $uploadedTo = 0): Image
135 $image = $this->imageService->saveNew($imageName, $imageData, $type, $uploadedTo);
136 $this->loadThumbs($image);
142 * Save a drawing in the database.
144 * @throws ImageUploadException
146 public function saveDrawing(string $base64Uri, int $uploadedTo): Image
148 $name = 'Drawing-' . user()->id . '-' . time() . '.png';
150 return $this->imageService->saveNewFromBase64Uri($base64Uri, $name, 'drawio', $uploadedTo);
154 * Update the details of an image via an array of properties.
158 public function updateImageDetails(Image $image, $updateDetails): Image
160 $image->fill($updateDetails);
162 $this->loadThumbs($image);
168 * Destroys an Image object along with its revisions, files and thumbnails.
172 public function destroyImage(Image $image = null): void
175 $this->imageService->destroy($image);
180 * Destroy all images of a certain type.
184 public function destroyByType(string $imageType): void
186 $images = Image::query()->where('type', '=', $imageType)->get();
187 foreach ($images as $image) {
188 $this->destroyImage($image);
193 * Load thumbnails onto an image object.
195 public function loadThumbs(Image $image): void
197 $image->setAttribute('thumbs', [
198 'gallery' => $this->getThumbnail($image, 150, 150, false),
199 'display' => $this->getThumbnail($image, 1680, null, true),
204 * Get the thumbnail for an image.
205 * If $keepRatio is true only the width will be used.
206 * Checks the cache then storage to avoid creating / accessing the filesystem on every check.
208 protected function getThumbnail(Image $image, ?int $width, ?int $height, bool $keepRatio): ?string
211 return $this->imageService->getThumbnail($image, $width, $height, $keepRatio);
212 } catch (Exception $exception) {
218 * Get the raw image data from an Image.
220 public function getImageData(Image $image): ?string
223 return $this->imageService->getImageData($image);
224 } catch (Exception $exception) {
230 * Get the user visible pages using the given image.
232 public function getPagesUsingImage(Image $image): array
234 $pages = Page::visible()
235 ->where('html', 'like', '%' . $image->url . '%')
236 ->get(['id', 'name', 'slug', 'book_id']);
238 foreach ($pages as $page) {
239 $page->url = $page->getUrl();
242 return $pages->all();