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