SearchController.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Core\Controller;
  8. use OCP\AppFramework\Controller;
  9. use OCP\AppFramework\Http\Attribute\FrontpageRoute;
  10. use OCP\AppFramework\Http\Attribute\NoAdminRequired;
  11. use OCP\AppFramework\Http\JSONResponse;
  12. use OCP\IRequest;
  13. use OCP\ISearch;
  14. use OCP\Search\Result;
  15. use Psr\Log\LoggerInterface;
  16. class SearchController extends Controller {
  17. public function __construct(
  18. string $appName,
  19. IRequest $request,
  20. private ISearch $searcher,
  21. private LoggerInterface $logger,
  22. ) {
  23. parent::__construct($appName, $request);
  24. }
  25. #[NoAdminRequired]
  26. #[FrontpageRoute(verb: 'GET', url: '/core/search')]
  27. public function search(string $query, array $inApps = [], int $page = 1, int $size = 30): JSONResponse {
  28. $results = $this->searcher->searchPaged($query, $inApps, $page, $size);
  29. $results = array_filter($results, function (Result $result) {
  30. if (json_encode($result, JSON_HEX_TAG) === false) {
  31. $this->logger->warning("Skipping search result due to invalid encoding: {type: " . $result->type . ", id: " . $result->id . "}");
  32. return false;
  33. } else {
  34. return true;
  35. }
  36. });
  37. return new JSONResponse($results);
  38. }
  39. }