1
0

FilesSearchProvider.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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@protonmail.com>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. *
  12. * @license GNU AGPL version 3 or any later version
  13. *
  14. * This program is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License as
  16. * published by the Free Software Foundation, either version 3 of the
  17. * License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  26. *
  27. */
  28. namespace OCA\Files\Search;
  29. use InvalidArgumentException;
  30. use OC\Files\Search\SearchBinaryOperator;
  31. use OC\Files\Search\SearchComparison;
  32. use OC\Files\Search\SearchOrder;
  33. use OC\Files\Search\SearchQuery;
  34. use OC\Search\Filter\GroupFilter;
  35. use OC\Search\Filter\UserFilter;
  36. use OCP\Files\FileInfo;
  37. use OCP\Files\IMimeTypeDetector;
  38. use OCP\Files\IRootFolder;
  39. use OCP\Files\Node;
  40. use OCP\Files\Search\ISearchComparison;
  41. use OCP\Files\Search\ISearchOperator;
  42. use OCP\Files\Search\ISearchOrder;
  43. use OCP\IL10N;
  44. use OCP\IPreview;
  45. use OCP\IURLGenerator;
  46. use OCP\IUser;
  47. use OCP\Search\FilterDefinition;
  48. use OCP\Search\IFilter;
  49. use OCP\Search\IFilteringProvider;
  50. use OCP\Search\ISearchQuery;
  51. use OCP\Search\SearchResult;
  52. use OCP\Search\SearchResultEntry;
  53. use OCP\Share\IShare;
  54. class FilesSearchProvider implements IFilteringProvider {
  55. /** @var IL10N */
  56. private $l10n;
  57. /** @var IURLGenerator */
  58. private $urlGenerator;
  59. /** @var IMimeTypeDetector */
  60. private $mimeTypeDetector;
  61. /** @var IRootFolder */
  62. private $rootFolder;
  63. public function __construct(
  64. IL10N $l10n,
  65. IURLGenerator $urlGenerator,
  66. IMimeTypeDetector $mimeTypeDetector,
  67. IRootFolder $rootFolder,
  68. private IPreview $previewManager,
  69. ) {
  70. $this->l10n = $l10n;
  71. $this->urlGenerator = $urlGenerator;
  72. $this->mimeTypeDetector = $mimeTypeDetector;
  73. $this->rootFolder = $rootFolder;
  74. }
  75. /**
  76. * @inheritDoc
  77. */
  78. public function getId(): string {
  79. return 'files';
  80. }
  81. /**
  82. * @inheritDoc
  83. */
  84. public function getName(): string {
  85. return $this->l10n->t('Files');
  86. }
  87. /**
  88. * @inheritDoc
  89. */
  90. public function getOrder(string $route, array $routeParameters): int {
  91. if ($route === 'files.View.index') {
  92. // Before comments
  93. return -5;
  94. }
  95. return 5;
  96. }
  97. public function getSupportedFilters(): array {
  98. return [
  99. 'term',
  100. 'since',
  101. 'until',
  102. 'person',
  103. 'min-size',
  104. 'max-size',
  105. 'mime',
  106. 'type',
  107. 'is-favorite',
  108. 'title-only',
  109. ];
  110. }
  111. public function getAlternateIds(): array {
  112. return [];
  113. }
  114. public function getCustomFilters(): array {
  115. return [
  116. new FilterDefinition('min-size', FilterDefinition::TYPE_INT),
  117. new FilterDefinition('max-size', FilterDefinition::TYPE_INT),
  118. new FilterDefinition('mime', FilterDefinition::TYPE_STRING),
  119. new FilterDefinition('type', FilterDefinition::TYPE_STRING),
  120. new FilterDefinition('is-favorite', FilterDefinition::TYPE_BOOL),
  121. ];
  122. }
  123. public function search(IUser $user, ISearchQuery $query): SearchResult {
  124. $userFolder = $this->rootFolder->getUserFolder($user->getUID());
  125. $fileQuery = $this->buildSearchQuery($query, $user);
  126. return SearchResult::paginated(
  127. $this->l10n->t('Files'),
  128. array_map(function (Node $result) use ($userFolder) {
  129. $thumbnailUrl = $this->previewManager->isMimeSupported($result->getMimetype())
  130. ? $this->urlGenerator->linkToRouteAbsolute('core.Preview.getPreviewByFileId', ['x' => 32, 'y' => 32, 'fileId' => $result->getId()])
  131. : '';
  132. $icon = $result->getMimetype() === FileInfo::MIMETYPE_FOLDER
  133. ? 'icon-folder'
  134. : $this->mimeTypeDetector->mimeTypeIcon($result->getMimetype());
  135. $path = $userFolder->getRelativePath($result->getPath());
  136. // Use shortened link to centralize the various
  137. // files/folder url redirection in files.View.showFile
  138. $link = $this->urlGenerator->linkToRoute(
  139. 'files.View.showFile',
  140. ['fileid' => $result->getId()]
  141. );
  142. $searchResultEntry = new SearchResultEntry(
  143. $thumbnailUrl,
  144. $result->getName(),
  145. $this->formatSubline($path),
  146. $this->urlGenerator->getAbsoluteURL($link),
  147. $icon,
  148. );
  149. $searchResultEntry->addAttribute('fileId', (string)$result->getId());
  150. $searchResultEntry->addAttribute('path', $path);
  151. return $searchResultEntry;
  152. }, $userFolder->search($fileQuery)),
  153. $query->getCursor() + $query->getLimit()
  154. );
  155. }
  156. private function buildSearchQuery(ISearchQuery $query, IUser $user): SearchQuery {
  157. $comparisons = [];
  158. foreach ($query->getFilters() as $name => $filter) {
  159. $comparisons[] = match ($name) {
  160. 'term' => new SearchComparison(ISearchComparison::COMPARE_LIKE, 'name', '%' . $filter->get() . '%'),
  161. 'since' => new SearchComparison(ISearchComparison::COMPARE_GREATER_THAN_EQUAL, 'mtime', $filter->get()->getTimestamp()),
  162. 'until' => new SearchComparison(ISearchComparison::COMPARE_LESS_THAN_EQUAL, 'mtime', $filter->get()->getTimestamp()),
  163. 'min-size' => new SearchComparison(ISearchComparison::COMPARE_GREATER_THAN_EQUAL, 'size', $filter->get()),
  164. 'max-size' => new SearchComparison(ISearchComparison::COMPARE_LESS_THAN_EQUAL, 'size', $filter->get()),
  165. 'mime' => new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'mimetype', $filter->get()),
  166. 'type' => new SearchComparison(ISearchComparison::COMPARE_LIKE, 'mimetype', $filter->get() . '/%'),
  167. 'person' => $this->buildPersonSearchQuery($filter),
  168. default => throw new InvalidArgumentException('Unsupported comparison'),
  169. };
  170. }
  171. return new SearchQuery(
  172. new SearchBinaryOperator(SearchBinaryOperator::OPERATOR_AND, $comparisons),
  173. $query->getLimit(),
  174. (int) $query->getCursor(),
  175. $query->getSortOrder() === ISearchQuery::SORT_DATE_DESC
  176. ? [new SearchOrder(ISearchOrder::DIRECTION_DESCENDING, 'mtime')]
  177. : [],
  178. $user
  179. );
  180. }
  181. private function buildPersonSearchQuery(IFilter $person): ISearchOperator {
  182. if ($person instanceof UserFilter) {
  183. return new SearchBinaryOperator(SearchBinaryOperator::OPERATOR_OR, [
  184. new SearchBinaryOperator(SearchBinaryOperator::OPERATOR_AND, [
  185. new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'share_with', $person->get()->getUID()),
  186. new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'share_type', IShare::TYPE_USER),
  187. ]),
  188. new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'owner', $person->get()->getUID()),
  189. ]);
  190. }
  191. if ($person instanceof GroupFilter) {
  192. return new SearchBinaryOperator(SearchBinaryOperator::OPERATOR_AND, [
  193. new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'share_with', $person->get()->getGID()),
  194. new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'share_type', IShare::TYPE_GROUP),
  195. ]);
  196. }
  197. throw new InvalidArgumentException('Unsupported filter type');
  198. }
  199. /**
  200. * Format subline for files
  201. *
  202. * @param string $path
  203. * @return string
  204. */
  205. private function formatSubline(string $path): string {
  206. // Do not show the location if the file is in root
  207. if (strrpos($path, '/') > 0) {
  208. $path = ltrim(dirname($path), '/');
  209. return $this->l10n->t('in %s', [$path]);
  210. } else {
  211. return '';
  212. }
  213. }
  214. }