]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Images/GalleryImageController.php
c01eccfee30193a5fc95f85f5a2630e190846569
[bookstack] / app / Http / Controllers / Images / GalleryImageController.php
1 <?php
2
3 namespace BookStack\Http\Controllers\Images;
4
5 use BookStack\Exceptions\ImageUploadException;
6 use BookStack\Http\Controllers\Controller;
7 use BookStack\Uploads\ImageRepo;
8 use Illuminate\Http\Request;
9 use Illuminate\Validation\ValidationException;
10
11 class GalleryImageController extends Controller
12 {
13     public function __construct(
14         protected ImageRepo $imageRepo
15     ) {
16     }
17
18     /**
19      * Get a list of gallery images, in a list.
20      * Can be paged and filtered by entity.
21      */
22     public function list(Request $request)
23     {
24         $page = $request->get('page', 1);
25         $searchTerm = $request->get('search', null);
26         $uploadedToFilter = $request->get('uploaded_to', null);
27         $parentTypeFilter = $request->get('filter_type', null);
28
29         $imgData = $this->imageRepo->getEntityFiltered('gallery', $parentTypeFilter, $page, 24, $uploadedToFilter, $searchTerm);
30
31         return view('pages.parts.image-manager-list', [
32             'images'  => $imgData['images'],
33             'hasMore' => $imgData['has_more'],
34         ]);
35     }
36
37     /**
38      * Store a new gallery image in the system.
39      *
40      * @throws ValidationException
41      */
42     public function create(Request $request)
43     {
44         $this->checkPermission('image-create-all');
45
46         try {
47             $this->validate($request, [
48                 'file' => $this->getImageValidationRules(),
49             ]);
50         } catch (ValidationException $exception) {
51             return $this->jsonError(implode("\n", $exception->errors()['file']));
52         }
53
54         try {
55             $imageUpload = $request->file('file');
56             $uploadedTo = $request->get('uploaded_to', 0);
57             $image = $this->imageRepo->saveNew($imageUpload, 'gallery', $uploadedTo);
58         } catch (ImageUploadException $e) {
59             return response($e->getMessage(), 500);
60         }
61
62         return response()->json($image);
63     }
64 }