Skip to content

Commit

Permalink
fix: changed from StreamResponse to StreamedJsonResponse (#3312)
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Jan 4, 2025
1 parent a8d698f commit c79c3d1
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 53 deletions.
68 changes: 39 additions & 29 deletions phpmyfaq/assets/src/configuration/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,51 +108,61 @@ 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%';
progressBarInstallation.classList.remove('progress-bar-animated');
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};
Expand All @@ -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();
};
Expand All @@ -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();
};
Expand All @@ -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();
};
Expand Down
31 changes: 12 additions & 19 deletions phpmyfaq/src/phpMyFAQ/Controller/Api/SetupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -91,40 +92,32 @@ 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);
}

$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;
})());
}
}
2 changes: 1 addition & 1 deletion scripts/version.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit c79c3d1

Please sign in to comment.