Merge pull request #26087 from nextcloud/backport/26049/stable21

[stable21] limit constructing of result objects in file search
This commit is contained in:
Vincent Petry 2021-03-18 08:21:50 +01:00 committed by GitHub
commit 73d2f42d41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 4 deletions

View File

@ -103,7 +103,7 @@ class FilesSearchProvider implements IProvider {
// Make sure we setup the users filesystem // Make sure we setup the users filesystem
$this->rootFolder->getUserFolder($user->getUID()); $this->rootFolder->getUserFolder($user->getUID());
return SearchResult::complete( return SearchResult::paginated(
$this->l10n->t('Files'), $this->l10n->t('Files'),
array_map(function (FileResult $result) { array_map(function (FileResult $result) {
// Generate thumbnail url // Generate thumbnail url
@ -121,7 +121,8 @@ class FilesSearchProvider implements IProvider {
$searchResultEntry->addAttribute('fileId', (string)$result->id); $searchResultEntry->addAttribute('fileId', (string)$result->id);
$searchResultEntry->addAttribute('path', $result->path); $searchResultEntry->addAttribute('path', $result->path);
return $searchResultEntry; return $searchResultEntry;
}, $this->fileSearch->search($query->getTerm())) }, $this->fileSearch->search($query->getTerm(), $query->getLimit(), (int)$query->getCursor())),
$query->getCursor() + $query->getLimit()
); );
} }

View File

@ -30,23 +30,33 @@
namespace OC\Search\Provider; namespace OC\Search\Provider;
use OC\Files\Filesystem; use OC\Files\Filesystem;
use OCP\Search\PagedProvider;
/** /**
* Provide search results from the 'files' app * Provide search results from the 'files' app
* @deprecated 20.0.0 * @deprecated 20.0.0
*/ */
class File extends \OCP\Search\Provider { class File extends PagedProvider {
/** /**
* Search for files and folders matching the given query * Search for files and folders matching the given query
*
* @param string $query * @param string $query
* @param int|null $limit
* @param int|null $offset
* @return \OCP\Search\Result[] * @return \OCP\Search\Result[]
* @deprecated 20.0.0 * @deprecated 20.0.0
*/ */
public function search($query) { public function search($query, int $limit = null, int $offset = null) {
if ($offset === null) {
$offset = 0;
}
\OC_Util::setupFS(); \OC_Util::setupFS();
$files = Filesystem::search($query); $files = Filesystem::search($query);
$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 // skip versions
@ -79,4 +89,12 @@ class File extends \OCP\Search\Provider {
// return // return
return $results; return $results;
} }
public function searchPaged($query, $page, $size) {
if ($size === 0) {
return $this->search($query);
} else {
return $this->search($query, $size, ($page - 1) * $size);
}
}
} }