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', 'status_description' => 'Failed to scan directory', ], 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', 'status_description' => 'No text files found', ], 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, // ]); $fileName = 'RMGLAS02-1725800-1.txt'; $lines = file($fileName, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); if (!$lines) { return response()->json(['status' => 'ERROR', 'message' => 'File is empty'], 400); } $table = 'class_characteristics'; $columns = Schema::getColumnListing($table); // returns lowercase column names $rows = []; foreach ($lines as $line) { $values = str_getcsv($line); // split by comma $row = []; foreach ($columns as $index => $column) { $value = $values[$index] ?? null; // if missing, set null if ($value === '') $value = null; // empty string -> null $row[$column] = $value; } $rows[] = $row; } // Insert into database foreach ($rows as $row) { \App\Models\ClassCharacteristic::create($row); } return response()->json([ 'status' => 'SUCCESS', 'rows_imported' => count($rows), ]); } /** * 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) { // } }