Refactor readFiles method in SapFileController for improved error handling and code clarity
This commit is contained in:
@@ -33,23 +33,37 @@ class SapFileController extends Controller
|
|||||||
$path = "/srv/pds.iotsignin.com/sapftp/In/";
|
$path = "/srv/pds.iotsignin.com/sapftp/In/";
|
||||||
|
|
||||||
// $files = scandir($path);
|
// $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 = [];
|
$data = [];
|
||||||
|
|
||||||
foreach ($files as $file) {
|
foreach ($files as $file) {
|
||||||
|
$filePath = $path . $file;
|
||||||
|
|
||||||
|
// Read file content safely
|
||||||
|
$content = file_get_contents($filePath);
|
||||||
|
|
||||||
$data[] = [
|
$data[] = [
|
||||||
'filename' => $file,
|
'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([
|
return response()->json([
|
||||||
|
|||||||
Reference in New Issue
Block a user