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.
39 public function imageExtensionSupported(string $extension): bool
41 return in_array(trim($extension, '. \t\n\r\0\x0B'), static::$supportedExtensions);
45 * Get an image with the given id.
47 public function getById($id): Image
49 return $this->image->findOrFail($id);
53 * Execute a paginated query, returning in a standard format.
54 * Also runs the query through the restriction system.
56 private function returnPaginated($query, $page = 1, $pageSize = 24): array
58 $images = $query->orderBy('created_at', 'desc')->skip($pageSize * ($page - 1))->take($pageSize + 1)->get();
59 $hasMore = count($images) > $pageSize;
61 $returnImages = $images->take($pageSize);
62 $returnImages->each(function ($image) {
63 $this->loadThumbs($image);
67 'images' => $returnImages,
68 'has_more' => $hasMore,
73 * Fetch a list of images in a paginated format, filtered by image type.
74 * Can be filtered by uploaded to and also by name.
76 public function getPaginatedByType(
80 int $uploadedTo = null,
81 string $search = null,
82 callable $whereClause = null
84 $imageQuery = $this->image->newQuery()->where('type', '=', strtolower($type));
86 if ($uploadedTo !== null) {
87 $imageQuery = $imageQuery->where('uploaded_to', '=', $uploadedTo);
90 if ($search !== null) {
91 $imageQuery = $imageQuery->where('name', 'LIKE', '%' . $search . '%');
94 // Filter by page access
95 $imageQuery = $this->restrictionService->filterRelatedEntity(Page::class, $imageQuery, 'images', 'uploaded_to');
97 if ($whereClause !== null) {
98 $imageQuery = $imageQuery->where($whereClause);
101 return $this->returnPaginated($imageQuery, $page, $pageSize);
105 * Get paginated gallery images within a specific page or book.
107 public function getEntityFiltered(
109 string $filterType = null,
112 int $uploadedTo = null,
113 string $search = null
115 $contextPage = $this->page->findOrFail($uploadedTo);
116 $parentFilter = null;
118 if ($filterType === 'book' || $filterType === 'page') {
119 $parentFilter = function (Builder $query) use ($filterType, $contextPage) {
120 if ($filterType === 'page') {
121 $query->where('uploaded_to', '=', $contextPage->id);
122 } elseif ($filterType === 'book') {
123 $validPageIds = $contextPage->book->pages()->visible()->get(['id'])->pluck('id')->toArray();
124 $query->whereIn('uploaded_to', $validPageIds);
129 return $this->getPaginatedByType($type, $page, $pageSize, null, $search, $parentFilter);
133 * Save a new image into storage and return the new image.
135 * @throws ImageUploadException
137 public function saveNew(UploadedFile $uploadFile, string $type, int $uploadedTo = 0, int $resizeWidth = null, int $resizeHeight = null, bool $keepRatio = true): Image
139 $image = $this->imageService->saveNewFromUpload($uploadFile, $type, $uploadedTo, $resizeWidth, $resizeHeight, $keepRatio);
140 $this->loadThumbs($image);
146 * Save a new image from an existing image data string.
148 * @throws ImageUploadException
150 public function saveNewFromData(string $imageName, string $imageData, string $type, int $uploadedTo = 0)
152 $image = $this->imageService->saveNew($imageName, $imageData, $type, $uploadedTo);
153 $this->loadThumbs($image);
159 * Save a drawing the the database.
161 * @throws ImageUploadException
163 public function saveDrawing(string $base64Uri, int $uploadedTo): Image
165 $name = 'Drawing-' . strval(user()->id) . '-' . strval(time()) . '.png';
167 return $this->imageService->saveNewFromBase64Uri($base64Uri, $name, 'drawio', $uploadedTo);
171 * Update the details of an image via an array of properties.
173 * @throws ImageUploadException
176 public function updateImageDetails(Image $image, $updateDetails): Image
178 $image->fill($updateDetails);
180 $this->loadThumbs($image);
186 * Destroys an Image object along with its revisions, files and thumbnails.
190 public function destroyImage(Image $image = null): bool
193 $this->imageService->destroy($image);
200 * Destroy all images of a certain type.
204 public function destroyByType(string $imageType)
206 $images = $this->image->where('type', '=', $imageType)->get();
207 foreach ($images as $image) {
208 $this->destroyImage($image);
213 * Load thumbnails onto an image object.
217 public function loadThumbs(Image $image)
220 'gallery' => $this->getThumbnail($image, 150, 150, false),
221 'display' => $this->getThumbnail($image, 1680, null, true),
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.
232 protected function getThumbnail(Image $image, ?int $width = 220, ?int $height = 220, bool $keepRatio = false): ?string
235 return $this->imageService->getThumbnail($image, $width, $height, $keepRatio);
236 } catch (Exception $exception) {
242 * Get the raw image data from an Image.
244 public function getImageData(Image $image): ?string
247 return $this->imageService->getImageData($image);
248 } catch (Exception $exception) {
254 * Get the user visible pages using the given image.
256 public function getPagesUsingImage(Image $image): array
258 $pages = Page::visible()
259 ->where('html', 'like', '%' . $image->url . '%')
260 ->get(['id', 'name', 'slug', 'book_id']);
262 foreach ($pages as $page) {
263 $page->url = $page->getUrl();
266 return $pages->all();