From af78da59bcd45b4d4f3bec3f7547f93ea5ca55eb Mon Sep 17 00:00:00 2001 From: Josh Date: Tue, 10 Mar 2026 19:21:47 -0400 Subject: [PATCH] refactor(View): improve readability of getLocalFile and resolvePath Signed-off-by: Josh --- lib/private/Files/View.php | 45 +++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/lib/private/Files/View.php b/lib/private/Files/View.php index 21265b7d048..e85cd680371 100644 --- a/lib/private/Files/View.php +++ b/lib/private/Files/View.php @@ -185,33 +185,42 @@ class View { } /** - * Resolve a path to a storage and internal path + * Resolve a path to a storage and internal path. * - * @param string $path - * @return array{?IStorage, string} an array consisting of the storage and the internal path + * Accepts both: + * - relative paths (interpreted relative to this view root), and + * - absolute paths in Nextcloud's virtual filesystem (leading '/'). + * + * @param string $path Relative path, or absolute virtual filesystem path + * @return array{?IStorage, string} An array containing [storage, internalPath] */ - public function resolvePath($path): array { - $a = $this->getAbsolutePath($path); - $p = Filesystem::normalizePath($a); - return Filesystem::resolvePath($p); + public function resolvePath(string $path): array { + $absolutePath = $this->getAbsolutePath($path); + $normalizedPath = Filesystem::normalizePath($absolutePath); + return Filesystem::resolvePath($normalizedPath); } /** - * Return the path to a local version of the file - * we need this because we can't know if a file is stored local or not from - * outside the filestorage and for some purposes a local file is needed + * Return the path to a local representation of a file. * - * @param string $path + * For local storages this is usually the real on-disk path. + * For non-local storages this may be a temporary local file. + * + * @param string $path Path relative to this view, or absolute virtual filesystem path + * @return string|false Local file path, or false if unavailable */ - public function getLocalFile($path): string|false { - $parent = substr($path, 0, strrpos($path, '/') ?: 0); - $path = $this->getAbsolutePath($path); - [$storage, $internalPath] = Filesystem::resolvePath($path); - if (Filesystem::isValidPath($parent) && $storage) { - return $storage->getLocalFile($internalPath); - } else { + public function getLocalFile(string $path): string|false { + $parentPath = dirname($path); + if (!Filesystem::isValidPath($parentPath)) { return false; } + + [$storage, $internalPath] = $this->resolvePath($path); + if (!$storage) { + return false; + } + + return $storage->getLocalFile($internalPath); } /**