Skip to content

Commit

Permalink
add interface to get only a single node by id instead of all nodes fo…
Browse files Browse the repository at this point in the history
…r the id

this should be enough in most(?) cases and makes efficient implementation and caching easier

Signed-off-by: Robin Appelman <[email protected]>
  • Loading branch information
icewind1991 committed Feb 8, 2024
1 parent 9e90401 commit a983470
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 16 deletions.
6 changes: 5 additions & 1 deletion lib/private/Files/Node/Folder.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,12 +307,16 @@ public function searchBySystemTag(string $tagName, string $userId, int $limit =

/**
* @param int $id
* @return \OC\Files\Node\Node[]
* @return \OCP\Files\Node[]
*/
public function getById($id) {
return $this->root->getByIdInPath((int)$id, $this->getPath());
}

public function getFirstNodeById(int $id): ?\OCP\Files\Node {
return current($this->getById($id));
}

protected function getAppDataDirectoryName(): string {
$instanceId = \OC::$server->getConfig()->getSystemValueString('instanceid');
return 'appdata_' . $instanceId;
Expand Down
8 changes: 6 additions & 2 deletions lib/private/Files/Node/LazyFolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function emit($scope, $method, $arguments = []) {
$this->__call(__FUNCTION__, func_get_args());
}

/**
/**'
* @inheritDoc
*/
public function mount($storage, $mountPoint, $arguments = []) {
Expand Down Expand Up @@ -492,7 +492,11 @@ public function searchBySystemTag(string $tagName, string $userId, int $limit =
* @inheritDoc
*/
public function getById($id) {
return $this->__call(__FUNCTION__, func_get_args());
return $this->getRootFolder()->getByIdInPath((int)$id, $this->getPath());
}

public function getFirstNodeById(int $id): ?\OCP\Files\Node {
return $this->getRootFolder()->getFirstNodeByIdInPath($id, $this->getPath());
}

/**
Expand Down
5 changes: 5 additions & 0 deletions lib/private/Files/Node/LazyRoot.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use OCP\Files\Cache\ICacheEntry;
use OCP\Files\IRootFolder;
use OCP\Files\Mount\IMountPoint;
use OCP\Files\Node;
use OCP\Files\Node as INode;

/**
Expand Down Expand Up @@ -56,6 +57,10 @@ public function getByIdInPath(int $id, string $path) {
return $this->__call(__FUNCTION__, func_get_args());
}

public function getFirstNodeByIdInPath(int $id, string $path): ?Node {
return $this->__call(__FUNCTION__, func_get_args());
}

public function getNodeFromCacheEntryAndMount(ICacheEntry $cacheEntry, IMountPoint $mountPoint): INode {
return $this->getRootFolder()->getNodeFromCacheEntryAndMount($cacheEntry, $mountPoint);
}
Expand Down
12 changes: 0 additions & 12 deletions lib/private/Files/Node/LazyUserFolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,6 @@ public function __construct(IRootFolder $rootFolder, IUser $user, IMountManager
]);
}

public function get($path) {
return $this->getRootFolder()->get('/' . $this->user->getUID() . '/files/' . ltrim($path, '/'));
}

/**
* @param int $id
* @return \OCP\Files\Node[]
*/
public function getById($id) {
return $this->getRootFolder()->getByIdInPath((int)$id, $this->getPath());
}

public function getMountPoint() {
if ($this->folder !== null) {
return $this->folder->getMountPoint();
Expand Down
4 changes: 4 additions & 0 deletions lib/private/Files/Node/NonExistingFolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ public function getById($id) {
throw new NotFoundException();
}

public function getFirstNodeById(int $id): ?\OCP\Files\Node {
throw new NotFoundException();
}

public function getFreeSpace() {
throw new NotFoundException();
}
Expand Down
4 changes: 4 additions & 0 deletions lib/private/Files/Node/Root.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,10 @@ public function getUserMountCache() {
return $this->userMountCache;
}

public function getFirstNodeByIdInPath(int $id, string $path): ?INode {
return current($this->getByIdInPath($id, $path));
}

/**
* @param int $id
* @return Node[]
Expand Down
22 changes: 21 additions & 1 deletion lib/public/Files/Folder.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,17 +152,37 @@ public function searchByTag($tag, $userId);
public function searchBySystemTag(string $tagName, string $userId, int $limit = 0, int $offset = 0);

/**
* get a file or folder inside the folder by it's internal id
* get a file or folder inside the folder by its internal id
*
* This method could return multiple entries. For example once the file/folder
* is shared or mounted (files_external) to the user multiple times.
*
* Note that the different entries can have different permissions.
*
* @param int $id
* @return \OCP\Files\Node[]
* @since 6.0.0
*/
public function getById($id);

/**
* get a file or folder inside the folder by its internal id
*
* Unlike getById, this method only returns a single node even if the user has
* access to the file with the requested id multiple times.
*
* This method provides no guarantee about which of the nodes in returned and the
* returned node might, for example, have less permissions than other nodes for the same file
*
* Apps that require accurate information about the users access to the file should use getById
* instead of pick the correct node out of the result.
*
* @param int $id
* @return Node|null
* @since 29.0.0
*/
public function getFirstNodeById(int $id): ?Node;

/**
* Get the amount of free space inside the folder
*
Expand Down
18 changes: 18 additions & 0 deletions lib/public/Files/IRootFolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,24 @@ public function getUserFolder($userId);
*/
public function getByIdInPath(int $id, string $path);

/**
* get a file or folder inside the folder by its internal id
*
* Unlike getByIdInPath, this method only returns a single node even if the user has
* access to the file with the requested id multiple times.
*
* This method provides no guarantee about which of the nodes in returned and the
* returned node might, for example, have less permissions than other nodes for the same file
*
* Apps that require accurate information about the users access to the file should use getByIdInPath
* instead of pick the correct node out of the result.
*
* @param int $id
* @return Node|null
* @since 29.0.0
*/
public function getFirstNodeByIdInPath(int $id, string $path): ?Node;

/**
* @return IMountPoint[]
*
Expand Down

0 comments on commit a983470

Please sign in to comment.