Refactor readFiles method in SapFileController for improved error handling and code clarity
This commit is contained in:
@@ -33,28 +33,42 @@ 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';
|
||||
});
|
||||
|
||||
$files = array_filter(scandir($path), fn($file) =>
|
||||
$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);
|
||||
}
|
||||
|
||||
$data = [];
|
||||
|
||||
$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([
|
||||
'status' => 'success',
|
||||
'files' => $data,
|
||||
'files' => $data,
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user