| 
									
										
										
										
											2019-04-27 21:18:00 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-18 00:56:55 +08:00
										 |  |  | namespace BookStack\Uploads\Controllers; | 
					
						
							| 
									
										
										
										
											2019-04-27 21:18:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | use BookStack\Exceptions\ImageUploadException; | 
					
						
							| 
									
										
										
										
											2023-05-19 03:53:39 +08:00
										 |  |  | use BookStack\Http\Controller; | 
					
						
							| 
									
										
										
										
											2019-04-27 21:18:00 +08:00
										 |  |  | use BookStack\Uploads\ImageRepo; | 
					
						
							| 
									
										
										
										
											2020-04-06 00:27:16 +08:00
										 |  |  | use Exception; | 
					
						
							| 
									
										
										
										
											2019-04-27 21:18:00 +08:00
										 |  |  | use Illuminate\Http\Request; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class DrawioImageController extends Controller | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     protected $imageRepo; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function __construct(ImageRepo $imageRepo) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->imageRepo = $imageRepo; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get a list of gallery images, in a list. | 
					
						
							|  |  |  |      * Can be paged and filtered by entity. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function list(Request $request) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $page = $request->get('page', 1); | 
					
						
							|  |  |  |         $searchTerm = $request->get('search', null); | 
					
						
							|  |  |  |         $uploadedToFilter = $request->get('uploaded_to', null); | 
					
						
							|  |  |  |         $parentTypeFilter = $request->get('filter_type', null); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $imgData = $this->imageRepo->getEntityFiltered('drawio', $parentTypeFilter, $page, 24, $uploadedToFilter, $searchTerm); | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-22 20:15:58 +08:00
										 |  |  |         return view('pages.parts.image-manager-list', [ | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |             'images'  => $imgData['images'], | 
					
						
							| 
									
										
										
										
											2020-07-25 07:20:58 +08:00
										 |  |  |             'hasMore' => $imgData['has_more'], | 
					
						
							|  |  |  |         ]); | 
					
						
							| 
									
										
										
										
											2019-04-27 21:18:00 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Store a new gallery image in the system. | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2020-04-06 00:27:16 +08:00
										 |  |  |      * @throws Exception | 
					
						
							| 
									
										
										
										
											2019-04-27 21:18:00 +08:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function create(Request $request) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->validate($request, [ | 
					
						
							| 
									
										
										
										
											2021-11-05 08:26:55 +08:00
										 |  |  |             'image'       => ['required', 'string'], | 
					
						
							|  |  |  |             'uploaded_to' => ['required', 'integer'], | 
					
						
							| 
									
										
										
										
											2019-04-27 21:18:00 +08:00
										 |  |  |         ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->checkPermission('image-create-all'); | 
					
						
							|  |  |  |         $imageBase64Data = $request->get('image'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             $uploadedTo = $request->get('uploaded_to', 0); | 
					
						
							|  |  |  |             $image = $this->imageRepo->saveDrawing($imageBase64Data, $uploadedTo); | 
					
						
							|  |  |  |         } catch (ImageUploadException $e) { | 
					
						
							|  |  |  |             return response($e->getMessage(), 500); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return response()->json($image); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-04 22:48:15 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Get the content of an image based64 encoded. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getAsBase64($id) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-01-26 20:16:23 +08:00
										 |  |  |         try { | 
					
						
							|  |  |  |             $image = $this->imageRepo->getById($id); | 
					
						
							|  |  |  |         } catch (Exception $exception) { | 
					
						
							|  |  |  |             return $this->jsonError(trans('errors.drawing_data_not_found'), 404); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($image->type !== 'drawio' || !userCan('page-view', $image->getPage())) { | 
					
						
							|  |  |  |             return $this->jsonError(trans('errors.drawing_data_not_found'), 404); | 
					
						
							| 
									
										
										
										
											2019-05-04 22:48:15 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $imageData = $this->imageRepo->getImageData($image); | 
					
						
							| 
									
										
										
										
											2021-11-01 19:17:30 +08:00
										 |  |  |         if (is_null($imageData)) { | 
					
						
							| 
									
										
										
										
											2023-01-26 20:16:23 +08:00
										 |  |  |             return $this->jsonError(trans('errors.drawing_data_not_found'), 404); | 
					
						
							| 
									
										
										
										
											2019-05-04 22:48:15 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-07-25 07:20:58 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-04 22:48:15 +08:00
										 |  |  |         return response()->json([ | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |             'content' => base64_encode($imageData), | 
					
						
							| 
									
										
										
										
											2019-05-04 22:48:15 +08:00
										 |  |  |         ]); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-04-27 21:18:00 +08:00
										 |  |  | } |