Skip to content

Commit f55a613

Browse files
committed
remove recursive parse on shareinfo
Signed-off-by: Maxence Lange <[email protected]>
1 parent 5104ee9 commit f55a613

1 file changed

Lines changed: 73 additions & 8 deletions

File tree

apps/files_sharing/lib/Controller/ShareInfoController.php

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

26-
use OCA\Files_External\NotFoundException;
2726
use OCP\AppFramework\ApiController;
2827
use OCP\AppFramework\Http;
2928
use OCP\AppFramework\Http\JSONResponse;
3029
use OCP\Constants;
3130
use OCP\Files\File;
3231
use OCP\Files\Folder;
3332
use OCP\Files\Node;
33+
use OCP\Files\NotFoundException;
3434
use OCP\IRequest;
3535
use OCP\Share\Exceptions\ShareNotFound;
3636
use OCP\Share\IManager;
@@ -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 $startAt = 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(), $startAt);
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,88 @@ 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) {
114-
$data = $this->format($folder, $permissionMask);
115121

122+
private function parseFolder(Folder $folder, int $permissionMask, bool $recursive = true) {
123+
$data = $this->format($folder, $permissionMask);
116124
$data['children'] = [];
117125

126+
if (!$recursive && $folder->getSize() > 0) {
127+
$data['hasChildren'] = true;
128+
129+
return $data;
130+
}
131+
132+
// in case of [sub]folders containing only empty files
118133
$nodes = $folder->getDirectoryListing();
134+
if (!$recursive && count($nodes) > 0) {
135+
$data['hasChildren'] = $data['containsEmptyFilesOnly'] = true;
136+
137+
return $data;
138+
}
139+
119140
foreach ($nodes as $node) {
120-
$data['children'][] = $this->parseNode($node, $permissionMask);
141+
$data['children'][] = $this->parseNode($node, $permissionMask, false);
121142
}
122143

123144
return $data;
124145
}
125146

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

0 commit comments

Comments
 (0)