Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX] Fixed import, when users temp preview file is corrupt #414

Merged
merged 3 commits into from
Jul 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions src/Controller/ConfigDataObjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,14 @@ protected function loadAvailableColumnHeaders(
try {
$interpreter = $interpreterFactory->loadInterpreter($configName, $config['interpreterConfig'], $config['processingConfig']);
$dataPreview = $interpreter->previewData($previewFilePath);
$columnHeaders = $dataPreview->getDataColumnHeaders();

return $dataPreview->getDataColumnHeaders();
// Validate if the column headers are valid JSON. Otherwise take care of the preview file to be deleted.
if (!$this->isValidJson($columnHeaders)) {
throw new \Exception('Invalid column headers.');
}

return $columnHeaders;
} catch (Exception $e) {
Logger::warning($e);
}
Expand All @@ -132,6 +138,13 @@ protected function loadAvailableColumnHeaders(
return [];
}

protected function isValidJson(array $array): bool
{
json_encode($array);

return json_last_error() === \JSON_ERROR_NONE;
}

/**
* @Route("/get")
*
Expand Down Expand Up @@ -287,6 +300,7 @@ public function loadDataPreviewAction(
$hasData = false;
$errorMessage = '';
$previewFilePath = $this->previewService->getLocalPreviewFile($configName, $this->getPimcoreUser());
$dataPreviewData = [];
if (is_file($previewFilePath)) {
$config = $configurationPreparationService->prepareConfiguration($configName, $currentConfig);

Expand All @@ -304,6 +318,13 @@ public function loadDataPreviewAction(
if ($interpreter->fileValid($previewFilePath)) {
$dataPreview = $interpreter->previewData($previewFilePath, $recordNumber, $mappedColumns);
$hasData = true;

$preview = $dataPreview->getDataPreview();
if (!$this->isValidJson($preview)) {
unlink($previewFilePath);
throw new \Exception('Invalid data preview. Deleted preview data.');
}
$dataPreviewData = $preview;
} else {
$errorMessage = $translator->trans('plugin_pimcore_datahub_data_importer_configpanel_preview_error_invalid_file', [], 'admin');
}
Expand All @@ -314,7 +335,7 @@ public function loadDataPreviewAction(
}

return new JsonResponse([
'dataPreview' => $dataPreview ? $dataPreview->getDataPreview() : [],
'dataPreview' => $dataPreviewData,
'previewRecordIndex' => $dataPreview ? $dataPreview->getRecordNumber() : 0,
'hasData' => $hasData,
'errorMessage' => $errorMessage
Expand Down
Loading