1 <?php namespace BookStack\Uploads;
3 use BookStack\Auth\Permissions\PermissionService;
4 use BookStack\Entities\Models\Page;
5 use BookStack\Exceptions\ImageUploadException;
7 use Illuminate\Database\Eloquent\Builder;
8 use Symfony\Component\HttpFoundation\File\UploadedFile;
14 protected $imageService;
15 protected $restrictionService;
19 * ImageRepo constructor.
21 public function __construct(
23 ImageService $imageService,
24 PermissionService $permissionService,
27 $this->image = $image;
28 $this->imageService = $imageService;
29 $this->restrictionService = $permissionService;
35 * Get an image with the given id.
37 public function getById($id): Image
39 return $this->image->findOrFail($id);
43 * Execute a paginated query, returning in a standard format.
44 * Also runs the query through the restriction system.
46 private function returnPaginated($query, $page = 1, $pageSize = 24): array
48 $images = $query->orderBy('created_at', 'desc')->skip($pageSize * ($page - 1))->take($pageSize + 1)->get();
49 $hasMore = count($images) > $pageSize;
51 $returnImages = $images->take($pageSize);
52 $returnImages->each(function ($image) {
53 $this->loadThumbs($image);
57 'images' => $returnImages,
58 'has_more' => $hasMore
63 * Fetch a list of images in a paginated format, filtered by image type.
64 * Can be filtered by uploaded to and also by name.
66 public function getPaginatedByType(
70 int $uploadedTo = null,
71 string $search = null,
72 callable $whereClause = null
74 $imageQuery = $this->image->newQuery()->where('type', '=', strtolower($type));
76 if ($uploadedTo !== null) {
77 $imageQuery = $imageQuery->where('uploaded_to', '=', $uploadedTo);
80 if ($search !== null) {
81 $imageQuery = $imageQuery->where('name', 'LIKE', '%' . $search . '%');
84 // Filter by page access
85 $imageQuery = $this->restrictionService->filterRelatedEntity(Page::class, $imageQuery, 'images', 'uploaded_to');
87 if ($whereClause !== null) {
88 $imageQuery = $imageQuery->where($whereClause);
91 return $this->returnPaginated($imageQuery, $page, $pageSize);
95 * Get paginated gallery images within a specific page or book.
97 public function getEntityFiltered(
99 string $filterType = null,
102 int $uploadedTo = null,
103 string $search = null
105 $contextPage = $this->page->findOrFail($uploadedTo);
106 $parentFilter = null;
108 if ($filterType === 'book' || $filterType === 'page') {
109 $parentFilter = function (Builder $query) use ($filterType, $contextPage) {
110 if ($filterType === 'page') {
111 $query->where('uploaded_to', '=', $contextPage->id);
112 } elseif ($filterType === 'book') {
113 $validPageIds = $contextPage->book->pages()->visible()->get(['id'])->pluck('id')->toArray();
114 $query->whereIn('uploaded_to', $validPageIds);
119 return $this->getPaginatedByType($type, $page, $pageSize, null, $search, $parentFilter);
123 * Save a new image into storage and return the new image.
124 * @throws ImageUploadException
126 public function saveNew(UploadedFile $uploadFile, string $type, int $uploadedTo = 0, int $resizeWidth = null, int $resizeHeight = null, bool $keepRatio = true): Image
128 $image = $this->imageService->saveNewFromUpload($uploadFile, $type, $uploadedTo, $resizeWidth, $resizeHeight, $keepRatio);
129 $this->loadThumbs($image);
134 * Save a new image from an existing image data string.
135 * @throws ImageUploadException
137 public function saveNewFromData(string $imageName, string $imageData, string $type, int $uploadedTo = 0)
139 $image = $this->imageService->saveNew($imageName, $imageData, $type, $uploadedTo);
140 $this->loadThumbs($image);
145 * Save a drawing the the database.
146 * @throws ImageUploadException
148 public function saveDrawing(string $base64Uri, int $uploadedTo): Image
150 $name = 'Drawing-' . strval(user()->id) . '-' . strval(time()) . '.png';
151 return $this->imageService->saveNewFromBase64Uri($base64Uri, $name, 'drawio', $uploadedTo);
156 * Update the details of an image via an array of properties.
157 * @throws ImageUploadException
160 public function updateImageDetails(Image $image, $updateDetails): Image
162 $image->fill($updateDetails);
164 $this->loadThumbs($image);
169 * Destroys an Image object along with its revisions, files and thumbnails.
172 public function destroyImage(Image $image = null): bool
175 $this->imageService->destroy($image);
181 * Destroy all images of a certain type.
184 public function destroyByType(string $imageType)
186 $images = $this->image->where('type', '=', $imageType)->get();
187 foreach ($images as $image) {
188 $this->destroyImage($image);
194 * Load thumbnails onto an image object.
197 public function loadThumbs(Image $image)
200 'gallery' => $this->getThumbnail($image, 150, 150, false),
201 'display' => $this->getThumbnail($image, 1680, null, true)
206 * Get the thumbnail for an image.
207 * If $keepRatio is true only the width will be used.
208 * Checks the cache then storage to avoid creating / accessing the filesystem on every check.
211 protected function getThumbnail(Image $image, ?int $width = 220, ?int $height = 220, bool $keepRatio = false): ?string
214 return $this->imageService->getThumbnail($image, $width, $height, $keepRatio);
215 } catch (Exception $exception) {
221 * Get the raw image data from an Image.
223 public function getImageData(Image $image): ?string
226 return $this->imageService->getImageData($image);
227 } catch (Exception $exception) {
233 * Get the user visible pages using the given image.
235 public function getPagesUsingImage(Image $image): array
237 $pages = Page::visible()
238 ->where('html', 'like', '%' . $image->url . '%')
239 ->get(['id', 'name', 'slug', 'book_id']);
241 foreach ($pages as $page) {
242 $page->url = $page->getUrl();
245 return $pages->all();