1
0

FilesSearchProvider.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. 'path',
  108. 'is-favorite',
  109. 'title-only',
  110. ];
  111. }
  112. public function getAlternateIds(): array {
  113. return [];
  114. }
  115. public function getCustomFilters(): array {
  116. return [
  117. new FilterDefinition('min-size', FilterDefinition::TYPE_INT),
  118. new FilterDefinition('max-size', FilterDefinition::TYPE_INT),
  119. new FilterDefinition('mime', FilterDefinition::TYPE_STRING),
  120. new FilterDefinition('type', FilterDefinition::TYPE_STRING),
  121. new FilterDefinition('path', FilterDefinition::TYPE_STRING),
  122. new FilterDefinition('is-favorite', FilterDefinition::TYPE_BOOL),
  123. ];
  124. }
  125. public function search(IUser $user, ISearchQuery $query): SearchResult {
  126. $userFolder = $this->rootFolder->getUserFolder($user->getUID());
  127. $fileQuery = $this->buildSearchQuery($query, $user);
  128. return SearchResult::paginated(
  129. $this->l10n->t('Files'),
  130. array_map(function (Node $result) use ($userFolder) {
  131. $thumbnailUrl = $this->previewManager->isMimeSupported($result->getMimetype())
  132. ? $this->urlGenerator->linkToRouteAbsolute('core.Preview.getPreviewByFileId', ['x' => 32, 'y' => 32, 'fileId' => $result->getId()])
  133. : '';
  134. $icon = $result->getMimetype() === FileInfo::MIMETYPE_FOLDER
  135. ? 'icon-folder'
  136. : $this->mimeTypeDetector->mimeTypeIcon($result->getMimetype());
  137. $path = $userFolder->getRelativePath($result->getPath());
  138. // Use shortened link to centralize the various
  139. // files/folder url redirection in files.View.showFile
  140. $link = $this->urlGenerator->linkToRoute(
  141. 'files.View.showFile',
  142. ['fileid' => $result->getId()]
  143. );
  144. $searchResultEntry = new SearchResultEntry(
  145. $thumbnailUrl,
  146. $result->getName(),
  147. $this->formatSubline($path),
  148. $this->urlGenerator->getAbsoluteURL($link),
  149. $icon,
  150. );
  151. $searchResultEntry->addAttribute('fileId', (string)$result->getId());
  152. $searchResultEntry->addAttribute('path', $path);
  153. return $searchResultEntry;
  154. }, $userFolder->search($fileQuery)),
  155. $query->getCursor() + $query->getLimit()
  156. );
  157. }
  158. private function buildSearchQuery(ISearchQuery $query, IUser $user): SearchQuery {
  159. $comparisons = [];
  160. foreach ($query->getFilters() as $name => $filter) {
  161. $comparisons[] = match ($name) {
  162. 'term' => new SearchComparison(ISearchComparison::COMPARE_LIKE, 'name', '%' . $filter->get() . '%'),
  163. 'since' => new SearchComparison(ISearchComparison::COMPARE_GREATER_THAN_EQUAL, 'mtime', $filter->get()->getTimestamp()),
  164. 'until' => new SearchComparison(ISearchComparison::COMPARE_LESS_THAN_EQUAL, 'mtime', $filter->get()->getTimestamp()),
  165. 'min-size' => new SearchComparison(ISearchComparison::COMPARE_GREATER_THAN_EQUAL, 'size', $filter->get()),
  166. 'max-size' => new SearchComparison(ISearchComparison::COMPARE_LESS_THAN_EQUAL, 'size', $filter->get()),
  167. 'mime' => new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'mimetype', $filter->get()),
  168. 'type' => new SearchComparison(ISearchComparison::COMPARE_LIKE, 'mimetype', $filter->get() . '/%'),
  169. 'path' => new SearchComparison(ISearchComparison::COMPARE_LIKE, 'path', 'files/' . ltrim($filter->get(), '/') . '%'),
  170. 'person' => $this->buildPersonSearchQuery($filter),
  171. default => throw new InvalidArgumentException('Unsupported comparison'),
  172. };
  173. }
  174. return new SearchQuery(
  175. new SearchBinaryOperator(SearchBinaryOperator::OPERATOR_AND, $comparisons),
  176. $query->getLimit(),
  177. (int) $query->getCursor(),
  178. $query->getSortOrder() === ISearchQuery::SORT_DATE_DESC
  179. ? [new SearchOrder(ISearchOrder::DIRECTION_DESCENDING, 'mtime')]
  180. : [],
  181. $user
  182. );
  183. }
  184. private function buildPersonSearchQuery(IFilter $person): ISearchOperator {
  185. if ($person instanceof UserFilter) {
  186. return new SearchBinaryOperator(SearchBinaryOperator::OPERATOR_OR, [
  187. new SearchBinaryOperator(SearchBinaryOperator::OPERATOR_AND, [
  188. new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'share_with', $person->get()->getUID()),
  189. new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'share_type', IShare::TYPE_USER),
  190. ]),
  191. new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'owner', $person->get()->getUID()),
  192. ]);
  193. }
  194. if ($person instanceof GroupFilter) {
  195. return new SearchBinaryOperator(SearchBinaryOperator::OPERATOR_AND, [
  196. new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'share_with', $person->get()->getGID()),
  197. new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'share_type', IShare::TYPE_GROUP),
  198. ]);
  199. }
  200. throw new InvalidArgumentException('Unsupported filter type');
  201. }
  202. /**
  203. * Format subline for files
  204. *
  205. * @param string $path
  206. * @return string
  207. */
  208. private function formatSubline(string $path): string {
  209. // Do not show the location if the file is in root
  210. if (strrpos($path, '/') > 0) {
  211. $path = ltrim(dirname($path), '/');
  212. return $this->l10n->t('in %s', [$path]);
  213. } else {
  214. return '';
  215. }
  216. }
  217. }