Validate plant code in readFiles method and update response structure

This commit is contained in:
dhanabalan
2025-09-26 10:43:52 +05:30
parent 0710c3a7a1
commit 3d21975096

View File

@@ -3,6 +3,7 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Str;
class SapFileController extends Controller class SapFileController extends Controller
{ {
@@ -42,7 +43,24 @@ class SapFileController extends Controller
], 403); ], 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); $isDir = is_dir($path);
$isReadable = is_readable($path); $isReadable = is_readable($path);
@@ -69,17 +87,10 @@ class SapFileController extends Controller
], 500); ], 500);
} }
// Filter only .txt files $files = array_filter($allFiles, function($item) use ($path) {
// $files = array_filter($allFiles, function($file) use ($path) { $fullPath = $path . '/' . $item;
// return $file !== '.' && $file !== '..' && is_file($path . $file) return @is_file($fullPath) && pathinfo($item, PATHINFO_EXTENSION) === 'txt';
// && 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';
});
if (empty($files)) { if (empty($files)) {
return response()->json([ return response()->json([
@@ -91,13 +102,11 @@ class SapFileController extends Controller
], 404); ], 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); $content = file_get_contents($filePath);
$data[] = [ $data[] = [