Skip to content

Commit

Permalink
feat(files): Allow advanced search for files
Browse files Browse the repository at this point in the history
Signed-off-by: Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
  • Loading branch information
Altahrim committed Sep 25, 2023
1 parent 662d4f7 commit 9852a99
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions apps/files/lib/Search/FilesSearchProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
*/
namespace OCA\Files\Search;

use OC\Files\Search\SearchBinaryOperator;
use OC\Files\Search\SearchComparison;
use OC\Files\Search\SearchOrder;
use OC\Files\Search\SearchQuery;
Expand All @@ -47,7 +48,6 @@
use OCP\Search\SearchResultEntry;

class FilesSearchProvider implements IProvider {

/** @var IL10N */
private $l10n;

Expand Down Expand Up @@ -102,16 +102,7 @@ public function getOrder(string $route, array $routeParameters): int {
*/
public function search(IUser $user, ISearchQuery $query): SearchResult {
$userFolder = $this->rootFolder->getUserFolder($user->getUID());
$fileQuery = new SearchQuery(
new SearchComparison(ISearchComparison::COMPARE_LIKE, 'name', '%' . $query->getTerm() . '%'),
$query->getLimit(),
(int)$query->getCursor(),
$query->getSortOrder() === ISearchQuery::SORT_DATE_DESC ? [
new SearchOrder(ISearchOrder::DIRECTION_DESCENDING, 'mtime'),
] : [],
$user
);

$fileQuery = $this->buildSearchQuery($query, $user);
return SearchResult::paginated(
$this->l10n->t('Files'),
array_map(function (Node $result) use ($userFolder) {
Expand Down Expand Up @@ -141,6 +132,35 @@ public function search(IUser $user, ISearchQuery $query): SearchResult {
);
}

private function buildSearchQuery(ISearchQuery $query, IUser $user): SearchQuery {
$comparisons = [];
foreach ($query->getFilters() as $filter => $value) {
$comparison = match ($filter) {
'term' => new SearchComparison(ISearchComparison::COMPARE_LIKE, 'name', '%' . $value . '%'),
'from' => new SearchComparison(ISearchComparison::COMPARE_GREATER_THAN_EQUAL, 'mtime', $value),
'to' => new SearchComparison(ISearchComparison::COMPARE_LESS_THAN_EQUAL, 'mtime', $value),
'min-size' => new SearchComparison(ISearchComparison::COMPARE_GREATER_THAN_EQUAL, 'size', (int) $value),
'max-size' => new SearchComparison(ISearchComparison::COMPARE_LESS_THAN_EQUAL, 'size', (int) $value),
'mime' => new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'mimetype', $value),
'type' => new SearchComparison(ISearchComparison::COMPARE_LIKE, 'mimetype', $value . '/%'),
default => false,
};
if ($comparison) {
$comparisons[] = $comparison;
}
}

return new SearchQuery(
new SearchBinaryOperator(SearchBinaryOperator::OPERATOR_AND, $comparisons),
$query->getLimit(),
(int) $query->getCursor(),
$query->getSortOrder() === ISearchQuery::SORT_DATE_DESC
? [new SearchOrder(ISearchOrder::DIRECTION_DESCENDING, 'mtime')]
: [],
$user
);
}

/**
* Format subline for files
*
Expand Down

0 comments on commit 9852a99

Please sign in to comment.