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