From 3d219750966217d5478baab46bfb93f5396cc728 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Fri, 26 Sep 2025 10:43:52 +0530 Subject: [PATCH] Validate plant code in readFiles method and update response structure --- app/Http/Controllers/SapFileController.php | 45 +++++++++++++--------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/app/Http/Controllers/SapFileController.php b/app/Http/Controllers/SapFileController.php index 4fdc481..77b8a65 100644 --- a/app/Http/Controllers/SapFileController.php +++ b/app/Http/Controllers/SapFileController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers; use Illuminate\Http\Request; +use Str; class SapFileController extends Controller { @@ -42,7 +43,24 @@ class SapFileController extends Controller ], 403); } - $path = "/LaserPRD"; + $plantCode = $request->header('plant-code'); + + if ($plantCode == null || $plantCode == '') + { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Plant code can't be empty!" + ], 400); + } + else if (Str::length($plantCode) < 4 || !is_numeric($plantCode) || !preg_match('/^[1-9]\d{3,}$/', $plantCode)) + { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Invalid plant code found!" + ], 400); + } + + $path = "/LaserPRD/{$plantCode}"; $isDir = is_dir($path); $isReadable = is_readable($path); @@ -69,17 +87,10 @@ class SapFileController extends Controller ], 500); } - // Filter only .txt files - // $files = array_filter($allFiles, function($file) use ($path) { - // return $file !== '.' && $file !== '..' && is_file($path . $file) - // && pathinfo($file, PATHINFO_EXTENSION) === 'txt'; - // }); - - $files = array_filter($allFiles, function($item) use ($path) { - $fullPath = $path . '/' . $item; - return @is_file($fullPath) && pathinfo($item, PATHINFO_EXTENSION) === 'txt'; - }); - + $files = array_filter($allFiles, function($item) use ($path) { + $fullPath = $path . '/' . $item; + return @is_file($fullPath) && pathinfo($item, PATHINFO_EXTENSION) === 'txt'; + }); if (empty($files)) { return response()->json([ @@ -91,13 +102,11 @@ class SapFileController extends Controller ], 404); } + $data = []; + foreach ($files as $file) + { + $filePath = $path . '/' . $file; - $data = []; - foreach ($files as $file) { - $filePath = $path . '/' . $file; // ✅ Correct - - - // Read file content safely $content = file_get_contents($filePath); $data[] = [