Browse Source

perf: use more optimized node-by-id logic in View::getPath

Signed-off-by: Robin Appelman <robin@icewind.nl>
pull/54384/head
Robin Appelman 2 months ago
parent
commit
5987584b90
  1. 2
      lib/private/Files/Node/Root.php
  2. 44
      lib/private/Files/View.php

2
lib/private/Files/Node/Root.php

@ -415,7 +415,7 @@ class Root extends Folder implements IRootFolder {
*/
public function getByIdInPath(int $id, string $path): array {
$mountCache = $this->getUserMountCache();
if (strpos($path, '/', 1) > 0) {
if ($path !== '' && strpos($path, '/', 1) > 0) {
[, $user] = explode('/', $path);
} else {
$user = null;

44
lib/private/Files/View.php

@ -1828,43 +1828,25 @@ class View {
* @return string
* @throws NotFoundException
*/
public function getPath($id, ?int $storageId = null) {
public function getPath($id, ?int $storageId = null): string {
$id = (int)$id;
$manager = Filesystem::getMountManager();
$mounts = $manager->findIn($this->fakeRoot);
$mounts[] = $manager->find($this->fakeRoot);
$mounts = array_filter($mounts);
// reverse the array, so we start with the storage this view is in
// which is the most likely to contain the file we're looking for
$mounts = array_reverse($mounts);
// put non-shared mounts in front of the shared mount
// this prevents unneeded recursion into shares
usort($mounts, function (IMountPoint $a, IMountPoint $b) {
return $a instanceof SharedMount && (!$b instanceof SharedMount) ? 1 : -1;
});
$rootFolder = Server::get(Files\IRootFolder::class);
if (!is_null($storageId)) {
$mounts = array_filter($mounts, function (IMountPoint $mount) use ($storageId) {
return $mount->getNumericStorageId() === $storageId;
});
$node = $rootFolder->getFirstNodeByIdInPath($id, $this->getRoot());
if ($node) {
if ($storageId === null || $storageId === $node->getStorage()->getCache()->getNumericStorageId()) {
return $this->getRelativePath($node->getPath()) ?? '';
}
} else {
throw new NotFoundException(sprintf('File with id "%s" has not been found.', $id));
}
foreach ($mounts as $mount) {
/**
* @var \OC\Files\Mount\MountPoint $mount
*/
if ($mount->getStorage()) {
$cache = $mount->getStorage()->getCache();
$internalPath = $cache->getPathById($id);
if (is_string($internalPath)) {
$fullPath = $mount->getMountPoint() . $internalPath;
if (!is_null($path = $this->getRelativePath($fullPath))) {
return $path;
}
}
foreach ($rootFolder->getByIdInPath($id, $this->getRoot()) as $node) {
if ($storageId === $node->getStorage()->getCache()->getNumericStorageId()) {
return $this->getRelativePath($node->getPath()) ?? '';
}
}
throw new NotFoundException(sprintf('File with id "%s" has not been found.', $id));
}

Loading…
Cancel
Save