FilesSearchProvider.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OCA\Files\Search;
  27. use OC\Search\Provider\File;
  28. use OC\Search\Result\File as FileResult;
  29. use OCP\Files\IMimeTypeDetector;
  30. use OCP\Files\IRootFolder;
  31. use OCP\IL10N;
  32. use OCP\IURLGenerator;
  33. use OCP\IUser;
  34. use OCP\Search\IProvider;
  35. use OCP\Search\ISearchQuery;
  36. use OCP\Search\SearchResult;
  37. use OCP\Search\SearchResultEntry;
  38. class FilesSearchProvider implements IProvider {
  39. /** @var File */
  40. private $fileSearch;
  41. /** @var IL10N */
  42. private $l10n;
  43. /** @var IURLGenerator */
  44. private $urlGenerator;
  45. /** @var IMimeTypeDetector */
  46. private $mimeTypeDetector;
  47. /** @var IRootFolder */
  48. private $rootFolder;
  49. public function __construct(File $fileSearch,
  50. IL10N $l10n,
  51. IURLGenerator $urlGenerator,
  52. IMimeTypeDetector $mimeTypeDetector,
  53. IRootFolder $rootFolder) {
  54. $this->l10n = $l10n;
  55. $this->fileSearch = $fileSearch;
  56. $this->urlGenerator = $urlGenerator;
  57. $this->mimeTypeDetector = $mimeTypeDetector;
  58. $this->rootFolder = $rootFolder;
  59. }
  60. /**
  61. * @inheritDoc
  62. */
  63. public function getId(): string {
  64. return 'files';
  65. }
  66. /**
  67. * @inheritDoc
  68. */
  69. public function getName(): string {
  70. return $this->l10n->t('Files');
  71. }
  72. /**
  73. * @inheritDoc
  74. */
  75. public function getOrder(string $route, array $routeParameters): int {
  76. if ($route === 'files.View.index') {
  77. // Before comments
  78. return -5;
  79. }
  80. return 5;
  81. }
  82. /**
  83. * @inheritDoc
  84. */
  85. public function search(IUser $user, ISearchQuery $query): SearchResult {
  86. // Make sure we setup the users filesystem
  87. $this->rootFolder->getUserFolder($user->getUID());
  88. return SearchResult::complete(
  89. $this->l10n->t('Files'),
  90. array_map(function (FileResult $result) {
  91. // Generate thumbnail url
  92. $thumbnailUrl = $result->has_preview
  93. ? $this->urlGenerator->linkToRouteAbsolute('core.Preview.getPreviewByFileId', ['x' => 32, 'y' => 32, 'fileId' => $result->id])
  94. : '';
  95. return new SearchResultEntry(
  96. $thumbnailUrl,
  97. $result->name,
  98. $this->formatSubline($result),
  99. $this->urlGenerator->getAbsoluteURL($result->link),
  100. $result->type === 'folder' ? 'icon-folder' : $this->mimeTypeDetector->mimeTypeIcon($result->mime_type)
  101. );
  102. }, $this->fileSearch->search($query->getTerm()))
  103. );
  104. }
  105. /**
  106. * Format subline for files
  107. *
  108. * @param FileResult $result
  109. * @return string
  110. */
  111. private function formatSubline($result): string {
  112. // Do not show the location if the file is in root
  113. if ($result->path === '/' . $result->name) {
  114. return '';
  115. }
  116. $path = ltrim(dirname($result->path), '/');
  117. return $this->l10n->t('in %s', [$path]);
  118. }
  119. }