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
parent 75871f76ad
commit 60551ced9e
No known key found for this signature in database
GPG Key ID: 42B69D8A64526EFB
1 changed files with 40 additions and 29 deletions

View File

@ -28,10 +28,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;
@ -42,9 +45,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;
@ -57,13 +57,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;
@ -98,26 +98,37 @@ 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(),
]
);
return new SearchResultEntry( return 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())
); );
}, $this->fileSearch->search($query->getTerm(), $query->getLimit(), (int)$query->getCursor())), }, $userFolder->search($fileQuery)),
$query->getCursor() + $query->getLimit() $query->getCursor() + $query->getLimit()
); );
} }
@ -125,16 +136,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]);
} }
} }