Skip to content

Commit a983470

Browse files
committed
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>
1 parent 9e90401 commit a983470

8 files changed

Lines changed: 63 additions & 16 deletions

File tree

lib/private/Files/Node/Folder.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,12 +307,16 @@ public function searchBySystemTag(string $tagName, string $userId, int $limit =
307307

308308
/**
309309
* @param int $id
310-
* @return \OC\Files\Node\Node[]
310+
* @return \OCP\Files\Node[]
311311
*/
312312
public function getById($id) {
313313
return $this->root->getByIdInPath((int)$id, $this->getPath());
314314
}
315315

316+
public function getFirstNodeById(int $id): ?\OCP\Files\Node {
317+
return current($this->getById($id));
318+
}
319+
316320
protected function getAppDataDirectoryName(): string {
317321
$instanceId = \OC::$server->getConfig()->getSystemValueString('instanceid');
318322
return 'appdata_' . $instanceId;

lib/private/Files/Node/LazyFolder.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function emit($scope, $method, $arguments = []) {
112112
$this->__call(__FUNCTION__, func_get_args());
113113
}
114114

115-
/**
115+
/**'
116116
* @inheritDoc
117117
*/
118118
public function mount($storage, $mountPoint, $arguments = []) {
@@ -492,7 +492,11 @@ public function searchBySystemTag(string $tagName, string $userId, int $limit =
492492
* @inheritDoc
493493
*/
494494
public function getById($id) {
495-
return $this->__call(__FUNCTION__, func_get_args());
495+
return $this->getRootFolder()->getByIdInPath((int)$id, $this->getPath());
496+
}
497+
498+
public function getFirstNodeById(int $id): ?\OCP\Files\Node {
499+
return $this->getRootFolder()->getFirstNodeByIdInPath($id, $this->getPath());
496500
}
497501

498502
/**

lib/private/Files/Node/LazyRoot.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use OCP\Files\Cache\ICacheEntry;
2626
use OCP\Files\IRootFolder;
2727
use OCP\Files\Mount\IMountPoint;
28+
use OCP\Files\Node;
2829
use OCP\Files\Node as INode;
2930

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

60+
public function getFirstNodeByIdInPath(int $id, string $path): ?Node {
61+
return $this->__call(__FUNCTION__, func_get_args());
62+
}
63+
5964
public function getNodeFromCacheEntryAndMount(ICacheEntry $cacheEntry, IMountPoint $mountPoint): INode {
6065
return $this->getRootFolder()->getNodeFromCacheEntryAndMount($cacheEntry, $mountPoint);
6166
}

lib/private/Files/Node/LazyUserFolder.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,6 @@ public function __construct(IRootFolder $rootFolder, IUser $user, IMountManager
6868
]);
6969
}
7070

71-
public function get($path) {
72-
return $this->getRootFolder()->get('/' . $this->user->getUID() . '/files/' . ltrim($path, '/'));
73-
}
74-
75-
/**
76-
* @param int $id
77-
* @return \OCP\Files\Node[]
78-
*/
79-
public function getById($id) {
80-
return $this->getRootFolder()->getByIdInPath((int)$id, $this->getPath());
81-
}
82-
8371
public function getMountPoint() {
8472
if ($this->folder !== null) {
8573
return $this->folder->getMountPoint();

lib/private/Files/Node/NonExistingFolder.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ public function getById($id) {
162162
throw new NotFoundException();
163163
}
164164

165+
public function getFirstNodeById(int $id): ?\OCP\Files\Node {
166+
throw new NotFoundException();
167+
}
168+
165169
public function getFreeSpace() {
166170
throw new NotFoundException();
167171
}

lib/private/Files/Node/Root.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,10 @@ public function getUserMountCache() {
405405
return $this->userMountCache;
406406
}
407407

408+
public function getFirstNodeByIdInPath(int $id, string $path): ?INode {
409+
return current($this->getByIdInPath($id, $path));
410+
}
411+
408412
/**
409413
* @param int $id
410414
* @return Node[]

lib/public/Files/Folder.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,17 +152,37 @@ public function searchByTag($tag, $userId);
152152
public function searchBySystemTag(string $tagName, string $userId, int $limit = 0, int $offset = 0);
153153

154154
/**
155-
* get a file or folder inside the folder by it's internal id
155+
* get a file or folder inside the folder by its internal id
156156
*
157157
* This method could return multiple entries. For example once the file/folder
158158
* is shared or mounted (files_external) to the user multiple times.
159159
*
160+
* Note that the different entries can have different permissions.
161+
*
160162
* @param int $id
161163
* @return \OCP\Files\Node[]
162164
* @since 6.0.0
163165
*/
164166
public function getById($id);
165167

168+
/**
169+
* get a file or folder inside the folder by its internal id
170+
*
171+
* Unlike getById, this method only returns a single node even if the user has
172+
* access to the file with the requested id multiple times.
173+
*
174+
* This method provides no guarantee about which of the nodes in returned and the
175+
* returned node might, for example, have less permissions than other nodes for the same file
176+
*
177+
* Apps that require accurate information about the users access to the file should use getById
178+
* instead of pick the correct node out of the result.
179+
*
180+
* @param int $id
181+
* @return Node|null
182+
* @since 29.0.0
183+
*/
184+
public function getFirstNodeById(int $id): ?Node;
185+
166186
/**
167187
* Get the amount of free space inside the folder
168188
*

lib/public/Files/IRootFolder.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,24 @@ public function getUserFolder($userId);
5959
*/
6060
public function getByIdInPath(int $id, string $path);
6161

62+
/**
63+
* get a file or folder inside the folder by its internal id
64+
*
65+
* Unlike getByIdInPath, this method only returns a single node even if the user has
66+
* access to the file with the requested id multiple times.
67+
*
68+
* This method provides no guarantee about which of the nodes in returned and the
69+
* returned node might, for example, have less permissions than other nodes for the same file
70+
*
71+
* Apps that require accurate information about the users access to the file should use getByIdInPath
72+
* instead of pick the correct node out of the result.
73+
*
74+
* @param int $id
75+
* @return Node|null
76+
* @since 29.0.0
77+
*/
78+
public function getFirstNodeByIdInPath(int $id, string $path): ?Node;
79+
6280
/**
6381
* @return IMountPoint[]
6482
*

0 commit comments

Comments
 (0)