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