aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files/lib/Controller/ApiController.php
diff options
context:
space:
mode:
authorChristopher Ng <chrng8@gmail.com>2024-08-07 20:52:29 -0700
committerChristopher Ng <chrng8@gmail.com>2024-08-08 10:52:20 -0700
commite9083db7682cd8347dcc18ab8c4a6349136bd138 (patch)
treeceaca9ad973f51f72369073b6d36e54bcfdf2fb1 /apps/files/lib/Controller/ApiController.php
parentb30054abb77e71ce2765950a01d83e0222e13b18 (diff)
downloadnextcloud-server-e9083db7682cd8347dcc18ab8c4a6349136bd138.tar.gz
nextcloud-server-e9083db7682cd8347dcc18ab8c4a6349136bd138.zip
feat(files): Allow specifying path and depth for returned folder tree
Signed-off-by: Christopher Ng <chrng8@gmail.com>
Diffstat (limited to 'apps/files/lib/Controller/ApiController.php')
-rw-r--r--apps/files/lib/Controller/ApiController.php81
1 files changed, 37 insertions, 44 deletions
diff --git a/apps/files/lib/Controller/ApiController.php b/apps/files/lib/Controller/ApiController.php
index 0d4503682b0..44e88c443e4 100644
--- a/apps/files/lib/Controller/ApiController.php
+++ b/apps/files/lib/Controller/ApiController.php
@@ -7,10 +7,7 @@
*/
namespace OCA\Files\Controller;
-use OC\AppFramework\Middleware\Security\Exceptions\NotLoggedInException;
use OC\Files\Node\Node;
-use OC\Files\Search\SearchComparison;
-use OC\Files\Search\SearchQuery;
use OCA\Files\ResponseDefinitions;
use OCA\Files\Service\TagService;
use OCA\Files\Service\UserConfig;
@@ -29,12 +26,10 @@ use OCP\AppFramework\Http\FileDisplayResponse;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\Http\StreamResponse;
-use OCP\Files\Cache\ICacheEntry;
use OCP\Files\File;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
-use OCP\Files\Search\ISearchComparison;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IPreview;
@@ -234,37 +229,33 @@ class ApiController extends Controller {
}
/**
- * @param Folder[] $folders
+ * @param \OCP\Files\Node[] $nodes
+ * @param int $depth The depth to traverse into the contents of each node
*/
- private function getTree(array $folders): array {
- $user = $this->userSession->getUser();
- if (!($user instanceof IUser)) {
- throw new NotLoggedInException();
+ private function getChildren(array $nodes, int $depth = 1, int $currentDepth = 0): array {
+ if ($currentDepth >= $depth) {
+ return [];
}
- $userFolder = $this->rootFolder->getUserFolder($user->getUID());
- $tree = [];
- foreach ($folders as $folder) {
- $path = $userFolder->getRelativePath($folder->getPath());
- if ($path === null) {
+ $children = [];
+ foreach ($nodes as $node) {
+ if (!($node instanceof Folder)) {
continue;
}
- $pathBasenames = explode('/', trim($path, '/'));
- $current = &$tree;
- foreach ($pathBasenames as $basename) {
- if (!isset($current['children'][$basename])) {
- $current['children'][$basename] = [
- 'id' => $folder->getId(),
- ];
- $displayName = $folder->getName();
- if ($displayName !== $basename) {
- $current['children'][$basename]['displayName'] = $displayName;
- }
- }
- $current = &$current['children'][$basename];
+
+ $basename = basename($node->getPath());
+ $entry = [
+ 'id' => $node->getId(),
+ 'basename' => $basename,
+ 'children' => $this->getChildren($node->getDirectoryListing(), $depth, $currentDepth + 1),
+ ];
+ $displayName = $node->getName();
+ if ($basename !== $displayName) {
+ $entry['displayName'] = $displayName;
}
+ $children[] = $entry;
}
- return $tree['children'] ?? $tree;
+ return $children;
}
/**
@@ -277,32 +268,34 @@ class ApiController extends Controller {
*/
#[NoAdminRequired]
#[ApiRoute(verb: 'GET', url: '/api/v1/folder-tree')]
- public function getFolderTree(): JSONResponse {
+ public function getFolderTree(string $path = '/', int $depth = 1): JSONResponse {
$user = $this->userSession->getUser();
if (!($user instanceof IUser)) {
return new JSONResponse([
'message' => $this->l10n->t('Failed to authorize'),
], Http::STATUS_UNAUTHORIZED);
}
-
- $userFolder = $this->rootFolder->getUserFolder($user->getUID());
try {
- $searchQuery = new SearchQuery(
- new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'mimetype', ICacheEntry::DIRECTORY_MIMETYPE),
- 0,
- 0,
- [],
- $user,
- false,
- );
- /** @var Folder[] $folders */
- $folders = $userFolder->search($searchQuery);
- $tree = $this->getTree($folders);
+ $userFolder = $this->rootFolder->getUserFolder($user->getUID());
+ $userFolderPath = $userFolder->getPath();
+ $fullPath = implode('/', [$userFolderPath, trim($path, '/')]);
+ $node = $this->rootFolder->get($fullPath);
+ if (!($node instanceof Folder)) {
+ return new JSONResponse([
+ 'message' => $this->l10n->t('Invalid folder path'),
+ ], Http::STATUS_BAD_REQUEST);
+ }
+ $nodes = $node->getDirectoryListing();
+ $tree = $this->getChildren($nodes, $depth);
+ } catch (NotFoundException $e) {
+ return new JSONResponse([
+ 'message' => $this->l10n->t('Folder not found'),
+ ], Http::STATUS_NOT_FOUND);
} catch (Throwable $th) {
$this->logger->error($th->getMessage(), ['exception' => $th]);
$tree = [];
}
- return new JSONResponse($tree, Http::STATUS_OK, [], JSON_FORCE_OBJECT);
+ return new JSONResponse($tree);
}
/**