From 9c046efd2dbdab5e1591c3d9b3c2a412d7d14b00 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Fri, 19 Mar 2021 14:47:30 +0100 Subject: [PATCH] use node search api for legacy file search endpoint Signed-off-by: Robin Appelman --- lib/private/Files/Node/Folder.php | 1 - lib/private/Search/Provider/File.php | 48 +++++++++++++++++----------- lib/private/Search/Result/File.php | 7 ++-- 3 files changed, 32 insertions(+), 24 deletions(-) diff --git a/lib/private/Files/Node/Folder.php b/lib/private/Files/Node/Folder.php index 74a33259d2..88ea0b8aa9 100644 --- a/lib/private/Files/Node/Folder.php +++ b/lib/private/Files/Node/Folder.php @@ -34,7 +34,6 @@ use OC\DB\QueryBuilder\Literal; use OC\Files\Search\SearchBinaryOperator; use OC\Files\Search\SearchComparison; use OC\Files\Search\SearchQuery; -use OC\Files\Storage\Wrapper\Jail; use OC\Files\Storage\Storage; use OCA\Files_Sharing\SharedStorage; use OCP\DB\QueryBuilder\IQueryBuilder; diff --git a/lib/private/Search/Provider/File.php b/lib/private/Search/Provider/File.php index 253a4013ec..6b4f100826 100644 --- a/lib/private/Search/Provider/File.php +++ b/lib/private/Search/Provider/File.php @@ -28,7 +28,14 @@ namespace OC\Search\Provider; -use OC\Files\Filesystem; +use OC\Files\Search\SearchComparison; +use OC\Files\Search\SearchOrder; +use OC\Files\Search\SearchQuery; +use OCP\Files\FileInfo; +use OCP\Files\IRootFolder; +use OCP\Files\Search\ISearchComparison; +use OCP\Files\Search\ISearchOrder; +use OCP\IUserSession; use OCP\Search\PagedProvider; /** @@ -45,35 +52,38 @@ class File extends PagedProvider { * @return \OCP\Search\Result[] */ public function search($query, int $limit = null, int $offset = null) { - if ($offset === null) { - $offset = 0; + /** @var IRootFolder $rootFolder */ + $rootFolder = \OC::$server->query(IRootFolder::class); + /** @var IUserSession $userSession */ + $userSession = \OC::$server->query(IUserSession::class); + $user = $userSession->getUser(); + if (!$user) { + return []; } - \OC_Util::setupFS(); - $files = Filesystem::search($query); + $userFolder = $rootFolder->getUserFolder($user->getUID()); + $fileQuery = new SearchQuery( + new SearchComparison(ISearchComparison::COMPARE_LIKE, 'name', '%' . $query . '%'), + (int)$limit, + (int)$offset, + [ + new SearchOrder(ISearchOrder::DIRECTION_DESCENDING, 'mtime'), + ], + $user + ); + $files = $userFolder->search($fileQuery); $results = []; - if ($limit !== null) { - $files = array_slice($files, $offset, $offset + $limit); - } // edit results foreach ($files as $fileData) { - // skip versions - if (strpos($fileData['path'], '_versions') === 0) { - continue; - } - // skip top-level folder - if ($fileData['name'] === 'files' && $fileData['parent'] === -1) { - continue; - } // create audio result - if ($fileData['mimepart'] === 'audio') { + if ($fileData->getMimePart() === 'audio') { $result = new \OC\Search\Result\Audio($fileData); } // create image result - elseif ($fileData['mimepart'] === 'image') { + elseif ($fileData->getMimePart() === 'image') { $result = new \OC\Search\Result\Image($fileData); } // create folder result - elseif ($fileData['mimetype'] === 'httpd/unix-directory') { + elseif ($fileData->getMimetype() === FileInfo::MIMETYPE_FOLDER) { $result = new \OC\Search\Result\Folder($fileData); } // or create file result diff --git a/lib/private/Search/Result/File.php b/lib/private/Search/Result/File.php index cfff54e069..e1dda3860b 100644 --- a/lib/private/Search/Result/File.php +++ b/lib/private/Search/Result/File.php @@ -78,14 +78,13 @@ class File extends \OCP\Search\Result { public function __construct(FileInfo $data) { $path = $this->getRelativePath($data->getPath()); - $info = pathinfo($path); $this->id = $data->getId(); - $this->name = $info['basename']; + $this->name = $data->getName(); $this->link = \OC::$server->getURLGenerator()->linkToRoute( 'files.view.index', [ - 'dir' => $info['dirname'], - 'scrollto' => $info['basename'], + 'dir' => dirname($path), + 'scrollto' => $data->getName(), ] ); $this->permissions = $data->getPermissions();