From c79c3d17e27aa206b8edfa15bc162506e3b4d846 Mon Sep 17 00:00:00 2001 From: Thorsten Rinne Date: Sat, 4 Jan 2025 19:07:50 +0100 Subject: [PATCH] fix: changed from StreamResponse to StreamedJsonResponse (#3312) --- phpmyfaq/assets/src/configuration/update.js | 68 +++++++++++-------- .../Administration/UpdateController.php | 8 +-- .../Controller/Api/SetupController.php | 31 ++++----- scripts/version.sh | 2 +- 4 files changed, 56 insertions(+), 53 deletions(-) diff --git a/phpmyfaq/assets/src/configuration/update.js b/phpmyfaq/assets/src/configuration/update.js index 8e9a57aa1b..e70ce195ef 100644 --- a/phpmyfaq/assets/src/configuration/update.js +++ b/phpmyfaq/assets/src/configuration/update.js @@ -108,15 +108,21 @@ export const handleDatabaseUpdate = async () => { Accept: 'application/json, text/plain, */*', 'Content-Type': 'application/json', }, - body: installedVersion.value, + body: JSON.stringify({ version: installedVersion.value }), }); const progressBarInstallation = document.getElementById('result-update'); const reader = response.body.getReader(); + const decoder = new TextDecoder(); + let buffer = ''; + + const { done, value } = await reader.read(); + buffer += decoder.decode(value || new Uint8Array(), { stream: !done }); async function pump() { const { done, value } = await reader.read(); - const decodedValue = new TextDecoder().decode(value); + buffer += decoder.decode(value || new Uint8Array(), { stream: !done }); + if (done) { progressBarInstallation.style.width = '100%'; progressBarInstallation.innerText = '100%'; @@ -124,35 +130,39 @@ export const handleDatabaseUpdate = async () => { const alert = document.getElementById('phpmyfaq-update-database-success'); alert.classList.remove('d-none'); return; - } else { - let value; - try { - value = JSON.parse(decodedValue); - } catch (error) { - console.error('Failed to parse JSON:', error); - const alert = document.getElementById('phpmyfaq-update-database-error'); - const errorMessage = document.getElementById('error-messages'); - alert.classList.remove('d-none'); - errorMessage.innerText = `Error: ${error.message}\nFull Error: ${decodedValue}`; - return; - } - if (value.progress) { - progressBarInstallation.style.width = value.progress; - progressBarInstallation.innerText = value.progress; - } - if (value.error) { - progressBarInstallation.style.width = '100%'; - progressBarInstallation.innerText = '100%'; - progressBarInstallation.classList.remove('progress-bar-animated'); - const alert = document.getElementById('phpmyfaq-update-database-error'); - const errorMessage = document.getElementById('error-messages'); - alert.classList.remove('d-none'); - errorMessage.innerHTML = value.error; - return; - } } - await pump(); + let boundary; + while ((boundary = buffer.indexOf('\n')) !== -1) { + const chunk = buffer.slice(0, boundary).trim(); + buffer = buffer.slice(boundary + 1); + if (chunk) { + try { + const value = JSON.parse(chunk); + if (value.progress) { + progressBarInstallation.style.width = value.progress; + progressBarInstallation.innerText = value.progress; + } + if (value.error) { + progressBarInstallation.style.width = '100%'; + progressBarInstallation.innerText = '100%'; + progressBarInstallation.classList.remove('progress-bar-animated'); + const alert = document.getElementById('phpmyfaq-update-database-error'); + const errorMessage = document.getElementById('error-messages'); + alert.classList.remove('d-none'); + errorMessage.innerHTML = value.error; + return; + } + } catch (error) { + console.error('Failed to parse JSON:', error); + const alert = document.getElementById('phpmyfaq-update-database-error'); + const errorMessage = document.getElementById('error-messages'); + alert.classList.remove('d-none'); + errorMessage.innerText = `Error: ${error.message}\nFull Error: ${chunk}`; + return; + } + } + } } await pump(); diff --git a/phpmyfaq/src/phpMyFAQ/Controller/Administration/UpdateController.php b/phpmyfaq/src/phpMyFAQ/Controller/Administration/UpdateController.php index acbd5046d5..1cab48fad1 100644 --- a/phpmyfaq/src/phpMyFAQ/Controller/Administration/UpdateController.php +++ b/phpmyfaq/src/phpMyFAQ/Controller/Administration/UpdateController.php @@ -195,7 +195,7 @@ public function extractPackage(): StreamedResponse return new StreamedResponse(static function () use ($upgrade, $pathToPackage) { $progressCallback = static function ($progress) { - echo json_encode(['progress' => $progress]); + echo json_encode(['progress' => $progress]) . "\n"; ob_flush(); flush(); }; @@ -220,7 +220,7 @@ public function createTemporaryBackup(): StreamedResponse return new StreamedResponse(static function () use ($upgrade, $backupHash) { $progressCallback = static function ($progress) { - echo json_encode(['progress' => $progress]); + echo json_encode(['progress' => $progress]) . "\n"; ob_flush(); flush(); }; @@ -244,7 +244,7 @@ public function installPackage(): StreamedResponse $configurator = $this->container->get('phpmyfaq.setup.environment_configurator'); return new StreamedResponse(static function () use ($upgrade, $configurator) { $progressCallback = static function ($progress) { - echo json_encode(['progress' => $progress]); + echo json_encode(['progress' => $progress]) . "\n"; ob_flush(); flush(); }; @@ -271,7 +271,7 @@ public function updateDatabase(): StreamedResponse return new StreamedResponse(static function () use ($configuration, $update) { $progressCallback = static function ($progress) { - echo json_encode(['progress' => $progress]); + echo json_encode(['progress' => $progress]) . "\n"; ob_flush(); flush(); }; diff --git a/phpmyfaq/src/phpMyFAQ/Controller/Api/SetupController.php b/phpmyfaq/src/phpMyFAQ/Controller/Api/SetupController.php index 84407af164..3d8b85e36b 100644 --- a/phpmyfaq/src/phpMyFAQ/Controller/Api/SetupController.php +++ b/phpmyfaq/src/phpMyFAQ/Controller/Api/SetupController.php @@ -26,6 +26,7 @@ use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpFoundation\StreamedJsonResponse; use Symfony\Component\HttpFoundation\StreamedResponse; class SetupController extends AbstractController @@ -91,7 +92,7 @@ public function backup(Request $request): JsonResponse return $this->json(['message' => '✅ Backup successful', 'backupFile' => $pathToBackup], Response::HTTP_OK); } - public function updateDatabase(Request $request): StreamedResponse|JsonResponse + public function updateDatabase(Request $request): StreamedJsonResponse|JsonResponse { if (empty($request->getContent())) { return $this->json(['message' => 'No version given.'], Response::HTTP_BAD_REQUEST); @@ -99,32 +100,24 @@ public function updateDatabase(Request $request): StreamedResponse|JsonResponse $installedVersion = Filter::filterVar($request->getContent(), FILTER_SANITIZE_SPECIAL_CHARS); + $version = json_decode(html_entity_decode($installedVersion)); + $update = new Update(new System(), $this->configuration); - $update->setVersion($installedVersion); + $update->setVersion($version->version); - $response = new StreamedResponse(); $configuration = $this->configuration; - $response->setCallback(static function () use ($update, $configuration) { - $progressCallback = static function ($progress) { - echo json_encode(['progress' => $progress]); - ob_flush(); - flush(); + return new StreamedJsonResponse((function () use ($update, $configuration) { + $progressCallback = function ($progress) { + yield json_encode(['progress' => $progress]) . "\n"; }; try { if ($update->applyUpdates($progressCallback)) { - $configuration->set('main.maintenanceMode', false); - return new JsonResponse( - ['success' => '✅ Database successfully updated.'], - Response::HTTP_OK - ); + $configuration->set('main.maintenanceMode', true); + yield json_encode(['success' => '✅ Database successfully updated.']) . "\n"; } } catch (Exception $exception) { - return new JsonResponse( - ['error' => 'Update database failed: ' . $exception->getMessage()], - Response::HTTP_BAD_GATEWAY - ); + yield json_encode(['error' => 'Update database failed: ' . $exception->getMessage()]) . "\n"; } - }); - return $response; + })()); } } diff --git a/scripts/version.sh b/scripts/version.sh index 45c8048e1e..6cedd41286 100644 --- a/scripts/version.sh +++ b/scripts/version.sh @@ -1,5 +1,5 @@ #!/bin/sh # Check if PMF_VERSION is not set or empty if [ -z "${PMF_VERSION}" ]; then - PMF_VERSION="4.0.3" + PMF_VERSION="4.0.4" fi