2021-06-26 11:23:15 -04:00
|
|
|
<?php
|
|
|
|
|
2023-05-17 12:56:55 -04:00
|
|
|
namespace BookStack\Uploads\Controllers;
|
2019-05-04 10:48:15 -04:00
|
|
|
|
|
|
|
use BookStack\Exceptions\ImageUploadException;
|
2021-05-08 13:49:58 -04:00
|
|
|
use BookStack\Exceptions\NotFoundException;
|
2023-05-18 15:53:39 -04:00
|
|
|
use BookStack\Http\Controller;
|
2019-05-04 10:48:15 -04:00
|
|
|
use BookStack\Uploads\Image;
|
|
|
|
use BookStack\Uploads\ImageRepo;
|
2021-10-31 19:53:17 -04:00
|
|
|
use BookStack\Uploads\ImageService;
|
2023-09-29 08:54:08 -04:00
|
|
|
use BookStack\Util\OutOfMemoryHandler;
|
2020-07-24 19:20:58 -04:00
|
|
|
use Exception;
|
2019-05-04 10:48:15 -04:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
|
|
class ImageController extends Controller
|
|
|
|
{
|
2023-05-28 12:32:22 -04:00
|
|
|
public function __construct(
|
|
|
|
protected ImageRepo $imageRepo,
|
|
|
|
protected 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
|
|
|
*/
|
2020-07-24 19:20:58 -04:00
|
|
|
public function update(Request $request, string $id)
|
2019-05-04 10:48:15 -04:00
|
|
|
{
|
2023-09-29 06:45:31 -04:00
|
|
|
$data = $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);
|
|
|
|
|
2023-09-29 06:45:31 -04:00
|
|
|
$image = $this->imageRepo->updateImageDetails($image, $data);
|
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
|
|
|
}
|
|
|
|
|
2023-05-28 12:32:22 -04:00
|
|
|
/**
|
|
|
|
* Update the file for an existing image.
|
|
|
|
*/
|
|
|
|
public function updateFile(Request $request, string $id)
|
|
|
|
{
|
|
|
|
$this->validate($request, [
|
|
|
|
'file' => ['required', 'file', ...$this->getImageValidationRules()],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$image = $this->imageRepo->getById($id);
|
|
|
|
$this->checkImagePermission($image);
|
|
|
|
$this->checkOwnablePermission('image-update', $image);
|
|
|
|
$file = $request->file('file');
|
|
|
|
|
2023-09-30 13:28:42 -04:00
|
|
|
new OutOfMemoryHandler(function () {
|
|
|
|
return $this->jsonError(trans('errors.image_upload_memory_limit'));
|
|
|
|
});
|
|
|
|
|
2023-05-28 12:32:22 -04:00
|
|
|
try {
|
|
|
|
$this->imageRepo->updateImageFile($image, $file);
|
|
|
|
} catch (ImageUploadException $exception) {
|
|
|
|
return $this->jsonError($exception->getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
return response('');
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-09-29 06:45:31 -04:00
|
|
|
$this->imageRepo->loadThumbs($image, false);
|
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
|
|
|
}
|
|
|
|
|
2023-09-29 08:54:08 -04:00
|
|
|
/**
|
|
|
|
* Rebuild the thumbnails for the given image.
|
|
|
|
*/
|
|
|
|
public function rebuildThumbnails(string $id)
|
|
|
|
{
|
|
|
|
$image = $this->imageRepo->getById($id);
|
|
|
|
$this->checkImagePermission($image);
|
|
|
|
$this->checkOwnablePermission('image-update', $image);
|
|
|
|
|
|
|
|
new OutOfMemoryHandler(function () {
|
|
|
|
return $this->jsonError(trans('errors.image_thumbnail_memory_limit'));
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->imageRepo->loadThumbs($image, true);
|
|
|
|
|
|
|
|
return response(trans('components.image_rebuild_thumbs_success'));
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|