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;
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,18 +87,11 @@ 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';
});
if (empty($files)) {
return response()->json([
'status' => 'error',
@@ -91,13 +102,11 @@ class SapFileController extends Controller
], 404);
}
$data = [];
foreach ($files as $file) {
$filePath = $path . '/' . $file; // ✅ Correct
foreach ($files as $file)
{
$filePath = $path . '/' . $file;
// Read file content safely
$content = file_get_contents($filePath);
$data[] = [