use "newer" node search api directly in unified search

Signed-off-by: Robin Appelman <robin@icewind.nl>
This commit is contained in:
Robin Appelman 2021-03-18 16:22:29 +01:00 committed by backportbot[bot]
parent c60f0e06d0
commit 680e21b7c5
1 changed files with 42 additions and 31 deletions

View File

@ -29,10 +29,13 @@ declare(strict_types=1);
namespace OCA\Files\Search; namespace OCA\Files\Search;
use OC\Search\Provider\File; use OC\Files\Search\SearchComparison;
use OC\Search\Result\File as FileResult; use OC\Files\Search\SearchQuery;
use OCP\Files\FileInfo;
use OCP\Files\IMimeTypeDetector; use OCP\Files\IMimeTypeDetector;
use OCP\Files\IRootFolder; use OCP\Files\IRootFolder;
use OCP\Files\Search\ISearchComparison;
use OCP\Files\Node;
use OCP\IL10N; use OCP\IL10N;
use OCP\IURLGenerator; use OCP\IURLGenerator;
use OCP\IUser; use OCP\IUser;
@ -43,9 +46,6 @@ use OCP\Search\SearchResultEntry;
class FilesSearchProvider implements IProvider { class FilesSearchProvider implements IProvider {
/** @var File */
private $fileSearch;
/** @var IL10N */ /** @var IL10N */
private $l10n; private $l10n;
@ -58,13 +58,13 @@ class FilesSearchProvider implements IProvider {
/** @var IRootFolder */ /** @var IRootFolder */
private $rootFolder; private $rootFolder;
public function __construct(File $fileSearch, public function __construct(
IL10N $l10n, IL10N $l10n,
IURLGenerator $urlGenerator, IURLGenerator $urlGenerator,
IMimeTypeDetector $mimeTypeDetector, IMimeTypeDetector $mimeTypeDetector,
IRootFolder $rootFolder) { IRootFolder $rootFolder
) {
$this->l10n = $l10n; $this->l10n = $l10n;
$this->fileSearch = $fileSearch;
$this->urlGenerator = $urlGenerator; $this->urlGenerator = $urlGenerator;
$this->mimeTypeDetector = $mimeTypeDetector; $this->mimeTypeDetector = $mimeTypeDetector;
$this->rootFolder = $rootFolder; $this->rootFolder = $rootFolder;
@ -99,29 +99,40 @@ class FilesSearchProvider implements IProvider {
* @inheritDoc * @inheritDoc
*/ */
public function search(IUser $user, ISearchQuery $query): SearchResult { public function search(IUser $user, ISearchQuery $query): SearchResult {
$userFolder = $this->rootFolder->getUserFolder($user->getUID());
// Make sure we setup the users filesystem $fileQuery = new SearchQuery(
$this->rootFolder->getUserFolder($user->getUID()); new SearchComparison(ISearchComparison::COMPARE_LIKE, 'name', '%' . $query->getTerm() . '%'),
$query->getLimit(),
(int)$query->getCursor(),
[],
$user
);
return SearchResult::paginated( return SearchResult::paginated(
$this->l10n->t('Files'), $this->l10n->t('Files'),
array_map(function (FileResult $result) { array_map(function (Node $result) use ($userFolder) {
// Generate thumbnail url // Generate thumbnail url
$thumbnailUrl = $result->has_preview $thumbnailUrl = $this->urlGenerator->linkToRouteAbsolute('core.Preview.getPreviewByFileId', ['x' => 32, 'y' => 32, 'fileId' => $result->getId()]);
? $this->urlGenerator->linkToRouteAbsolute('core.Preview.getPreviewByFileId', ['x' => 32, 'y' => 32, 'fileId' => $result->id]) $path = $userFolder->getRelativePath($result->getPath());
: ''; $link = $this->urlGenerator->linkToRoute(
'files.view.index',
[
'dir' => dirname($path),
'scrollto' => $result->getName(),
]
);
$searchResultEntry = new SearchResultEntry( $searchResultEntry = new SearchResultEntry(
$thumbnailUrl, $thumbnailUrl,
$result->name, $result->getName(),
$this->formatSubline($result), $this->formatSubline($path),
$this->urlGenerator->getAbsoluteURL($result->link), $this->urlGenerator->getAbsoluteURL($link),
$result->type === 'folder' ? 'icon-folder' : $this->mimeTypeDetector->mimeTypeIcon($result->mime_type) $result->getMimetype() === FileInfo::MIMETYPE_FOLDER ? 'icon-folder' : $this->mimeTypeDetector->mimeTypeIcon($result->getMimetype())
); );
$searchResultEntry->addAttribute('fileId', (string)$result->id); $searchResultEntry->addAttribute('fileId', (string)$result->getId());
$searchResultEntry->addAttribute('path', $result->path); $searchResultEntry->addAttribute('path', $path);
return $searchResultEntry; return $searchResultEntry;
}, $this->fileSearch->search($query->getTerm(), $query->getLimit(), (int)$query->getCursor())), }, $userFolder->search($fileQuery)),
$query->getCursor() + $query->getLimit() $query->getCursor() + $query->getLimit()
); );
} }
@ -129,16 +140,16 @@ class FilesSearchProvider implements IProvider {
/** /**
* Format subline for files * Format subline for files
* *
* @param FileResult $result * @param string $path
* @return string * @return string
*/ */
private function formatSubline($result): string { private function formatSubline(string $path): string {
// Do not show the location if the file is in root // Do not show the location if the file is in root
if ($result->path === '/' . $result->name) { if (strrpos($path, '/') > 0) {
$path = ltrim(dirname($path), '/');
return $this->l10n->t('in %s', [$path]);
} else {
return ''; return '';
} }
$path = ltrim(dirname($result->path), '/');
return $this->l10n->t('in %s', [$path]);
} }
} }