header('Authorization'); $expectedToken = $expectedUser . ':' . $expectedPw; if ("Bearer " . $expectedToken != $header_auth) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'Invalid authorization token!' ], 403); } $plantCode = $request->header('plant-code'); if ($plantCode == null || $plantCode == '') { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Plant code can't be empty!" ], 400); } else if (Str::length($plantCode) < 4 || !is_numeric($plantCode) || !preg_match('/^[1-9]\d{3,}$/', $plantCode)) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Invalid plant code found!" ], 400); } $path = "/LaserPRD/{$plantCode}"; // $isDir = is_dir($path); // $isReadable = is_readable($path); // Check if folder exists if (!is_dir($path)) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Folder for plant-code {$plantCode} not found!" ], 404); } if (!is_readable($path)) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Folder for plant-code {$plantCode} exists but is not readable!" ], 500); } // Scan folder $allFiles = scandir($path); if ($allFiles === false) { return response()->json([ 'status' => 'error', 'message' => 'Failed to scan directory', 'debug' => ['path_checked' => $path] ], 500); } $files = array_filter($allFiles, function($item) use ($path) { $fullPath = $path . '/' . $item; return @is_file($fullPath) && pathinfo($item, PATHINFO_EXTENSION) === 'txt'; }); if (empty($files)) { return response()->json([ 'status' => 'error', 'message' => 'No text files found', 'debug' => [ 'scanned_files' => $allFiles ] ], 404); } $data = []; foreach ($files as $file) { $filePath = $path . '/' . $file; $content = file_get_contents($filePath); $data[] = [ 'filename' => $file, 'content' => $content, ]; } return response()->json([ 'status' => 'success', 'files' => $data, ]); } /** * Display the specified resource. */ public function show(string $id) { // } /** * Update the specified resource in storage. */ public function update(Request $request, string $id) { // } /** * Remove the specified resource from storage. */ public function destroy(string $id) { // } }