3 namespace BookStack\Uploads;
5 use BookStack\Entities\Models\Page;
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
21 * Get an image with the given id.
23 public function getById($id): Image
25 return Image::query()->findOrFail($id);
29 * Execute a paginated query, returning in a standard format.
30 * Also runs the query through the restriction system.
32 private function returnPaginated($query, $page = 1, $pageSize = 24): array
34 $images = $query->orderBy('created_at', 'desc')->skip($pageSize * ($page - 1))->take($pageSize + 1)->get();
35 $hasMore = count($images) > $pageSize;
37 $returnImages = $images->take($pageSize);
38 $returnImages->each(function (Image $image) {
39 $this->loadThumbs($image);
43 'images' => $returnImages,
44 'has_more' => $hasMore,
49 * Fetch a list of images in a paginated format, filtered by image type.
50 * Can be filtered by uploaded to and also by name.
52 public function getPaginatedByType(
56 int $uploadedTo = null,
57 string $search = null,
58 callable $whereClause = null
60 $imageQuery = Image::query()->where('type', '=', strtolower($type));
62 if ($uploadedTo !== null) {
63 $imageQuery = $imageQuery->where('uploaded_to', '=', $uploadedTo);
66 if ($search !== null) {
67 $imageQuery = $imageQuery->where('name', 'LIKE', '%' . $search . '%');
70 // Filter by page access
71 $imageQuery = $this->permissions->restrictPageRelationQuery($imageQuery, 'images', 'uploaded_to');
73 if ($whereClause !== null) {
74 $imageQuery = $imageQuery->where($whereClause);
77 return $this->returnPaginated($imageQuery, $page, $pageSize);
81 * Get paginated gallery images within a specific page or book.
83 public function getEntityFiltered(
85 string $filterType = null,
88 int $uploadedTo = null,
91 /** @var Page $contextPage */
92 $contextPage = Page::visible()->findOrFail($uploadedTo);
95 if ($filterType === 'book' || $filterType === 'page') {
96 $parentFilter = function (Builder $query) use ($filterType, $contextPage) {
97 if ($filterType === 'page') {
98 $query->where('uploaded_to', '=', $contextPage->id);
99 } elseif ($filterType === 'book') {
100 $validPageIds = $contextPage->book->pages()
104 $query->whereIn('uploaded_to', $validPageIds);
109 return $this->getPaginatedByType($type, $page, $pageSize, null, $search, $parentFilter);
113 * Save a new image into storage and return the new image.
115 * @throws ImageUploadException
117 public function saveNew(UploadedFile $uploadFile, string $type, int $uploadedTo = 0, int $resizeWidth = null, int $resizeHeight = null, bool $keepRatio = true): Image
119 $image = $this->imageService->saveNewFromUpload($uploadFile, $type, $uploadedTo, $resizeWidth, $resizeHeight, $keepRatio);
121 if ($type !== 'system') {
122 $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);
161 $image->updated_by = user()->id;
163 $this->loadThumbs($image);
169 * Update the image file of an existing image in the system.
170 * @throws ImageUploadException
172 public function updateImageFile(Image $image, UploadedFile $file): void
174 if ($file->getClientOriginalExtension() !== pathinfo($image->path, PATHINFO_EXTENSION)) {
175 throw new ImageUploadException(trans('errors.image_upload_replace_type'));
179 $image->updated_by = user()->id;
181 $this->imageService->replaceExistingFromUpload($image->path, $image->type, $file);
182 $this->loadThumbs($image, true);
186 * Destroys an Image object along with its revisions, files and thumbnails.
190 public function destroyImage(Image $image = null): void
193 $this->imageService->destroy($image);
198 * Destroy images that have a specific URL and type combination.
202 public function destroyByUrlAndType(string $url, string $imageType): void
204 $images = Image::query()
205 ->where('url', '=', $url)
206 ->where('type', '=', $imageType)
209 foreach ($images as $image) {
210 $this->destroyImage($image);
215 * Load thumbnails onto an image object.
217 public function loadThumbs(Image $image, bool $forceCreate = false): void
219 $image->setAttribute('thumbs', [
220 'gallery' => $this->getThumbnail($image, 150, 150, false, $forceCreate),
221 'display' => $this->getThumbnail($image, 1680, null, true, $forceCreate),
226 * Get the thumbnail for an image.
227 * If $keepRatio is true only the width will be used.
228 * Checks the cache then storage to avoid creating / accessing the filesystem on every check.
230 protected function getThumbnail(Image $image, ?int $width, ?int $height, bool $keepRatio, bool $forceCreate): ?string
233 return $this->imageService->getThumbnail($image, $width, $height, $keepRatio, $forceCreate);
234 } catch (Exception $exception) {
240 * Get the raw image data from an Image.
242 public function getImageData(Image $image): ?string
245 return $this->imageService->getImageData($image);
246 } catch (Exception $exception) {
252 * Get the user visible pages using the given image.
254 public function getPagesUsingImage(Image $image): array
256 $pages = Page::visible()
257 ->where('html', 'like', '%' . $image->url . '%')
258 ->get(['id', 'name', 'slug', 'book_id']);
260 foreach ($pages as $page) {
261 $page->setAttribute('url', $page->getUrl());
264 return $pages->all();