Skip to content

Commit 3fd8249

Browse files
committed
remove recursive parse on shareinfo
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
1 parent 5104ee9 commit 3fd8249

1 file changed

Lines changed: 63 additions & 7 deletions

File tree

apps/files_sharing/lib/Controller/ShareInfoController.php

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*/
2424
namespace OCA\Files_Sharing\Controller;
2525

26-
use OCA\Files_External\NotFoundException;
26+
use OCP\Files\NotFoundException;
2727
use OCP\AppFramework\ApiController;
2828
use OCP\AppFramework\Http;
2929
use OCP\AppFramework\Http\JSONResponse;
@@ -65,7 +65,7 @@ public function __construct(string $appName,
6565
* @param null $dir
6666
* @return JSONResponse
6767
*/
68-
public function info($t, $password = null, $dir = null) {
68+
public function info($t, $password = null, $dir = null, int $st = 0) {
6969
try {
7070
$share = $this->shareManager->getShareByToken($t);
7171
} catch (ShareNotFound $e) {
@@ -87,7 +87,14 @@ public function info($t, $password = null, $dir = null) {
8787
}
8888

8989
$permissionMask = $share->getPermissions();
90-
$node = $share->getNode();
90+
91+
try {
92+
$node = $this->getFirstNode($share->getNode(), $st);
93+
} catch (NotFoundException $e) {
94+
$response = new JSONResponse([], Http::STATUS_NOT_FOUND);
95+
$response->throttle(['token' => $t]);
96+
return $response;
97+
}
9198

9299
if ($dir !== null && $node instanceof Folder) {
93100
try {
@@ -99,30 +106,79 @@ public function info($t, $password = null, $dir = null) {
99106
return new JSONResponse($this->parseNode($node, $permissionMask));
100107
}
101108

102-
private function parseNode(Node $node, int $permissionMask) {
109+
private function parseNode(Node $node, int $permissionMask, bool $recursive = true) {
103110
if ($node instanceof File) {
104111
return $this->parseFile($node, $permissionMask);
105112
}
106-
return $this->parseFolder($node, $permissionMask);
113+
114+
return $this->parseFolder($node, $permissionMask, $recursive);
107115
}
108116

109117
private function parseFile(File $file, int $permissionMask) {
110118
return $this->format($file, $permissionMask);
111119
}
112120

113-
private function parseFolder(Folder $folder, int $permissionMask) {
121+
private function parseFolder(Folder $folder, int $permissionMask, bool $recursive = true) {
114122
$data = $this->format($folder, $permissionMask);
115123

116124
$data['children'] = [];
117125

118126
$nodes = $folder->getDirectoryListing();
119127
foreach ($nodes as $node) {
120-
$data['children'][] = $this->parseNode($node, $permissionMask);
128+
if ($recursive) {
129+
$data['children'][] = $this->parseNode($node, $permissionMask, false);
130+
} else {
131+
$data['hasChildren'] = true;
132+
}
121133
}
122134

123135
return $data;
124136
}
125137

138+
/**
139+
* @param Node $node
140+
* @param int $st
141+
*
142+
* @return Node
143+
*/
144+
private function getFirstNode(Node $node, int $st): Node {
145+
// returns current node if $st is not set, or set to current nodeId
146+
if ($st < 1 || $node->getId() === $st) {
147+
return $node;
148+
}
149+
150+
// checking all path that link to node_id set as $st
151+
// will returns the node that fit current node full path
152+
/** @var Node[] $subs */
153+
$subs = $node->getById($st);
154+
if (empty($subs)) {
155+
throw new NotFoundException();
156+
}
157+
158+
$curr = $node->getPath();
159+
foreach($subs as $sub) {
160+
$pos = strpos($sub->getPath(), $curr);
161+
if ($pos === false || $pos <> 0) {
162+
continue;
163+
}
164+
165+
$subPath = trim(substr($sub->getPath(), strlen($curr)), '/');
166+
try {
167+
$new = $node;
168+
foreach (explode('/', $subPath) as $subFolder) {
169+
$new = $new->get($subFolder);
170+
}
171+
} catch (NotFoundException $e) {
172+
continue;
173+
}
174+
175+
return $new;
176+
}
177+
178+
throw new NotFoundException();
179+
}
180+
181+
126182
private function format(Node $node, int $permissionMask) {
127183
$entry = [];
128184

0 commit comments

Comments
 (0)