use "newer" node search api directly in unified search
Signed-off-by: Robin Appelman <robin@icewind.nl>
This commit is contained in:
parent
75871f76ad
commit
60551ced9e
|
@ -28,10 +28,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;
|
||||
|
@ -42,9 +45,6 @@ use OCP\Search\SearchResultEntry;
|
|||
|
||||
class FilesSearchProvider implements IProvider {
|
||||
|
||||
/** @var File */
|
||||
private $fileSearch;
|
||||
|
||||
/** @var IL10N */
|
||||
private $l10n;
|
||||
|
||||
|
@ -57,13 +57,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;
|
||||
|
@ -98,26 +98,37 @@ 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(),
|
||||
]
|
||||
);
|
||||
|
||||
return 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())
|
||||
);
|
||||
}, $this->fileSearch->search($query->getTerm(), $query->getLimit(), (int)$query->getCursor())),
|
||||
}, $userFolder->search($fileQuery)),
|
||||
$query->getCursor() + $query->getLimit()
|
||||
);
|
||||
}
|
||||
|
@ -125,16 +136,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]);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue