| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											2023-10-01 20:05:18 +08:00
										 |  |  | use BookStack\Uploads\ImageResizer; | 
					
						
							| 
									
										
										
										
											2023-10-01 01:28:42 +08:00
										 |  |  | use BookStack\Util\OutOfMemoryHandler; | 
					
						
							| 
									
										
										
										
											2019-04-27 21:18:00 +08:00
										 |  |  | use Illuminate\Http\Request; | 
					
						
							| 
									
										
										
										
											2020-07-25 07:20:58 +08:00
										 |  |  | use Illuminate\Validation\ValidationException; | 
					
						
							| 
									
										
										
										
											2019-04-27 21:18:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class GalleryImageController extends Controller | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-03-14 20:19:19 +08:00
										 |  |  |     public function __construct( | 
					
						
							|  |  |  |         protected ImageRepo $imageRepo | 
					
						
							|  |  |  |     ) { | 
					
						
							| 
									
										
										
										
											2019-04-27 21:18:00 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get a list of gallery images, in a list. | 
					
						
							|  |  |  |      * Can be paged and filtered by entity. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-10-01 20:05:18 +08:00
										 |  |  |     public function list(Request $request, ImageResizer $resizer) | 
					
						
							| 
									
										
										
										
											2019-04-27 21:18:00 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         $page = $request->get('page', 1); | 
					
						
							|  |  |  |         $searchTerm = $request->get('search', null); | 
					
						
							|  |  |  |         $uploadedToFilter = $request->get('uploaded_to', null); | 
					
						
							|  |  |  |         $parentTypeFilter = $request->get('filter_type', null); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-26 21:30:59 +08:00
										 |  |  |         $imgData = $this->imageRepo->getEntityFiltered('gallery', $parentTypeFilter, $page, 30, $uploadedToFilter, $searchTerm); | 
					
						
							| 
									
										
										
										
											2023-10-01 20:05:18 +08:00
										 |  |  |         $viewData = [ | 
					
						
							|  |  |  |             'warning' => '', | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |             'images'  => $imgData['images'], | 
					
						
							| 
									
										
										
										
											2020-07-25 07:20:58 +08:00
										 |  |  |             'hasMore' => $imgData['has_more'], | 
					
						
							| 
									
										
										
										
											2023-10-01 20:05:18 +08:00
										 |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         new OutOfMemoryHandler(function () use ($viewData) { | 
					
						
							|  |  |  |             $viewData['warning'] = trans('errors.image_gallery_thumbnail_memory_limit'); | 
					
						
							|  |  |  |             return response()->view('pages.parts.image-manager-list', $viewData, 200); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $resizer->loadGalleryThumbnailsForMany($imgData['images']); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return view('pages.parts.image-manager-list', $viewData); | 
					
						
							| 
									
										
										
										
											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-07-25 07:20:58 +08:00
										 |  |  |      * @throws ValidationException | 
					
						
							| 
									
										
										
										
											2019-04-27 21:18:00 +08:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function create(Request $request) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->checkPermission('image-create-all'); | 
					
						
							| 
									
										
										
										
											2023-04-24 23:19:20 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             $this->validate($request, [ | 
					
						
							|  |  |  |                 'file' => $this->getImageValidationRules(), | 
					
						
							|  |  |  |             ]); | 
					
						
							|  |  |  |         } catch (ValidationException $exception) { | 
					
						
							|  |  |  |             return $this->jsonError(implode("\n", $exception->errors()['file'])); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-04-27 21:18:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-01 01:28:42 +08:00
										 |  |  |         new OutOfMemoryHandler(function () { | 
					
						
							|  |  |  |             return $this->jsonError(trans('errors.image_upload_memory_limit')); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-27 21:18:00 +08:00
										 |  |  |         try { | 
					
						
							|  |  |  |             $imageUpload = $request->file('file'); | 
					
						
							|  |  |  |             $uploadedTo = $request->get('uploaded_to', 0); | 
					
						
							|  |  |  |             $image = $this->imageRepo->saveNew($imageUpload, 'gallery', $uploadedTo); | 
					
						
							|  |  |  |         } catch (ImageUploadException $e) { | 
					
						
							|  |  |  |             return response($e->getMessage(), 500); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return response()->json($image); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |