3 namespace BookStack\Http\Controllers\Images;
5 use BookStack\Entities\EntityProvider;
6 use BookStack\Entities\Repos\EntityRepo;
7 use BookStack\Exceptions\ImageUploadException;
8 use BookStack\Uploads\ImageRepo;
9 use Illuminate\Http\Request;
10 use BookStack\Http\Controllers\Controller;
12 class CoverImageController extends Controller
15 protected $entityRepo;
18 * CoverImageController constructor.
19 * @param ImageRepo $imageRepo
20 * @param EntityRepo $entityRepo
22 public function __construct(ImageRepo $imageRepo, EntityRepo $entityRepo)
24 $this->imageRepo = $imageRepo;
25 $this->entityRepo = $entityRepo;
27 parent::__construct();
31 * Get a list of cover images, in a list.
32 * Can be paged and filtered by entity.
33 * @param Request $request
34 * @param string $entity
35 * @return \Illuminate\Http\JsonResponse
37 public function list(Request $request, $entity)
39 if (!$this->isValidEntityTypeForCover($entity)) {
40 return $this->jsonError(trans('errors.image_upload_type_error'));
43 $page = $request->get('page', 1);
44 $searchTerm = $request->get('search', null);
46 $type = 'cover_' . $entity;
47 $imgData = $this->imageRepo->getPaginatedByType($type, $page, 24, null, $searchTerm);
48 return response()->json($imgData);
52 * Store a new cover image in the system.
53 * @param Request $request
54 * @param string $entity
55 * @return Illuminate\Http\JsonResponse
58 public function create(Request $request, $entity)
60 $this->checkPermission('image-create-all');
61 $this->validate($request, [
62 'file' => $this->imageRepo->getImageValidationRules(),
63 'uploaded_to' => 'required|integer'
66 if (!$this->isValidEntityTypeForCover($entity)) {
67 return $this->jsonError(trans('errors.image_upload_type_error'));
70 $uploadedTo = $request->get('uploaded_to', 0);
71 $entityInstance = $this->entityRepo->getById($entity, $uploadedTo);
72 $this->checkOwnablePermission($entity . '-update', $entityInstance);
75 $type = 'cover_' . $entity;
76 $imageUpload = $request->file('file');
77 $image = $this->imageRepo->saveNew($imageUpload, $type, $uploadedTo);
78 } catch (ImageUploadException $e) {
79 return response($e->getMessage(), 500);
82 return response()->json($image);
86 * Check if the given entity type is valid entity to have cover images.
87 * @param string $entityType
90 protected function isValidEntityTypeForCover(string $entityType)
92 return ($entityType === 'book' || $entityType === 'bookshelf');