Refactor readFiles method in SapFileController for improved file handling and readability
This commit is contained in:
@@ -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([
|
||||
|
||||
Reference in New Issue
Block a user