Refactor readFiles method in SapFileController for improved error handling and code clarity

This commit is contained in:
dhanabalan
2025-09-23 09:42:57 +05:30
parent 6a70b45f4f
commit 86ad06def7

View File

@@ -33,23 +33,37 @@ class SapFileController extends Controller
$path = "/srv/pds.iotsignin.com/sapftp/In/";
// $files = scandir($path);
if (!is_dir($path) || !is_readable($path)) {
return response()->json([
'status' => 'error',
'message' => 'Folder not accessible',
], 500);
}
$path = '/srv/pds.iotsignin.com/sapftp/In/';
$files = array_filter(scandir($path), function($file) use ($path) {
return $file !== '.' && $file !== '..' && is_file($path . $file)
&& pathinfo($file, PATHINFO_EXTENSION) === 'txt';
});
if (empty($files)) {
return response()->json([
'status' => 'error',
'message' => 'No text files found',
], 404);
}
$files = array_filter(scandir($path), fn($file) =>
$file !== '.' && $file !== '..' && is_file($path . $file) && pathinfo($file, PATHINFO_EXTENSION) === 'txt'
);
$data = [];
foreach ($files as $file) {
$filePath = $path . $file;
// Read file content safely
$content = file_get_contents($filePath);
$data[] = [
'filename' => $file,
'content' => file_get_contents($path . $file), // plain text content
'content' => $content,
];
// Optional: move processed file to Out
rename($path . $file, '/srv/pds.iotsignin.com/sapftp/Out/' . $file);
}
return response()->json([