From 7cc9ba28a7e221e7e9f3723f3a6fb51ad57ea152 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Tue, 31 Jan 2023 08:48:45 +0100 Subject: [PATCH 1/2] perf(federation): Only request root share info for checking availability MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Otherwise this would request a full recursive dirctory listing while the result is never being used Signed-off-by: Julius Härtl --- .../lib/Controller/ShareInfoController.php | 16 ++++++++++------ apps/files_sharing/lib/External/Storage.php | 6 +++--- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/apps/files_sharing/lib/Controller/ShareInfoController.php b/apps/files_sharing/lib/Controller/ShareInfoController.php index b090e6efcf183..2b8373af1cd76 100644 --- a/apps/files_sharing/lib/Controller/ShareInfoController.php +++ b/apps/files_sharing/lib/Controller/ShareInfoController.php @@ -65,7 +65,7 @@ public function __construct(string $appName, * @param ?string $dir * @return JSONResponse */ - public function info(string $t, ?string $password = null, ?string $dir = null) { + public function info(string $t, ?string $password = null, ?string $dir = null, int $depth = -1) { try { $share = $this->shareManager->getShareByToken($t); } catch (ShareNotFound $e) { @@ -96,28 +96,32 @@ public function info(string $t, ?string $password = null, ?string $dir = null) { } } - return new JSONResponse($this->parseNode($node, $permissionMask)); + return new JSONResponse($this->parseNode($node, $permissionMask, $depth)); } - private function parseNode(Node $node, int $permissionMask) { + private function parseNode(Node $node, int $permissionMask, int $depth) { if ($node instanceof File) { return $this->parseFile($node, $permissionMask); } - return $this->parseFolder($node, $permissionMask); + return $this->parseFolder($node, $permissionMask, $depth); } private function parseFile(File $file, int $permissionMask) { return $this->format($file, $permissionMask); } - private function parseFolder(Folder $folder, int $permissionMask) { + private function parseFolder(Folder $folder, int $permissionMask, int $depth) { $data = $this->format($folder, $permissionMask); + if ($depth === 0) { + return $data; + } + $data['children'] = []; $nodes = $folder->getDirectoryListing(); foreach ($nodes as $node) { - $data['children'][] = $this->parseNode($node, $permissionMask); + $data['children'][] = $this->parseNode($node, $permissionMask, $depth <= -1 ? -1 : $depth - 1); } return $data; diff --git a/apps/files_sharing/lib/External/Storage.php b/apps/files_sharing/lib/External/Storage.php index 43568ea6a2b01..f33334ca3466d 100644 --- a/apps/files_sharing/lib/External/Storage.php +++ b/apps/files_sharing/lib/External/Storage.php @@ -214,7 +214,7 @@ public function test() { public function checkStorageAvailability() { // see if we can find out why the share is unavailable try { - $this->getShareInfo(); + $this->getShareInfo(0); } catch (NotFoundException $e) { // a 404 can either mean that the share no longer exists or there is no Nextcloud on the remote if ($this->testRemote()) { @@ -308,7 +308,7 @@ public function remoteIsOwnCloud(): bool { * @throws NotFoundException * @throws \Exception */ - public function getShareInfo() { + public function getShareInfo(int $depth = -1) { $remote = $this->getRemote(); $token = $this->getToken(); $password = $this->getPassword(); @@ -331,7 +331,7 @@ public function getShareInfo() { $client = \OC::$server->getHTTPClientService()->newClient(); try { $response = $client->post($url, [ - 'body' => ['password' => $password], + 'body' => ['password' => $password, 'depth' => $depth], 'timeout' => 10, 'connect_timeout' => 10, ]); From 7e3ea019d1c9f8eb5486de802f04be194c4d1f2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Tue, 31 Jan 2023 09:00:10 +0100 Subject: [PATCH 2/2] chore: Add return types MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- .../lib/Controller/ShareInfoController.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/apps/files_sharing/lib/Controller/ShareInfoController.php b/apps/files_sharing/lib/Controller/ShareInfoController.php index 2b8373af1cd76..b6242f9ee9a07 100644 --- a/apps/files_sharing/lib/Controller/ShareInfoController.php +++ b/apps/files_sharing/lib/Controller/ShareInfoController.php @@ -65,7 +65,7 @@ public function __construct(string $appName, * @param ?string $dir * @return JSONResponse */ - public function info(string $t, ?string $password = null, ?string $dir = null, int $depth = -1) { + public function info(string $t, ?string $password = null, ?string $dir = null, int $depth = -1): JSONResponse { try { $share = $this->shareManager->getShareByToken($t); } catch (ShareNotFound $e) { @@ -99,18 +99,19 @@ public function info(string $t, ?string $password = null, ?string $dir = null, i return new JSONResponse($this->parseNode($node, $permissionMask, $depth)); } - private function parseNode(Node $node, int $permissionMask, int $depth) { + private function parseNode(Node $node, int $permissionMask, int $depth): array { if ($node instanceof File) { return $this->parseFile($node, $permissionMask); } + /** @var Folder $node */ return $this->parseFolder($node, $permissionMask, $depth); } - private function parseFile(File $file, int $permissionMask) { + private function parseFile(File $file, int $permissionMask): array { return $this->format($file, $permissionMask); } - private function parseFolder(Folder $folder, int $permissionMask, int $depth) { + private function parseFolder(Folder $folder, int $permissionMask, int $depth): array { $data = $this->format($folder, $permissionMask); if ($depth === 0) { @@ -127,7 +128,7 @@ private function parseFolder(Folder $folder, int $permissionMask, int $depth) { return $data; } - private function format(Node $node, int $permissionMask) { + private function format(Node $node, int $permissionMask): array { $entry = []; $entry['id'] = $node->getId();