From 151c80039751bbe74042d7f6f5d58e9f115064e4 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Tue, 5 Apr 2022 15:29:49 +0200 Subject: [PATCH] allow reusing known folder info when getting directory contents Signed-off-by: Robin Appelman --- apps/dav/lib/Connector/Sabre/Directory.php | 4 ++-- apps/dav/lib/Connector/Sabre/Node.php | 14 +++++++++----- lib/private/Files/Node/Folder.php | 4 ++-- lib/private/Files/View.php | 21 ++++++++++++++------- tests/lib/Files/Node/FolderTest.php | 2 ++ 5 files changed, 29 insertions(+), 16 deletions(-) diff --git a/apps/dav/lib/Connector/Sabre/Directory.php b/apps/dav/lib/Connector/Sabre/Directory.php index 54ee14fdaf306..8b616b0cb8a1b 100644 --- a/apps/dav/lib/Connector/Sabre/Directory.php +++ b/apps/dav/lib/Connector/Sabre/Directory.php @@ -236,7 +236,7 @@ public function getChild($name, $info = null) { throw new \Sabre\DAV\Exception\NotFound('File with name ' . $path . ' could not be located'); } - if ($info['mimetype'] === 'httpd/unix-directory') { + if ($info->getMimeType() === FileInfo::MIMETYPE_FOLDER) { $node = new \OCA\DAV\Connector\Sabre\Directory($this->fileView, $info, $this->tree, $this->shareManager); } else { $node = new \OCA\DAV\Connector\Sabre\File($this->fileView, $info, $this->shareManager); @@ -264,7 +264,7 @@ public function getChildren() { // the caller believe that the collection itself does not exist throw new Forbidden('No read permissions'); } - $folderContent = $this->fileView->getDirectoryContent($this->path); + $folderContent = $this->getNode()->getDirectoryListing(); } catch (LockedException $e) { throw new Locked(); } diff --git a/apps/dav/lib/Connector/Sabre/Node.php b/apps/dav/lib/Connector/Sabre/Node.php index 79f1653c692cd..e4517068f4248 100644 --- a/apps/dav/lib/Connector/Sabre/Node.php +++ b/apps/dav/lib/Connector/Sabre/Node.php @@ -78,7 +78,7 @@ abstract class Node implements \Sabre\DAV\INode { */ protected $shareManager; - protected \OC\Files\Node\Node $node; + protected \OCP\Files\Node $node; /** * Sets up the node, expects a full path name @@ -96,11 +96,15 @@ public function __construct(View $view, FileInfo $info, IManager $shareManager = } else { $this->shareManager = \OC::$server->getShareManager(); } - $root = \OC::$server->get(IRootFolder::class); - if ($info->getType()=== FileInfo::TYPE_FOLDER) { - $this->node = new Folder($root, $view, $this->path, $info); + if ($info instanceof Folder || $info instanceof File) { + $this->node = $info; } else { - $this->node = new File($root, $view, $this->path, $info); + $root = \OC::$server->get(IRootFolder::class); + if ($info->getType() === FileInfo::TYPE_FOLDER) { + $this->node = new Folder($root, $view, $this->path, $info); + } else { + $this->node = new File($root, $view, $this->path, $info); + } } } diff --git a/lib/private/Files/Node/Folder.php b/lib/private/Files/Node/Folder.php index d058805b20ec6..9c15f0edf416f 100644 --- a/lib/private/Files/Node/Folder.php +++ b/lib/private/Files/Node/Folder.php @@ -97,10 +97,10 @@ public function isSubNode($node) { * @throws \OCP\Files\NotFoundException */ public function getDirectoryListing() { - $folderContent = $this->view->getDirectoryContent($this->path); + $folderContent = $this->view->getDirectoryContent($this->path, '', $this->getFileInfo()); return array_map(function (FileInfo $info) { - if ($info->getMimetype() === 'httpd/unix-directory') { + if ($info->getMimetype() === FileInfo::MIMETYPE_FOLDER) { return new Folder($this->root, $this->view, $info->getPath(), $info); } else { return new File($this->root, $this->view, $info->getPath(), $info); diff --git a/lib/private/Files/View.php b/lib/private/Files/View.php index e23dd4312aad8..eef87cc65f4e0 100644 --- a/lib/private/Files/View.php +++ b/lib/private/Files/View.php @@ -1431,7 +1431,7 @@ public function getFileInfo($path, $includeMountPoints = true) { * @param string $mimetype_filter limit returned content to this mimetype or mimepart * @return FileInfo[] */ - public function getDirectoryContent($directory, $mimetype_filter = '') { + public function getDirectoryContent($directory, $mimetype_filter = '', \OCP\Files\FileInfo $directoryInfo = null) { $this->assertPathLength($directory); if (!Filesystem::isValidPath($directory)) { return []; @@ -1449,14 +1449,21 @@ public function getDirectoryContent($directory, $mimetype_filter = '') { $cache = $storage->getCache($internalPath); $user = \OC_User::getUser(); - $data = $this->getCacheEntry($storage, $internalPath, $directory); + if (!$directoryInfo) { + $data = $this->getCacheEntry($storage, $internalPath, $directory); + if (!$data instanceof ICacheEntry || !isset($data['fileid'])) { + return []; + } + } else { + $data = $directoryInfo; + } - if (!$data instanceof ICacheEntry || !isset($data['fileid']) || !($data->getPermissions() & Constants::PERMISSION_READ)) { - return []; - } + if (!($data->getPermissions() & Constants::PERMISSION_READ)) { + return []; + } - $folderId = $data['fileid']; - $contents = $cache->getFolderContentsById($folderId); //TODO: mimetype_filter + $folderId = $data->getId(); + $contents = $cache->getFolderContentsById($folderId); //TODO: mimetype_filter $sharingDisabled = \OCP\Util::isSharingDisabledForUser(); diff --git a/tests/lib/Files/Node/FolderTest.php b/tests/lib/Files/Node/FolderTest.php index 05f546874ef57..4cda92b6e838b 100644 --- a/tests/lib/Files/Node/FolderTest.php +++ b/tests/lib/Files/Node/FolderTest.php @@ -78,6 +78,8 @@ public function testGetDirectoryContent() { new FileInfo('/bar/foo/asd', null, 'foo/asd', ['fileid' => 2, 'path' => '/bar/foo/asd', 'name' => 'asd', 'size' => 100, 'mtime' => 50, 'mimetype' => 'text/plain'], null), new FileInfo('/bar/foo/qwerty', null, 'foo/qwerty', ['fileid' => 3, 'path' => '/bar/foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'httpd/unix-directory'], null), ]); + $view->method('getFileInfo') + ->willReturn($this->createMock(FileInfo::class)); $node = new Folder($root, $view, '/bar/foo'); $children = $node->getDirectoryListing();