]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Images/DrawioImageController.php
Default OpenID display name set to standard value
[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 response()->json($imgData);
34     }
35
36     /**
37      * Store a new gallery image in the system.
38      * @throws Exception
39      */
40     public function create(Request $request)
41     {
42         $this->validate($request, [
43             'image' => 'required|string',
44             'uploaded_to' => 'required|integer'
45         ]);
46
47         $this->checkPermission('image-create-all');
48         $imageBase64Data = $request->get('image');
49
50         try {
51             $uploadedTo = $request->get('uploaded_to', 0);
52             $image = $this->imageRepo->saveDrawing($imageBase64Data, $uploadedTo);
53         } catch (ImageUploadException $e) {
54             return response($e->getMessage(), 500);
55         }
56
57         return response()->json($image);
58     }
59
60     /**
61      * Get the content of an image based64 encoded.
62      */
63     public function getAsBase64($id)
64     {
65         $image = $this->imageRepo->getById($id);
66         $page = $image->getPage();
67         if ($image === null || $image->type !== 'drawio' || !userCan('page-view', $page)) {
68             return $this->jsonError("Image data could not be found");
69         }
70
71         $imageData = $this->imageRepo->getImageData($image);
72         if ($imageData === null) {
73             return $this->jsonError("Image data could not be found");
74         }
75         return response()->json([
76             'content' => base64_encode($imageData)
77         ]);
78     }
79 }