]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Images/CoverImageController.php
Started extraction of image controller to separate controllers
[bookstack] / app / Http / Controllers / Images / CoverImageController.php
1 <?php
2
3 namespace BookStack\Http\Controllers\Images;
4
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;
11
12 class CoverImageController extends Controller
13 {
14     protected $imageRepo;
15     protected $entityRepo;
16
17     /**
18      * CoverImageController constructor.
19      * @param ImageRepo $imageRepo
20      * @param EntityRepo $entityRepo
21      */
22     public function __construct(ImageRepo $imageRepo, EntityRepo $entityRepo)
23     {
24         $this->imageRepo = $imageRepo;
25         $this->entityRepo = $entityRepo;
26
27         parent::__construct();
28     }
29
30     /**
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
36      */
37     public function list(Request $request, $entity)
38     {
39         if (!$this->isValidEntityTypeForCover($entity)) {
40             return $this->jsonError(trans('errors.image_upload_type_error'));
41         }
42
43         $page = $request->get('page', 1);
44         $searchTerm = $request->get('search', null);
45
46         $type = 'cover_' . $entity;
47         $imgData = $this->imageRepo->getPaginatedByType($type, $page, 24, null, $searchTerm);
48         return response()->json($imgData);
49     }
50
51     /**
52      * Store a new cover image in the system.
53      * @param Request $request
54      * @param string $entity
55      * @return Illuminate\Http\JsonResponse
56      * @throws \Exception
57      */
58     public function create(Request $request, $entity)
59     {
60         $this->checkPermission('image-create-all');
61         $this->validate($request, [
62             'file' => $this->imageRepo->getImageValidationRules(),
63             'uploaded_to' => 'required|integer'
64         ]);
65
66         if (!$this->isValidEntityTypeForCover($entity)) {
67             return $this->jsonError(trans('errors.image_upload_type_error'));
68         }
69
70         $uploadedTo = $request->get('uploaded_to', 0);
71         $entityInstance = $this->entityRepo->getById($entity, $uploadedTo);
72         $this->checkOwnablePermission($entity . '-update', $entityInstance);
73
74         try {
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);
80         }
81
82         return response()->json($image);
83     }
84
85     /**
86      * Check if the given entity type is valid entity to have cover images.
87      * @param string $entityType
88      * @return bool
89      */
90     protected function isValidEntityTypeForCover(string $entityType)
91     {
92         return ($entityType === 'book' || $entityType === 'bookshelf');
93     }
94
95 }