Optimize folders listing

pull/62/head
Varun Patil 2022-09-25 02:46:01 -07:00
parent 5fc05b4c26
commit 821e10525e
1 changed files with 17 additions and 16 deletions

View File

@ -162,7 +162,7 @@ class ApiController extends Controller {
// Add subfolder info if querying non-recursively // Add subfolder info if querying non-recursively
if (!$recursive) { if (!$recursive) {
$this->addSubfolders($folder, $list, $user); array_unshift($list, $this->getSubfoldersEntry($folder));
} }
return new JSONResponse($list, Http::STATUS_OK); return new JSONResponse($list, Http::STATUS_OK);
@ -199,25 +199,28 @@ class ApiController extends Controller {
} }
/** /**
* @NoAdminRequired * Get subfolders entry for days response
*/ */
public function addSubfolders(Folder &$folder, &$list, &$user) { public function getSubfoldersEntry(Folder &$folder) {
// Get subdirectories // Ugly: get the view of the folder with reflection
$sub = $folder->search(new SearchQuery( // This is unfortunately the only way to get the contents of a folder
new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'mimetype', FileInfo::MIMETYPE_FOLDER), // matching a MIME type without using SEARCH, which is deep
0, 0, [], $user)); $rp = new \ReflectionProperty('\OC\Files\Node\Node', 'view');
$sub = array_filter($sub, function ($item) use (&$folder) { $rp->setAccessible(true);
return $item->getParent()->getId() === $folder->getId(); $view = $rp->getValue($folder);
});
// Get the subfolders
$folders = $view->getDirectoryContent($folder->getPath(), FileInfo::MIMETYPE_FOLDER, $folder);
// Sort by name // Sort by name
usort($sub, function($a, $b) { usort($folders, function($a, $b) {
return strnatcmp($a->getName(), $b->getName()); return strnatcmp($a->getName(), $b->getName());
}); });
// Map sub to JSON array // Process to response type
$subdirArray = [ return [
"dayid" => \OCA\Memories\Util::$TAG_DAYID_FOLDERS, "dayid" => \OCA\Memories\Util::$TAG_DAYID_FOLDERS,
"count" => count($folders),
"detail" => array_map(function (&$node) { "detail" => array_map(function (&$node) {
return [ return [
"fileid" => $node->getId(), "fileid" => $node->getId(),
@ -225,10 +228,8 @@ class ApiController extends Controller {
"isfolder" => 1, "isfolder" => 1,
"path" => $node->getPath(), "path" => $node->getPath(),
]; ];
}, $sub, []), }, $folders, []),
]; ];
$subdirArray["count"] = count($subdirArray["detail"]);
array_unshift($list, $subdirArray);
} }
/** /**