Browse only a JSON source supplied by the authenticated user, never a shared report. */
(int $type, string $options = '{}')
| 166 | |
| 167 | /** Browse only a JSON source supplied by the authenticated user, never a shared report. */ |
| 168 | #[NoAdminRequired] |
| 169 | public function jsonContent(int $type, string $options = '{}'): DataResponse { |
| 170 | $user = $this->userSession->getUser(); |
| 171 | $settings = json_decode($options, true); |
| 172 | if ($user === null || !is_array($settings) |
| 173 | || !in_array($type, [self::DATASET_TYPE_LOCAL_JSON, self::DATASET_TYPE_EXTERNAL_JSON], true) |
| 174 | || !isset($this->getDatasources()[$type])) { |
| 175 | return new DataResponse(['error' => $this->l10n->t('Data source not available anymore')], 400); |
| 176 | } |
| 177 | // Browsing must work before an extraction path has been chosen. |
| 178 | $settings['path'] = ''; |
| 179 | $settings['timestamp'] = 'false'; |
| 180 | try { |
| 181 | $result = $this->read($type, [ |
| 182 | 'link' => json_encode($settings), 'user_id' => $user->getUID(), 'filteroptions' => '', |
| 183 | ], false); |
| 184 | if (!empty($result['error']) || !is_string($result['rawdata'] ?? null)) { |
| 185 | return new DataResponse(['error' => $this->l10n->t('Could not load JSON data. Check the datasource settings.')], 400); |
| 186 | } |
| 187 | if (strlen($result['rawdata']) > 2 * 1024 * 1024) { |
| 188 | return new DataResponse(['error' => $this->l10n->t('This JSON is too large to browse. Enter the Object path manually.')], 400); |
| 189 | } |
| 190 | // Do not return request headers, authentication, or other datasource metadata. |
| 191 | return new DataResponse(['content' => $result['rawdata']]); |
| 192 | } catch (\Throwable $e) { |
| 193 | $this->logger->debug('Could not browse JSON data', ['exception' => $e]); |
| 194 | return new DataResponse(['error' => $this->l10n->t('Could not load JSON data. Check the datasource settings.')], 400); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Get the data from a data source; |