Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 73 additions & 8 deletions apps/files_sharing/lib/Controller/ShareInfoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
*/
namespace OCA\Files_Sharing\Controller;

use OCA\Files_External\NotFoundException;
use OCP\AppFramework\ApiController;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
use OCP\Constants;
use OCP\Files\File;
use OCP\Files\Folder;
use OCP\Files\Node;
use OCP\Files\NotFoundException;
use OCP\IRequest;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IManager;
Expand Down Expand Up @@ -65,7 +65,7 @@ public function __construct(string $appName,
* @param null $dir
* @return JSONResponse
*/
public function info($t, $password = null, $dir = null) {
public function info($t, $password = null, $dir = null, int $startAt = 0) {
try {
$share = $this->shareManager->getShareByToken($t);
} catch (ShareNotFound $e) {
Expand All @@ -87,7 +87,14 @@ public function info($t, $password = null, $dir = null) {
}

$permissionMask = $share->getPermissions();
$node = $share->getNode();

try {
$node = $this->getFirstNode($share->getNode(), $startAt);
} catch (NotFoundException $e) {
$response = new JSONResponse([], Http::STATUS_NOT_FOUND);
$response->throttle(['token' => $t]);
return $response;
}

if ($dir !== null && $node instanceof Folder) {
try {
Expand All @@ -99,30 +106,88 @@ public function info($t, $password = null, $dir = null) {
return new JSONResponse($this->parseNode($node, $permissionMask));
}

private function parseNode(Node $node, int $permissionMask) {
private function parseNode(Node $node, int $permissionMask, bool $recursive = true) {

Check notice

Code scanning / Psalm

MissingReturnType

Method OCA\Files_Sharing\Controller\ShareInfoController::parseNode does not have a return type
if ($node instanceof File) {
return $this->parseFile($node, $permissionMask);
}
return $this->parseFolder($node, $permissionMask);

return $this->parseFolder($node, $permissionMask, $recursive);

Check notice

Code scanning / Psalm

ArgumentTypeCoercion

Argument 1 of OCA\Files_Sharing\Controller\ShareInfoController::parseFolder expects OCP\Files\Folder, parent type OCP\Files\Node provided
}

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

private function parseFolder(Folder $folder, int $permissionMask) {
$data = $this->format($folder, $permissionMask);

private function parseFolder(Folder $folder, int $permissionMask, bool $recursive = true) {

Check notice

Code scanning / Psalm

MissingReturnType

Method OCA\Files_Sharing\Controller\ShareInfoController::parseFolder does not have a return type
$data = $this->format($folder, $permissionMask);
$data['children'] = [];

if (!$recursive && $folder->getSize() > 0) {
$data['hasChildren'] = true;

return $data;
}

// in case of [sub]folders containing only empty files
$nodes = $folder->getDirectoryListing();
if (!$recursive && count($nodes) > 0) {
$data['hasChildren'] = $data['containsEmptyFilesOnly'] = true;

return $data;
}

foreach ($nodes as $node) {
$data['children'][] = $this->parseNode($node, $permissionMask);
$data['children'][] = $this->parseNode($node, $permissionMask, false);
}

return $data;
}

/**
* @param Node $node
* @param int $startAt
*
* @return Node
*/
private function getFirstNode(Node $node, int $startAt): Node {
// returns current node if $st is not set, or set to current nodeId
if ($startAt < 1 || $node->getId() === $startAt) {
return $node;
}

// checking all path that link to node_id set as $st
// will returns the node that fit current node full path
/** @var Node[] $subs */
$subs = $node->getById($startAt);

Check failure

Code scanning / Psalm

UndefinedInterfaceMethod

Method OCP\Files\Node::getById does not exist
if (empty($subs)) {
throw new NotFoundException();
}

$curr = $node->getPath();
foreach($subs as $sub) {
$pos = strpos($sub->getPath(), $curr);
if ($pos === false || $pos <> 0) {
continue;
}

$subPath = trim(substr($sub->getPath(), strlen($curr)), '/');
try {
$new = $node;
foreach (explode('/', $subPath) as $subFolder) {
$new = $new->get($subFolder);

Check notice

Code scanning / Psalm

PossiblyUndefinedMethod

Method OCP\Files\Node::get does not exist
}
} catch (NotFoundException $e) {
continue;
}

return $new;
}

throw new NotFoundException();
}


private function format(Node $node, int $permissionMask) {
$entry = [];

Expand Down