3 namespace BookStack\Uploads;
5 use BookStack\Entities\Queries\PageQueries;
6 use BookStack\Exceptions\ImageUploadException;
7 use BookStack\Permissions\PermissionApplicator;
9 use Illuminate\Database\Eloquent\Builder;
10 use Symfony\Component\HttpFoundation\File\UploadedFile;
14 public function __construct(
15 protected ImageService $imageService,
16 protected PermissionApplicator $permissions,
17 protected ImageResizer $imageResizer,
18 protected PageQueries $pageQueries,
23 * Get an image with the given id.
25 public function getById($id): Image
27 return Image::query()->findOrFail($id);
31 * Execute a paginated query, returning in a standard format.
32 * Also runs the query through the restriction system.
34 protected function returnPaginated(Builder $query, int $page = 1, int $pageSize = 24): array
36 $images = $query->orderBy('created_at', 'desc')->skip($pageSize * ($page - 1))->take($pageSize + 1)->get();
39 'images' => $images->take($pageSize),
40 'has_more' => count($images) > $pageSize,
45 * Fetch a list of images in a paginated format, filtered by image type.
46 * Can be filtered by uploaded to and also by name.
48 public function getPaginatedByType(
52 int $uploadedTo = null,
53 string $search = null,
54 callable $whereClause = null
56 $imageQuery = Image::query()->where('type', '=', strtolower($type));
58 if ($uploadedTo !== null) {
59 $imageQuery = $imageQuery->where('uploaded_to', '=', $uploadedTo);
62 if ($search !== null) {
63 $imageQuery = $imageQuery->where('name', 'LIKE', '%' . $search . '%');
66 // Filter by page access
67 $imageQuery = $this->permissions->restrictPageRelationQuery($imageQuery, 'images', 'uploaded_to');
69 if ($whereClause !== null) {
70 $imageQuery = $imageQuery->where($whereClause);
73 return $this->returnPaginated($imageQuery, $page, $pageSize);
77 * Get paginated gallery images within a specific page or book.
79 public function getEntityFiltered(
87 $contextPage = $this->pageQueries->findVisibleByIdOrFail($uploadedTo);
90 if ($filterType === 'book' || $filterType === 'page') {
91 $parentFilter = function (Builder $query) use ($filterType, $contextPage) {
92 if ($filterType === 'page') {
93 $query->where('uploaded_to', '=', $contextPage->id);
94 } elseif ($filterType === 'book') {
95 $validPageIds = $contextPage->book->pages()
99 $query->whereIn('uploaded_to', $validPageIds);
104 return $this->getPaginatedByType($type, $page, $pageSize, null, $search, $parentFilter);
108 * Save a new image into storage and return the new image.
110 * @throws ImageUploadException
112 public function saveNew(UploadedFile $uploadFile, string $type, int $uploadedTo = 0, int $resizeWidth = null, int $resizeHeight = null, bool $keepRatio = true): Image
114 $image = $this->imageService->saveNewFromUpload($uploadFile, $type, $uploadedTo, $resizeWidth, $resizeHeight, $keepRatio);
116 if ($type !== 'system') {
117 $this->imageResizer->loadGalleryThumbnailsForImage($image, true);
124 * Save a new image from an existing image data string.
126 * @throws ImageUploadException
128 public function saveNewFromData(string $imageName, string $imageData, string $type, int $uploadedTo = 0): Image
130 $image = $this->imageService->saveNew($imageName, $imageData, $type, $uploadedTo);
131 $this->imageResizer->loadGalleryThumbnailsForImage($image, true);
137 * Save a drawing in the database.
139 * @throws ImageUploadException
141 public function saveDrawing(string $base64Uri, int $uploadedTo): Image
143 $name = 'Drawing-' . user()->id . '-' . time() . '.png';
145 return $this->imageService->saveNewFromBase64Uri($base64Uri, $name, 'drawio', $uploadedTo);
149 * Update the details of an image via an array of properties.
153 public function updateImageDetails(Image $image, $updateDetails): Image
155 $image->fill($updateDetails);
156 $image->updated_by = user()->id;
158 $this->imageResizer->loadGalleryThumbnailsForImage($image, false);
164 * Update the image file of an existing image in the system.
165 * @throws ImageUploadException
167 public function updateImageFile(Image $image, UploadedFile $file): void
169 if (strtolower($file->getClientOriginalExtension()) !== strtolower(pathinfo($image->path, PATHINFO_EXTENSION))) {
170 throw new ImageUploadException(trans('errors.image_upload_replace_type'));
174 $image->updated_by = user()->id;
178 $this->imageService->replaceExistingFromUpload($image->path, $image->type, $file);
179 $this->imageResizer->loadGalleryThumbnailsForImage($image, true);
183 * Destroys an Image object along with its revisions, files and thumbnails.
187 public function destroyImage(Image $image = null): void
190 $this->imageService->destroy($image);
195 * Destroy images that have a specific URL and type combination.
199 public function destroyByUrlAndType(string $url, string $imageType): void
201 $images = Image::query()
202 ->where('url', '=', $url)
203 ->where('type', '=', $imageType)
206 foreach ($images as $image) {
207 $this->destroyImage($image);
212 * Get the raw image data from an Image.
214 public function getImageData(Image $image): ?string
217 return $this->imageService->getImageData($image);
218 } catch (Exception $exception) {
224 * Get the user visible pages using the given image.
226 public function getPagesUsingImage(Image $image): array
228 $pages = $this->pageQueries->visibleForList()
229 ->where('html', 'like', '%' . $image->url . '%')
232 foreach ($pages as $page) {
233 $page->setAttribute('url', $page->getUrl());
236 return $pages->all();