X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/c7fea8fe08e8d26840da7a0d353c05f7a84d3ff1..refs/pull/4467/head:/app/Uploads/ImageRepo.php diff --git a/app/Uploads/ImageRepo.php b/app/Uploads/ImageRepo.php index 4d46bb56d..5507933f3 100644 --- a/app/Uploads/ImageRepo.php +++ b/app/Uploads/ImageRepo.php @@ -2,27 +2,19 @@ namespace BookStack\Uploads; -use BookStack\Auth\Permissions\PermissionService; use BookStack\Entities\Models\Page; use BookStack\Exceptions\ImageUploadException; +use BookStack\Permissions\PermissionApplicator; use Exception; use Illuminate\Database\Eloquent\Builder; use Symfony\Component\HttpFoundation\File\UploadedFile; class ImageRepo { - protected $imageService; - protected $restrictionService; - - /** - * ImageRepo constructor. - */ public function __construct( - ImageService $imageService, - PermissionService $permissionService + protected ImageService $imageService, + protected PermissionApplicator $permissions ) { - $this->imageService = $imageService; - $this->restrictionService = $permissionService; } /** @@ -43,7 +35,7 @@ class ImageRepo $hasMore = count($images) > $pageSize; $returnImages = $images->take($pageSize); - $returnImages->each(function ($image) { + $returnImages->each(function (Image $image) { $this->loadThumbs($image); }); @@ -76,7 +68,7 @@ class ImageRepo } // Filter by page access - $imageQuery = $this->restrictionService->filterRelatedEntity(Page::class, $imageQuery, 'images', 'uploaded_to'); + $imageQuery = $this->permissions->restrictPageRelationQuery($imageQuery, 'images', 'uploaded_to'); if ($whereClause !== null) { $imageQuery = $imageQuery->where($whereClause); @@ -96,6 +88,7 @@ class ImageRepo int $uploadedTo = null, string $search = null ): array { + /** @var Page $contextPage */ $contextPage = Page::visible()->findOrFail($uploadedTo); $parentFilter = null; @@ -104,7 +97,10 @@ class ImageRepo if ($filterType === 'page') { $query->where('uploaded_to', '=', $contextPage->id); } elseif ($filterType === 'book') { - $validPageIds = $contextPage->book->pages()->visible()->get(['id'])->pluck('id')->toArray(); + $validPageIds = $contextPage->book->pages() + ->scopes('visible') + ->pluck('id') + ->toArray(); $query->whereIn('uploaded_to', $validPageIds); } }; @@ -121,7 +117,10 @@ class ImageRepo public function saveNew(UploadedFile $uploadFile, string $type, int $uploadedTo = 0, int $resizeWidth = null, int $resizeHeight = null, bool $keepRatio = true): Image { $image = $this->imageService->saveNewFromUpload($uploadFile, $type, $uploadedTo, $resizeWidth, $resizeHeight, $keepRatio); - $this->loadThumbs($image); + + if ($type !== 'system') { + $this->loadThumbs($image); + } return $image; } @@ -131,7 +130,7 @@ class ImageRepo * * @throws ImageUploadException */ - public function saveNewFromData(string $imageName, string $imageData, string $type, int $uploadedTo = 0) + public function saveNewFromData(string $imageName, string $imageData, string $type, int $uploadedTo = 0): Image { $image = $this->imageService->saveNew($imageName, $imageData, $type, $uploadedTo); $this->loadThumbs($image); @@ -140,13 +139,13 @@ class ImageRepo } /** - * Save a drawing the the database. + * Save a drawing in the database. * * @throws ImageUploadException */ public function saveDrawing(string $base64Uri, int $uploadedTo): Image { - $name = 'Drawing-' . strval(user()->id) . '-' . strval(time()) . '.png'; + $name = 'Drawing-' . user()->id . '-' . time() . '.png'; return $this->imageService->saveNewFromBase64Uri($base64Uri, $name, 'drawio', $uploadedTo); } @@ -154,40 +153,60 @@ class ImageRepo /** * Update the details of an image via an array of properties. * - * @throws ImageUploadException * @throws Exception */ public function updateImageDetails(Image $image, $updateDetails): Image { $image->fill($updateDetails); + $image->updated_by = user()->id; $image->save(); $this->loadThumbs($image); return $image; } + /** + * Update the image file of an existing image in the system. + * @throws ImageUploadException + */ + public function updateImageFile(Image $image, UploadedFile $file): void + { + if ($file->getClientOriginalExtension() !== pathinfo($image->path, PATHINFO_EXTENSION)) { + throw new ImageUploadException(trans('errors.image_upload_replace_type')); + } + + $image->refresh(); + $image->updated_by = user()->id; + $image->touch(); + $image->save(); + $this->imageService->replaceExistingFromUpload($image->path, $image->type, $file); + $this->loadThumbs($image, true); + } + /** * Destroys an Image object along with its revisions, files and thumbnails. * * @throws Exception */ - public function destroyImage(Image $image = null): bool + public function destroyImage(Image $image = null): void { if ($image) { $this->imageService->destroy($image); } - - return true; } /** - * Destroy all images of a certain type. + * Destroy images that have a specific URL and type combination. * * @throws Exception */ - public function destroyByType(string $imageType) + public function destroyByUrlAndType(string $url, string $imageType): void { - $images = Image::query()->where('type', '=', $imageType)->get(); + $images = Image::query() + ->where('url', '=', $url) + ->where('type', '=', $imageType) + ->get(); + foreach ($images as $image) { $this->destroyImage($image); } @@ -195,14 +214,12 @@ class ImageRepo /** * Load thumbnails onto an image object. - * - * @throws Exception */ - public function loadThumbs(Image $image) + public function loadThumbs(Image $image, bool $forceCreate = false): void { $image->setAttribute('thumbs', [ - 'gallery' => $this->getThumbnail($image, 150, 150, false), - 'display' => $this->getThumbnail($image, 1680, null, true), + 'gallery' => $this->getThumbnail($image, 150, 150, false, $forceCreate), + 'display' => $this->getThumbnail($image, 1680, null, true, $forceCreate), ]); } @@ -210,13 +227,11 @@ class ImageRepo * Get the thumbnail for an image. * If $keepRatio is true only the width will be used. * Checks the cache then storage to avoid creating / accessing the filesystem on every check. - * - * @throws Exception */ - protected function getThumbnail(Image $image, ?int $width = 220, ?int $height = 220, bool $keepRatio = false): ?string + protected function getThumbnail(Image $image, ?int $width, ?int $height, bool $keepRatio, bool $forceCreate): ?string { try { - return $this->imageService->getThumbnail($image, $width, $height, $keepRatio); + return $this->imageService->getThumbnail($image, $width, $height, $keepRatio, $forceCreate); } catch (Exception $exception) { return null; } @@ -244,7 +259,7 @@ class ImageRepo ->get(['id', 'name', 'slug', 'book_id']); foreach ($pages as $page) { - $page->url = $page->getUrl(); + $page->setAttribute('url', $page->getUrl()); } return $pages->all();