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;
15 protected $imageService;
16 protected $restrictionService;
19 protected static $supportedExtensions = ['jpg', 'jpeg', 'png', 'gif', 'webp'];
22 * ImageRepo constructor.
24 public function __construct(
26 ImageService $imageService,
27 PermissionService $permissionService,
30 $this->image = $image;
31 $this->imageService = $imageService;
32 $this->restrictionService = $permissionService;
37 * Check if the given image extension is supported by BookStack.
38 * The extension must not be altered in this function. This check should provide a guarantee
39 * that the provided extension is safe to use for the image to be saved.
41 public function imageExtensionSupported(string $extension): bool
43 return in_array($extension, static::$supportedExtensions);
47 * Get an image with the given id.
49 public function getById($id): Image
51 return $this->image->findOrFail($id);
55 * Execute a paginated query, returning in a standard format.
56 * Also runs the query through the restriction system.
58 private function returnPaginated($query, $page = 1, $pageSize = 24): array
60 $images = $query->orderBy('created_at', 'desc')->skip($pageSize * ($page - 1))->take($pageSize + 1)->get();
61 $hasMore = count($images) > $pageSize;
63 $returnImages = $images->take($pageSize);
64 $returnImages->each(function ($image) {
65 $this->loadThumbs($image);
69 'images' => $returnImages,
70 'has_more' => $hasMore,
75 * Fetch a list of images in a paginated format, filtered by image type.
76 * Can be filtered by uploaded to and also by name.
78 public function getPaginatedByType(
82 int $uploadedTo = null,
83 string $search = null,
84 callable $whereClause = null
86 $imageQuery = $this->image->newQuery()->where('type', '=', strtolower($type));
88 if ($uploadedTo !== null) {
89 $imageQuery = $imageQuery->where('uploaded_to', '=', $uploadedTo);
92 if ($search !== null) {
93 $imageQuery = $imageQuery->where('name', 'LIKE', '%' . $search . '%');
96 // Filter by page access
97 $imageQuery = $this->restrictionService->filterRelatedEntity(Page::class, $imageQuery, 'images', 'uploaded_to');
99 if ($whereClause !== null) {
100 $imageQuery = $imageQuery->where($whereClause);
103 return $this->returnPaginated($imageQuery, $page, $pageSize);
107 * Get paginated gallery images within a specific page or book.
109 public function getEntityFiltered(
111 string $filterType = null,
114 int $uploadedTo = null,
115 string $search = null
117 $contextPage = $this->page->findOrFail($uploadedTo);
118 $parentFilter = null;
120 if ($filterType === 'book' || $filterType === 'page') {
121 $parentFilter = function (Builder $query) use ($filterType, $contextPage) {
122 if ($filterType === 'page') {
123 $query->where('uploaded_to', '=', $contextPage->id);
124 } elseif ($filterType === 'book') {
125 $validPageIds = $contextPage->book->pages()->visible()->get(['id'])->pluck('id')->toArray();
126 $query->whereIn('uploaded_to', $validPageIds);
131 return $this->getPaginatedByType($type, $page, $pageSize, null, $search, $parentFilter);
135 * Save a new image into storage and return the new image.
137 * @throws ImageUploadException
139 public function saveNew(UploadedFile $uploadFile, string $type, int $uploadedTo = 0, int $resizeWidth = null, int $resizeHeight = null, bool $keepRatio = true): Image
141 $image = $this->imageService->saveNewFromUpload($uploadFile, $type, $uploadedTo, $resizeWidth, $resizeHeight, $keepRatio);
142 $this->loadThumbs($image);
148 * Save a new image from an existing image data string.
150 * @throws ImageUploadException
152 public function saveNewFromData(string $imageName, string $imageData, string $type, int $uploadedTo = 0)
154 $image = $this->imageService->saveNew($imageName, $imageData, $type, $uploadedTo);
155 $this->loadThumbs($image);
161 * Save a drawing the the database.
163 * @throws ImageUploadException
165 public function saveDrawing(string $base64Uri, int $uploadedTo): Image
167 $name = 'Drawing-' . strval(user()->id) . '-' . strval(time()) . '.png';
169 return $this->imageService->saveNewFromBase64Uri($base64Uri, $name, 'drawio', $uploadedTo);
173 * Update the details of an image via an array of properties.
175 * @throws ImageUploadException
178 public function updateImageDetails(Image $image, $updateDetails): Image
180 $image->fill($updateDetails);
182 $this->loadThumbs($image);
188 * Destroys an Image object along with its revisions, files and thumbnails.
192 public function destroyImage(Image $image = null): bool
195 $this->imageService->destroy($image);
202 * Destroy all images of a certain type.
206 public function destroyByType(string $imageType)
208 $images = $this->image->where('type', '=', $imageType)->get();
209 foreach ($images as $image) {
210 $this->destroyImage($image);
215 * Load thumbnails onto an image object.
219 public function loadThumbs(Image $image)
222 'gallery' => $this->getThumbnail($image, 150, 150, false),
223 'display' => $this->getThumbnail($image, 1680, null, true),
228 * Get the thumbnail for an image.
229 * If $keepRatio is true only the width will be used.
230 * Checks the cache then storage to avoid creating / accessing the filesystem on every check.
234 protected function getThumbnail(Image $image, ?int $width = 220, ?int $height = 220, bool $keepRatio = false): ?string
237 return $this->imageService->getThumbnail($image, $width, $height, $keepRatio);
238 } catch (Exception $exception) {
244 * Get the raw image data from an Image.
246 public function getImageData(Image $image): ?string
249 return $this->imageService->getImageData($image);
250 } catch (Exception $exception) {
256 * Get the user visible pages using the given image.
258 public function getPagesUsingImage(Image $image): array
260 $pages = Page::visible()
261 ->where('html', 'like', '%' . $image->url . '%')
262 ->get(['id', 'name', 'slug', 'book_id']);
264 foreach ($pages as $page) {
265 $page->url = $page->getUrl();
268 return $pages->all();