diff --git a/config/nova-tinymce-editor.php b/config/nova-tinymce-editor.php index ad71e06..7ef62cc 100644 --- a/config/nova-tinymce-editor.php +++ b/config/nova-tinymce-editor.php @@ -55,7 +55,7 @@ */ 'extra' => [ 'upload_images' => [ - 'enabled' => false, // Set true for enable images local upload + 'enable_api_routes' => env('TINYMCE_ENABLE_UPLOAD_API_ROUTES', true), 'folder' => 'images', 'maxSize' => 2048, // KB, 'disk' => 'public', diff --git a/routes/api.php b/routes/api.php index c6c12f9..b3bfca7 100644 --- a/routes/api.php +++ b/routes/api.php @@ -1,11 +1,10 @@ name('tinymce.upload') -// ->withoutMiddleware([VerifyCsrfToken::class]) -// ->middleware(TinymceMiddleware::class); +if (config('nova-tinymce-editor.extra.upload_images.enable_api_routes', true)) { + Route::post('/upload', TinyImageController::class)->name('tinymce.upload') + ->middleware('auth'); +} diff --git a/src/Http/Controllers/TinyImageController.php b/src/Http/Controllers/TinyImageController.php index ea51bad..4669a77 100644 --- a/src/Http/Controllers/TinyImageController.php +++ b/src/Http/Controllers/TinyImageController.php @@ -14,13 +14,47 @@ public function __invoke(Request $request): JsonResponse 'file' => [ 'required', 'image', - 'mimes:jpeg,png,jpg,gif', + 'mimes:jpeg,png,jpg,webp', 'max:'.config('nova-tinymce-editor.extra.upload_images.maxSize', 2048), ], ]); + + $uploadedFile = $request->file('file'); + + // Check if the uploaded file is a valid image + $imageInfo = @getimagesize($uploadedFile->getRealPath()); + if ($imageInfo === false) { + return response()->json(['error' => 'The file is not a valid image.'], 422); + } + + // Check MIME type + $allowedMimeTypes = ['image/jpeg', 'image/png', 'image/jpg', 'image/webp']; + if (! in_array($imageInfo['mime'], $allowedMimeTypes)) { + return response()->json(['error' => 'Unsupported image type.'], 422); + } + + // Check for potentially dangerous content + $fileContent = file_get_contents($uploadedFile->getRealPath()); + $dangerousPatterns = [ + '/<\?php/i', + '/<\?=/i', + '/