2021-06-26 15:23:15 +00:00
|
|
|
<?php
|
|
|
|
|
2023-05-17 16:56:55 +00:00
|
|
|
namespace BookStack\Uploads\Controllers;
|
2019-05-04 14:48:15 +00:00
|
|
|
|
|
|
|
use BookStack\Exceptions\ImageUploadException;
|
2021-05-08 17:49:58 +00:00
|
|
|
use BookStack\Exceptions\NotFoundException;
|
2023-05-18 19:53:39 +00:00
|
|
|
use BookStack\Http\Controller;
|
2019-05-04 14:48:15 +00:00
|
|
|
use BookStack\Uploads\Image;
|
|
|
|
use BookStack\Uploads\ImageRepo;
|
2021-10-31 23:53:17 +00:00
|
|
|
use BookStack\Uploads\ImageService;
|
2020-07-24 23:20:58 +00:00
|
|
|
use Exception;
|
2019-05-04 14:48:15 +00:00
|
|
|
use Illuminate\Http\Request;
|
2020-07-24 23:20:58 +00:00
|
|
|
use Illuminate\Validation\ValidationException;
|
2019-05-04 14:48:15 +00:00
|
|
|
|
|
|
|
class ImageController extends Controller
|
|
|
|
{
|
2022-09-05 14:52:12 +00:00
|
|
|
protected ImageRepo $imageRepo;
|
|
|
|
protected ImageService $imageService;
|
2022-09-06 16:41:32 +00:00
|
|
|
|
2021-11-01 00:24:42 +00:00
|
|
|
public function __construct(ImageRepo $imageRepo, ImageService $imageService)
|
2019-05-04 14:48:15 +00:00
|
|
|
{
|
|
|
|
$this->imageRepo = $imageRepo;
|
2021-10-31 23:53:17 +00:00
|
|
|
$this->imageService = $imageService;
|
2019-05-04 14:48:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provide an image file from storage.
|
2021-06-26 15:23:15 +00:00
|
|
|
*
|
2021-05-08 17:49:58 +00:00
|
|
|
* @throws NotFoundException
|
2019-05-04 14:48:15 +00:00
|
|
|
*/
|
|
|
|
public function showImage(string $path)
|
|
|
|
{
|
2022-09-01 15:17:14 +00:00
|
|
|
if (!$this->imageService->pathAccessibleInLocalSecure($path)) {
|
2021-05-08 17:49:58 +00:00
|
|
|
throw (new NotFoundException(trans('errors.image_not_found')))
|
|
|
|
->setSubtitle(trans('errors.image_not_found_subtitle'))
|
|
|
|
->setDetails(trans('errors.image_not_found_details'));
|
2019-05-04 14:48:15 +00:00
|
|
|
}
|
|
|
|
|
2021-10-31 23:53:17 +00:00
|
|
|
return $this->imageService->streamImageFromStorageResponse('gallery', $path);
|
2019-05-04 14:48:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-06-26 15:23:15 +00:00
|
|
|
* Update image details.
|
|
|
|
*
|
2019-05-04 14:48:15 +00:00
|
|
|
* @throws ImageUploadException
|
2020-07-24 23:20:58 +00:00
|
|
|
* @throws ValidationException
|
2019-05-04 14:48:15 +00:00
|
|
|
*/
|
2020-07-24 23:20:58 +00:00
|
|
|
public function update(Request $request, string $id)
|
2019-05-04 14:48:15 +00:00
|
|
|
{
|
|
|
|
$this->validate($request, [
|
2021-11-05 00:26:55 +00:00
|
|
|
'name' => ['required', 'min:2', 'string'],
|
2019-05-04 14:48:15 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
$image = $this->imageRepo->getById($id);
|
|
|
|
$this->checkImagePermission($image);
|
|
|
|
$this->checkOwnablePermission('image-update', $image);
|
|
|
|
|
|
|
|
$image = $this->imageRepo->updateImageDetails($image, $request->all());
|
2020-07-24 23:20:58 +00:00
|
|
|
|
|
|
|
$this->imageRepo->loadThumbs($image);
|
2021-06-26 15:23:15 +00:00
|
|
|
|
2021-08-22 12:15:58 +00:00
|
|
|
return view('pages.parts.image-manager-form', [
|
2021-06-26 15:23:15 +00:00
|
|
|
'image' => $image,
|
2020-07-24 23:20:58 +00:00
|
|
|
'dependantPages' => null,
|
|
|
|
]);
|
2019-05-04 14:48:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-07-24 23:20:58 +00:00
|
|
|
* Get the form for editing the given image.
|
2021-06-26 15:23:15 +00:00
|
|
|
*
|
2020-07-24 23:20:58 +00:00
|
|
|
* @throws Exception
|
2019-05-04 14:48:15 +00:00
|
|
|
*/
|
2020-07-24 23:20:58 +00:00
|
|
|
public function edit(Request $request, string $id)
|
2019-05-04 14:48:15 +00:00
|
|
|
{
|
|
|
|
$image = $this->imageRepo->getById($id);
|
|
|
|
$this->checkImagePermission($image);
|
2019-10-05 11:55:01 +00:00
|
|
|
|
2020-07-24 23:20:58 +00:00
|
|
|
if ($request->has('delete')) {
|
|
|
|
$dependantPages = $this->imageRepo->getPagesUsingImage($image);
|
2019-10-05 11:55:01 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 23:20:58 +00:00
|
|
|
$this->imageRepo->loadThumbs($image);
|
2021-06-26 15:23:15 +00:00
|
|
|
|
2021-08-22 12:15:58 +00:00
|
|
|
return view('pages.parts.image-manager-form', [
|
2021-06-26 15:23:15 +00:00
|
|
|
'image' => $image,
|
2020-07-24 23:20:58 +00:00
|
|
|
'dependantPages' => $dependantPages ?? null,
|
|
|
|
]);
|
2019-05-04 14:48:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-06-26 15:23:15 +00:00
|
|
|
* Deletes an image and all thumbnail/image files.
|
|
|
|
*
|
2020-07-24 23:20:58 +00:00
|
|
|
* @throws Exception
|
2019-05-04 14:48:15 +00:00
|
|
|
*/
|
2020-07-24 23:20:58 +00:00
|
|
|
public function destroy(string $id)
|
2019-05-04 14:48:15 +00:00
|
|
|
{
|
|
|
|
$image = $this->imageRepo->getById($id);
|
|
|
|
$this->checkOwnablePermission('image-delete', $image);
|
|
|
|
$this->checkImagePermission($image);
|
|
|
|
|
|
|
|
$this->imageRepo->destroyImage($image);
|
2021-06-26 15:23:15 +00:00
|
|
|
|
2020-07-24 23:20:58 +00:00
|
|
|
return response('');
|
2019-05-04 14:48:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check related page permission and ensure type is drawio or gallery.
|
|
|
|
*/
|
|
|
|
protected function checkImagePermission(Image $image)
|
|
|
|
{
|
2019-05-04 17:11:00 +00:00
|
|
|
if ($image->type !== 'drawio' && $image->type !== 'gallery') {
|
2019-05-04 14:48:15 +00:00
|
|
|
$this->showPermissionError();
|
|
|
|
}
|
|
|
|
|
|
|
|
$relatedPage = $image->getPage();
|
|
|
|
if ($relatedPage) {
|
|
|
|
$this->checkOwnablePermission('page-view', $relatedPage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|