2019-05-04 10:48:15 -04:00
|
|
|
<?php namespace BookStack\Http\Controllers\Images;
|
|
|
|
|
|
|
|
use BookStack\Exceptions\ImageUploadException;
|
|
|
|
use BookStack\Http\Controllers\Controller;
|
|
|
|
use BookStack\Uploads\Image;
|
|
|
|
use BookStack\Uploads\ImageRepo;
|
2020-07-24 19:20:58 -04:00
|
|
|
use Exception;
|
2019-05-04 10:48:15 -04:00
|
|
|
use Illuminate\Filesystem\Filesystem as File;
|
|
|
|
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
|
|
|
|
{
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
public function showImage(string $path)
|
|
|
|
{
|
|
|
|
$path = storage_path('uploads/images/' . $path);
|
|
|
|
if (!file_exists($path)) {
|
|
|
|
abort(404);
|
|
|
|
}
|
|
|
|
|
|
|
|
return response()->file($path);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update image details
|
|
|
|
* @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, [
|
|
|
|
'name' => 'required|min:2|string'
|
|
|
|
]);
|
|
|
|
|
|
|
|
$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);
|
|
|
|
return view('components.image-manager-form', [
|
|
|
|
'image' => $image,
|
|
|
|
'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.
|
|
|
|
* @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);
|
|
|
|
return view('components.image-manager-form', [
|
|
|
|
'image' => $image,
|
|
|
|
'dependantPages' => $dependantPages ?? null,
|
|
|
|
]);
|
2019-05-04 10:48: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);
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|