BookStack/app/Uploads/Controllers/GalleryImageController.php

65 lines
1.9 KiB
PHP
Raw Normal View History

<?php
2023-05-17 12:56:55 -04:00
namespace BookStack\Uploads\Controllers;
use BookStack\Exceptions\ImageUploadException;
use BookStack\Http\Controller;
use BookStack\Uploads\ImageRepo;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
class GalleryImageController extends Controller
{
2023-03-14 08:19:19 -04:00
public function __construct(
protected ImageRepo $imageRepo
) {
}
/**
* Get a list of gallery images, in a list.
* Can be paged and filtered by entity.
*/
public function list(Request $request)
{
$page = $request->get('page', 1);
$searchTerm = $request->get('search', null);
$uploadedToFilter = $request->get('uploaded_to', null);
$parentTypeFilter = $request->get('filter_type', null);
$imgData = $this->imageRepo->getEntityFiltered('gallery', $parentTypeFilter, $page, 24, $uploadedToFilter, $searchTerm);
2021-06-26 11:23:15 -04:00
return view('pages.parts.image-manager-list', [
2021-06-26 11:23:15 -04:00
'images' => $imgData['images'],
'hasMore' => $imgData['has_more'],
]);
}
/**
* Store a new gallery image in the system.
2021-06-26 11:23:15 -04:00
*
* @throws ValidationException
*/
public function create(Request $request)
{
$this->checkPermission('image-create-all');
2023-04-24 11:19:20 -04:00
try {
$this->validate($request, [
'file' => $this->getImageValidationRules(),
]);
} catch (ValidationException $exception) {
return $this->jsonError(implode("\n", $exception->errors()['file']));
}
try {
$imageUpload = $request->file('file');
$uploadedTo = $request->get('uploaded_to', 0);
$image = $this->imageRepo->saveNew($imageUpload, 'gallery', $uploadedTo);
} catch (ImageUploadException $e) {
return response($e->getMessage(), 500);
}
return response()->json($image);
}
}