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