]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Images/DrawioImageController.php
Fix "Ubunto Mono" $mono type misspelling
[bookstack] / app / Http / Controllers / Images / DrawioImageController.php
1 <?php
2
3 namespace BookStack\Http\Controllers\Images;
4
5 use BookStack\Exceptions\ImageUploadException;
6 use BookStack\Uploads\ImageRepo;
7 use Exception;
8 use Illuminate\Http\Request;
9 use BookStack\Http\Controllers\Controller;
10
11 class DrawioImageController extends Controller
12 {
13     protected $imageRepo;
14
15     public function __construct(ImageRepo $imageRepo)
16     {
17         $this->imageRepo = $imageRepo;
18         parent::__construct();
19     }
20
21     /**
22      * Get a list of gallery images, in a list.
23      * Can be paged and filtered by entity.
24      */
25     public function list(Request $request)
26     {
27         $page = $request->get('page', 1);
28         $searchTerm = $request->get('search', null);
29         $uploadedToFilter = $request->get('uploaded_to', null);
30         $parentTypeFilter = $request->get('filter_type', null);
31
32         $imgData = $this->imageRepo->getEntityFiltered('drawio', $parentTypeFilter, $page, 24, $uploadedToFilter, $searchTerm);
33         return view('components.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      * @throws Exception
42      */
43     public function create(Request $request)
44     {
45         $this->validate($request, [
46             'image' => 'required|string',
47             'uploaded_to' => 'required|integer'
48         ]);
49
50         $this->checkPermission('image-create-all');
51         $imageBase64Data = $request->get('image');
52
53         try {
54             $uploadedTo = $request->get('uploaded_to', 0);
55             $image = $this->imageRepo->saveDrawing($imageBase64Data, $uploadedTo);
56         } catch (ImageUploadException $e) {
57             return response($e->getMessage(), 500);
58         }
59
60         return response()->json($image);
61     }
62
63     /**
64      * Get the content of an image based64 encoded.
65      */
66     public function getAsBase64($id)
67     {
68         $image = $this->imageRepo->getById($id);
69         $page = $image->getPage();
70         if ($image === null || $image->type !== 'drawio' || !userCan('page-view', $page)) {
71             return $this->jsonError("Image data could not be found");
72         }
73
74         $imageData = $this->imageRepo->getImageData($image);
75         if ($imageData === null) {
76             return $this->jsonError("Image data could not be found");
77         }
78
79         return response()->json([
80             'content' => base64_encode($imageData)
81         ]);
82     }
83 }