diff options
author | Robin Appelman <robin@icewind.nl> | 2024-02-08 11:34:22 +0100 |
---|---|---|
committer | Robin Appelman <robin@icewind.nl> | 2024-03-04 12:26:30 +0100 |
commit | 4d110c1dd6ae384c00c93b4e266118004b71e498 (patch) | |
tree | f2a27509dd6d9b9094e062d69664568f156cd3a0 | |
parent | 2c9761c73ad1ea51f13103819637fbd315806761 (diff) | |
download | nextcloud-server-4d110c1dd6ae384c00c93b4e266118004b71e498.tar.gz nextcloud-server-4d110c1dd6ae384c00c93b4e266118004b71e498.zip |
feat: add interface to get only a single node by id instead of all nodes for the id
this should be enough in most(?) cases and makes efficient implementation and caching easier
Signed-off-by: Robin Appelman <robin@icewind.nl>
-rw-r--r-- | lib/private/Files/Node/Folder.php | 6 | ||||
-rw-r--r-- | lib/private/Files/Node/LazyFolder.php | 6 | ||||
-rw-r--r-- | lib/private/Files/Node/LazyRoot.php | 5 | ||||
-rw-r--r-- | lib/private/Files/Node/LazyUserFolder.php | 12 | ||||
-rw-r--r-- | lib/private/Files/Node/NonExistingFolder.php | 4 | ||||
-rw-r--r-- | lib/private/Files/Node/Root.php | 4 | ||||
-rw-r--r-- | lib/public/Files/Folder.php | 22 | ||||
-rw-r--r-- | lib/public/Files/IRootFolder.php | 18 |
8 files changed, 62 insertions, 15 deletions
diff --git a/lib/private/Files/Node/Folder.php b/lib/private/Files/Node/Folder.php index c7462572fed..cb8747e0055 100644 --- a/lib/private/Files/Node/Folder.php +++ b/lib/private/Files/Node/Folder.php @@ -307,12 +307,16 @@ class Folder extends Node implements \OCP\Files\Folder { /** * @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; diff --git a/lib/private/Files/Node/LazyFolder.php b/lib/private/Files/Node/LazyFolder.php index e30cfea693e..4aae5cf9804 100644 --- a/lib/private/Files/Node/LazyFolder.php +++ b/lib/private/Files/Node/LazyFolder.php @@ -492,7 +492,11 @@ class LazyFolder implements Folder { * @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()); } /** diff --git a/lib/private/Files/Node/LazyRoot.php b/lib/private/Files/Node/LazyRoot.php index 680e80cb45e..dd1596319fa 100644 --- a/lib/private/Files/Node/LazyRoot.php +++ b/lib/private/Files/Node/LazyRoot.php @@ -25,6 +25,7 @@ namespace OC\Files\Node; use OCP\Files\Cache\ICacheEntry; use OCP\Files\IRootFolder; use OCP\Files\Mount\IMountPoint; +use OCP\Files\Node; use OCP\Files\Node as INode; /** @@ -56,6 +57,10 @@ class LazyRoot extends LazyFolder implements IRootFolder { 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); } diff --git a/lib/private/Files/Node/LazyUserFolder.php b/lib/private/Files/Node/LazyUserFolder.php index 917ab80f366..3cb840ccb6d 100644 --- a/lib/private/Files/Node/LazyUserFolder.php +++ b/lib/private/Files/Node/LazyUserFolder.php @@ -68,18 +68,6 @@ class LazyUserFolder extends LazyFolder { ]); } - 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(); diff --git a/lib/private/Files/Node/NonExistingFolder.php b/lib/private/Files/Node/NonExistingFolder.php index 34621b18f19..af0ad002f24 100644 --- a/lib/private/Files/Node/NonExistingFolder.php +++ b/lib/private/Files/Node/NonExistingFolder.php @@ -162,6 +162,10 @@ class NonExistingFolder extends Folder { throw new NotFoundException(); } + public function getFirstNodeById(int $id): ?\OCP\Files\Node { + throw new NotFoundException(); + } + public function getFreeSpace() { throw new NotFoundException(); } diff --git a/lib/private/Files/Node/Root.php b/lib/private/Files/Node/Root.php index 9bfb610199b..bc76640966e 100644 --- a/lib/private/Files/Node/Root.php +++ b/lib/private/Files/Node/Root.php @@ -405,6 +405,10 @@ class Root extends Folder implements IRootFolder { return $this->userMountCache; } + public function getFirstNodeByIdInPath(int $id, string $path): ?INode { + return current($this->getByIdInPath($id, $path)); + } + /** * @param int $id * @return Node[] diff --git a/lib/public/Files/Folder.php b/lib/public/Files/Folder.php index eb81a2098ec..945df48a13e 100644 --- a/lib/public/Files/Folder.php +++ b/lib/public/Files/Folder.php @@ -152,11 +152,13 @@ interface Folder extends Node { 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 @@ -164,6 +166,24 @@ interface Folder extends Node { 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 * * @return int diff --git a/lib/public/Files/IRootFolder.php b/lib/public/Files/IRootFolder.php index 44f0ba5f2e1..c1c0e6e8c72 100644 --- a/lib/public/Files/IRootFolder.php +++ b/lib/public/Files/IRootFolder.php @@ -60,6 +60,24 @@ interface IRootFolder extends Folder, Emitter { 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[] * * @since 28.0.0 |