Skip to content

Commit

Permalink
Merge branch 'refs/heads/1.9' into 1.x
Browse files Browse the repository at this point in the history
  • Loading branch information
fashxp committed Aug 27, 2024
2 parents 651bfff + a9d3d48 commit aa4b735
Showing 1 changed file with 23 additions and 2 deletions.
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

0 comments on commit aa4b735

Please sign in to comment.