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 } else if ($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(
113 UploadedFile $uploadFile,
116 ?int $resizeWidth = null,
117 ?int $resizeHeight = null,
118 bool $keepRatio = true
120 $image = $this->imageService->saveNewFromUpload($uploadFile, $type, $uploadedTo, $resizeWidth, $resizeHeight, $keepRatio);
122 if ($type !== 'system') {
123 $this->imageResizer->loadGalleryThumbnailsForImage($image, true);
130 * Save a new image from an existing image data string.
132 * @throws ImageUploadException
134 public function saveNewFromData(string $imageName, string $imageData, string $type, int $uploadedTo = 0): Image
136 $image = $this->imageService->saveNew($imageName, $imageData, $type, $uploadedTo);
137 $this->imageResizer->loadGalleryThumbnailsForImage($image, true);
143 * Save a drawing in the database.
145 * @throws ImageUploadException
147 public function saveDrawing(string $base64Uri, int $uploadedTo): Image
149 $name = 'Drawing-' . user()->id . '-' . time() . '.png';
151 return $this->imageService->saveNewFromBase64Uri($base64Uri, $name, 'drawio', $uploadedTo);
155 * Update the details of an image via an array of properties.
159 public function updateImageDetails(Image $image, $updateDetails): Image
161 $image->fill($updateDetails);
162 $image->updated_by = user()->id;
164 $this->imageResizer->loadGalleryThumbnailsForImage($image, false);
170 * Update the image file of an existing image in the system.
171 * @throws ImageUploadException
173 public function updateImageFile(Image $image, UploadedFile $file): void
175 if (strtolower($file->getClientOriginalExtension()) !== strtolower(pathinfo($image->path, PATHINFO_EXTENSION))) {
176 throw new ImageUploadException(trans('errors.image_upload_replace_type'));
180 $image->updated_by = user()->id;
184 $this->imageService->replaceExistingFromUpload($image->path, $image->type, $file);
185 $this->imageResizer->loadGalleryThumbnailsForImage($image, true);
189 * Destroys an Image object along with its revisions, files and thumbnails.
193 public function destroyImage(?Image $image = null): void
196 $this->imageService->destroy($image);
201 * Destroy images that have a specific URL and type combination.
205 public function destroyByUrlAndType(string $url, string $imageType): void
207 $images = Image::query()
208 ->where('url', '=', $url)
209 ->where('type', '=', $imageType)
212 foreach ($images as $image) {
213 $this->destroyImage($image);
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 = $this->pageQueries->visibleForList()
235 ->where('html', 'like', '%' . $image->url . '%')
238 foreach ($pages as $page) {
239 $page->setAttribute('url', $page->getUrl());
242 return $pages->all();