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 1b5db252c5
commit 14a1b5063e
No known key found for this signature in database
GPG Key ID: 42B69D8A64526EFB
1 changed files with 42 additions and 31 deletions

View File

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