namespace BookStack\Uploads\Controllers;
-use BookStack\Entities\Models\Page;
+use BookStack\Entities\Queries\PageQueries;
use BookStack\Http\ApiController;
+use BookStack\Permissions\Permission;
use BookStack\Uploads\Image;
use BookStack\Uploads\ImageRepo;
+use BookStack\Uploads\ImageResizer;
use Illuminate\Http\Request;
class ImageGalleryApiController extends ApiController
];
public function __construct(
- protected ImageRepo $imageRepo
+ protected ImageRepo $imageRepo,
+ protected ImageResizer $imageResizer,
+ protected PageQueries $pageQueries,
) {
}
*/
public function create(Request $request)
{
- $this->checkPermission('image-create-all');
+ $this->checkPermission(Permission::ImageCreateAll);
$data = $this->validate($request, $this->rules()['create']);
- Page::visible()->findOrFail($data['uploaded_to']);
+ $page = $this->pageQueries->findVisibleByIdOrFail($data['uploaded_to']);
- $image = $this->imageRepo->saveNew($data['image'], $data['type'], $data['uploaded_to']);
+ $image = $this->imageRepo->saveNew($data['image'], $data['type'], $page->id);
if (isset($data['name'])) {
$image->refresh();
{
$data = $this->validate($request, $this->rules()['update']);
$image = $this->imageRepo->getById($id);
- $this->checkOwnablePermission('page-view', $image->getPage());
- $this->checkOwnablePermission('image-update', $image);
+ $this->checkOwnablePermission(Permission::PageView, $image->getPage());
+ $this->checkOwnablePermission(Permission::ImageUpdate, $image);
$this->imageRepo->updateImageDetails($image, $data);
if (isset($data['image'])) {
public function delete(string $id)
{
$image = $this->imageRepo->getById($id);
- $this->checkOwnablePermission('page-view', $image->getPage());
- $this->checkOwnablePermission('image-delete', $image);
+ $this->checkOwnablePermission(Permission::PageView, $image->getPage());
+ $this->checkOwnablePermission(Permission::ImageDelete, $image);
$this->imageRepo->destroyImage($image);
return response('', 204);
*/
protected function formatForSingleResponse(Image $image): array
{
- $this->imageRepo->loadThumbs($image);
+ $this->imageResizer->loadGalleryThumbnailsForImage($image, false);
$data = $image->toArray();
$data['created_by'] = $image->createdBy;
$data['updated_by'] = $image->updatedBy;
$escapedUrl = htmlentities($image->url);
$escapedName = htmlentities($image->name);
+
if ($image->type === 'drawio') {
$data['content']['html'] = "<div drawio-diagram=\"{$image->id}\"><img src=\"{$escapedUrl}\"></div>";
$data['content']['markdown'] = $data['content']['html'];
} else {
- $escapedDisplayThumb = htmlentities($image->thumbs['display']);
+ $escapedDisplayThumb = htmlentities($image->getAttribute('thumbs')['display']);
$data['content']['html'] = "<a href=\"{$escapedUrl}\" target=\"_blank\"><img src=\"{$escapedDisplayThumb}\" alt=\"{$escapedName}\"></a>";
$mdEscapedName = str_replace(']', '', str_replace('[', '', $image->name));
- $mdEscapedThumb = str_replace(']', '', str_replace('[', '', $image->thumbs['display']));
+ $mdEscapedThumb = str_replace(']', '', str_replace('[', '', $image->getAttribute('thumbs')['display']));
$data['content']['markdown'] = "";
}