diff --git a/public/index.php b/public/index.php index adbd460..b3881ee 100644 --- a/public/index.php +++ b/public/index.php @@ -170,7 +170,6 @@ $app->post('/api/table/delete', function (Request $request, Response $response) return $response->withHeader('Content-Type', 'application/json'); }); -// API: импорт CSV (массовая вставка) // API: импорт CSV (массовая вставка) $app->post('/api/table/import-csv', function (Request $request, Response $response) use ($container) { $body = (string)$request->getBody(); @@ -200,21 +199,45 @@ $app->post('/api/table/import-csv', function (Request $request, Response $respon $metaArr = $meta->getTableMeta($schema, $table); $result = $ds->insertMultipleRows($schema, $table, $rows, $metaArr['columns']); - $response->getBody()->write(json_encode($result)); + // ✅ Очищаем невалидные UTF-8 символы в сообщениях об ошибках + if (!empty($result['errorMessages'])) { + $result['errorMessages'] = array_map(function($msg) { + // Конвертируем в UTF-8 и удаляем невалидные символы + return mb_convert_encoding($msg, 'UTF-8', 'UTF-8'); + }, $result['errorMessages']); + } + + // ✅ Проверяем, что json_encode работает + $json = json_encode($result, JSON_UNESCAPED_UNICODE | JSON_PARTIAL_OUTPUT_ON_ERROR); + + if ($json === false) { + error_log("CSV Import: JSON encoding failed - " . json_last_error_msg()); + + // Возвращаем упрощённую версию без детальных ошибок + $json = json_encode([ + 'inserted' => $result['inserted'] ?? 0, + 'errors' => $result['errors'] ?? 0, + 'errorMessages' => ['Ошибка JSON сериализации. Проверьте логи сервера.'] + ], JSON_UNESCAPED_UNICODE); + } + + $response->getBody()->write($json); return $response->withHeader('Content-Type', 'application/json'); } catch (\Exception $e) { error_log("CSV Import: Critical error - " . $e->getMessage()); + error_log("Stack trace: " . $e->getTraceAsString()); $response->getBody()->write(json_encode([ - 'error' => $e->getMessage(), + 'error' => mb_convert_encoding($e->getMessage(), 'UTF-8', 'UTF-8'), 'inserted' => 0, 'errors' => 1 - ])); + ], JSON_UNESCAPED_UNICODE)); return $response->withHeader('Content-Type', 'application/json')->withStatus(500); } }); + // API: экспорт CSV $app->post('/api/table/export-csv', function (Request $request, Response $response) use ($container) { $payload = json_decode((string)$request->getBody(), true);