From 6a70b45f4f695e22a2a2fc74c4eb625616b9bfde Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Tue, 23 Sep 2025 09:38:47 +0530 Subject: [PATCH] Refactor readFiles method in SapFileController for improved file handling and readability --- app/Http/Controllers/SapFileController.php | 30 ++++++++++------------ 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/app/Http/Controllers/SapFileController.php b/app/Http/Controllers/SapFileController.php index 05fc072..aeb103f 100644 --- a/app/Http/Controllers/SapFileController.php +++ b/app/Http/Controllers/SapFileController.php @@ -32,26 +32,24 @@ class SapFileController extends Controller $path = "/srv/pds.iotsignin.com/sapftp/In/"; - $files = scandir($path); + // $files = scandir($path); - $data = []; + $path = '/srv/pds.iotsignin.com/sapftp/In/'; + + $files = array_filter(scandir($path), fn($file) => + $file !== '.' && $file !== '..' && is_file($path . $file) && pathinfo($file, PATHINFO_EXTENSION) === 'txt' + ); + + $data = []; foreach ($files as $file) { - if ($file == '.' || $file == '..') continue; // skip system entries + $data[] = [ + 'filename' => $file, + 'content' => file_get_contents($path . $file), // plain text content + ]; - if (is_file($path . $file) && pathinfo($file, PATHINFO_EXTENSION) == 'txt') { - $data[] = [ - 'filename' => $file, - 'content' => file_get_contents($path . $file), - ]; - } - } - - if (empty($data)) { - return response()->json([ - 'status' => 'error', - 'message' => 'No text files found', - ], 404); + // Optional: move processed file to Out + rename($path . $file, '/srv/pds.iotsignin.com/sapftp/Out/' . $file); } return response()->json([