]> BookStack Code Mirror - bookstack/blob - app/Uploads/Controllers/DrawioImageController.php
Images: Started refactor of image service
[bookstack] / app / Uploads / Controllers / DrawioImageController.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 Exception;
9 use Illuminate\Http\Request;
10
11 class DrawioImageController 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('drawio', $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 Exception
41      */
42     public function create(Request $request)
43     {
44         $this->validate($request, [
45             'image'       => ['required', 'string'],
46             'uploaded_to' => ['required', 'integer'],
47         ]);
48
49         $this->checkPermission('image-create-all');
50         $imageBase64Data = $request->get('image');
51
52         try {
53             $uploadedTo = $request->get('uploaded_to', 0);
54             $image = $this->imageRepo->saveDrawing($imageBase64Data, $uploadedTo);
55         } catch (ImageUploadException $e) {
56             return response($e->getMessage(), 500);
57         }
58
59         return response()->json($image);
60     }
61
62     /**
63      * Get the content of an image based64 encoded.
64      */
65     public function getAsBase64($id)
66     {
67         try {
68             $image = $this->imageRepo->getById($id);
69         } catch (Exception $exception) {
70             return $this->jsonError(trans('errors.drawing_data_not_found'), 404);
71         }
72
73         if ($image->type !== 'drawio' || !userCan('page-view', $image->getPage())) {
74             return $this->jsonError(trans('errors.drawing_data_not_found'), 404);
75         }
76
77         $imageData = $this->imageRepo->getImageData($image);
78         if (is_null($imageData)) {
79             return $this->jsonError(trans('errors.drawing_data_not_found'), 404);
80         }
81
82         return response()->json([
83             'content' => base64_encode($imageData),
84         ]);
85     }
86 }