use node search api for legacy file search endpoint

Signed-off-by: Robin Appelman <robin@icewind.nl>
This commit is contained in:
Robin Appelman 2021-03-19 14:47:30 +01:00
parent 10bf93aa24
commit 4ede9dbb07
No known key found for this signature in database
GPG Key ID: 42B69D8A64526EFB
3 changed files with 33 additions and 23 deletions

View File

@ -55,6 +55,7 @@ class SearchController extends Controller {
/** /**
* @NoAdminRequired * @NoAdminRequired
* @NoCSRFRequired
*/ */
public function search(string $query, array $inApps = [], int $page = 1, int $size = 30): JSONResponse { public function search(string $query, array $inApps = [], int $page = 1, int $size = 30): JSONResponse {
$results = $this->searcher->searchPaged($query, $inApps, $page, $size); $results = $this->searcher->searchPaged($query, $inApps, $page, $size);

View File

@ -29,7 +29,14 @@
namespace OC\Search\Provider; 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; use OCP\Search\PagedProvider;
/** /**
@ -48,35 +55,38 @@ class File extends PagedProvider {
* @deprecated 20.0.0 * @deprecated 20.0.0
*/ */
public function search($query, int $limit = null, int $offset = null) { public function search($query, int $limit = null, int $offset = null) {
if ($offset === null) { /** @var IRootFolder $rootFolder */
$offset = 0; $rootFolder = \OC::$server->query(IRootFolder::class);
/** @var IUserSession $userSession */
$userSession = \OC::$server->query(IUserSession::class);
$user = $userSession->getUser();
if (!$user) {
return [];
} }
\OC_Util::setupFS(); $userFolder = $rootFolder->getUserFolder($user->getUID());
$files = Filesystem::search($query); $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 = []; $results = [];
if ($limit !== null) {
$files = array_slice($files, $offset, $offset + $limit);
}
// edit results // edit results
foreach ($files as $fileData) { 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 // create audio result
if ($fileData['mimepart'] === 'audio') { if ($fileData->getMimePart() === 'audio') {
$result = new \OC\Search\Result\Audio($fileData); $result = new \OC\Search\Result\Audio($fileData);
} }
// create image result // create image result
elseif ($fileData['mimepart'] === 'image') { elseif ($fileData->getMimePart() === 'image') {
$result = new \OC\Search\Result\Image($fileData); $result = new \OC\Search\Result\Image($fileData);
} }
// create folder result // create folder result
elseif ($fileData['mimetype'] === 'httpd/unix-directory') { elseif ($fileData->getMimetype() === FileInfo::MIMETYPE_FOLDER) {
$result = new \OC\Search\Result\Folder($fileData); $result = new \OC\Search\Result\Folder($fileData);
} }
// or create file result // or create file result

View File

@ -97,14 +97,13 @@ class File extends \OCP\Search\Result {
public function __construct(FileInfo $data) { public function __construct(FileInfo $data) {
$path = $this->getRelativePath($data->getPath()); $path = $this->getRelativePath($data->getPath());
$info = pathinfo($path);
$this->id = $data->getId(); $this->id = $data->getId();
$this->name = $info['basename']; $this->name = $data->getName();
$this->link = \OC::$server->getURLGenerator()->linkToRoute( $this->link = \OC::$server->getURLGenerator()->linkToRoute(
'files.view.index', 'files.view.index',
[ [
'dir' => $info['dirname'], 'dir' => dirname($path),
'scrollto' => $info['basename'], 'scrollto' => $data->getName(),
] ]
); );
$this->permissions = $data->getPermissions(); $this->permissions = $data->getPermissions();