Commit 26d768b8 authored by Piotr Mrówczyński's avatar Piotr Mrówczyński Committed by Thomas Müller

Optimize any request by eliminating redundant query for rootInfo. (#27372)

Call only getChildren to avoid excessive calls to DB
parent da389acc
......@@ -110,9 +110,9 @@ class ServerFactory {
// Some WebDAV clients do require Class 2 WebDAV support (locking), since
// we do not provide locking we emulate it using a fake locking plugin.
if($this->request->isUserAgent([
'/WebDAVFS/',
'/Microsoft Office OneNote 2013/',
'/Microsoft-WebDAV-MiniRedir/',
'/WebDAVFS/',
'/Microsoft Office OneNote 2013/',
'/Microsoft-WebDAV-MiniRedir/',
])) {
$server->addPlugin(new \OCA\DAV\Connector\Sabre\FakeLockerPlugin());
}
......@@ -124,18 +124,26 @@ class ServerFactory {
// wait with registering these until auth is handled and the filesystem is setup
$server->on('beforeMethod', function () use ($server, $objectTree, $viewCallBack) {
// ensure the skeleton is copied
// Try to obtain User Folder
$userFolder = \OC::$server->getUserFolder();
/** @var \OC\Files\View $view */
$view = $viewCallBack($server);
$rootInfo = $view->getFileInfo('');
if (!is_null($userFolder)) {
// User folder exists and user is active and not anonymous
$rootInfo = $userFolder->getFileInfo();
} else {
// User is anonymous or inactive, we need to get root info
$rootInfo = $view->getFileInfo('');
}
// Create ownCloud Dir
// Create ownCloud Root
if ($rootInfo->getType() === 'dir') {
$root = new \OCA\DAV\Connector\Sabre\Directory($view, $rootInfo, $objectTree);
} else {
$root = new \OCA\DAV\Connector\Sabre\File($view, $rootInfo);
}
$objectTree->init($root, $view, $this->mountManager);
$server->addPlugin(
......@@ -154,7 +162,6 @@ class ServerFactory {
$server->addPlugin(new \OCA\DAV\Connector\Sabre\SharesPlugin(
$objectTree,
$this->userSession,
$userFolder,
\OC::$server->getShareManager()
));
$server->addPlugin(new \OCA\DAV\Connector\Sabre\CommentPropertiesPlugin(\OC::$server->getCommentsManager(), $this->userSession));
......@@ -183,4 +190,4 @@ class ServerFactory {
}, 30); // priority 30: after auth (10) and acl(20), before lock(50) and handling the request
return $server;
}
}
}
\ No newline at end of file
......@@ -74,12 +74,10 @@ class SharesPlugin extends \Sabre\DAV\ServerPlugin {
public function __construct(
\Sabre\DAV\Tree $tree,
IUserSession $userSession,
\OCP\Files\Folder $userFolder,
\OCP\Share\IManager $shareManager
) {
$this->tree = $tree;
$this->shareManager = $shareManager;
$this->userFolder = $userFolder;
$this->userId = $userSession->getUser()->getUID();
$this->cachedShareTypes = [];
}
......@@ -168,11 +166,10 @@ class SharesPlugin extends \Sabre\DAV\ServerPlugin {
&& $propFind->getDepth() !== 0
&& !is_null($propFind->getStatus(self::SHARETYPES_PROPERTYNAME))
) {
$folderNode = $this->userFolder->get($sabreNode->getPath());
$children = $folderNode->getDirectoryListing();
$children = $sabreNode->getChildren();
// Get ID of parent folder
$folderNodeID = intval($folderNode->getId());
$folderNodeID = $sabreNode->getId();
$nodeIdsArray = [$folderNodeID];
// Initialize share types array for this node in case there would be no shares for this node
......@@ -181,13 +178,13 @@ class SharesPlugin extends \Sabre\DAV\ServerPlugin {
// Get IDs for all children of the parent folder
foreach ($children as $childNode) {
// Ensure that they are of File or Folder type
if (!($childNode instanceof \OCP\Files\File) &&
!($childNode instanceof \OCP\Files\Folder)) {
if (!($childNode instanceof \OCA\DAV\Connector\Sabre\Directory) &&
!($childNode instanceof \OCA\DAV\Connector\Sabre\File)) {
return;
}
// Put node ID into an array and initialize cache for it
$nodeId = intval($childNode->getId());
$nodeId = $childNode->getId();
array_push($nodeIdsArray, $nodeId);
// Initialize share types array for this node in case there would be no shares for this node
......@@ -207,7 +204,7 @@ class SharesPlugin extends \Sabre\DAV\ServerPlugin {
$shareTypesHash = $this->cachedShareTypes[$sabreNode->getId()];
} else {
// Share types for this node not in cache, obtain if any
$nodeId = $this->userFolder->get($sabreNode->getPath())->getId();
$nodeId = $sabreNode->getId();
$returnedShares = $this->getSharesForNodeIds([$nodeId]);
// Initialize share types for this node and obtain share types hash if any
......
......@@ -188,7 +188,6 @@ class Server {
$this->server->addPlugin(new SharesPlugin(
$this->server->tree,
$userSession,
$userFolder,
\OC::$server->getShareManager()
));
$this->server->addPlugin(new CommentPropertiesPlugin(
......
......@@ -69,12 +69,9 @@ class SharesPluginTest extends \Test\TestCase {
->method('getUser')
->will($this->returnValue($user));
$this->userFolder = $this->createMock('\OCP\Files\Folder');
$this->plugin = new \OCA\DAV\Connector\Sabre\SharesPlugin(
$this->tree,
$userSession,
$this->userFolder,
$this->shareManager
);
$this->plugin->initialize($this->server);
......@@ -90,20 +87,6 @@ class SharesPluginTest extends \Test\TestCase {
$sabreNode->expects($this->any())
->method('getId')
->will($this->returnValue(123));
$sabreNode->expects($this->once())
->method('getPath')
->will($this->returnValue('/subdir'));
// node API nodes
$node = $this->createMock('\OCP\Files\Folder');
$node->expects($this->any())
->method('getId')
->will($this->returnValue(123));
$this->userFolder->expects($this->once())
->method('get')
->with('/subdir')
->will($this->returnValue($node));
$requestedShareTypes = [
\OCP\Share::SHARE_TYPE_USER,
......@@ -179,34 +162,13 @@ class SharesPluginTest extends \Test\TestCase {
->method('getId')
->will($this->returnValue(123));
// never, because we use getDirectoryListing from the Node API instead
$sabreNode->expects($this->never())
->method('getChildren');
$sabreNode->expects($this->once())
->method('getChildren')
->will($this->returnValue([$sabreNode1,$sabreNode2]));
$sabreNode->expects($this->any())
->method('getPath')
->will($this->returnValue('/subdir'));
// node API nodes
$node = $this->createMock('\OCP\Files\Folder');
$node->expects($this->any())
->method('getId')
->will($this->returnValue(123));
$node1 = $this->createMock('\OCP\Files\File');
$node1->expects($this->any())
->method('getId')
->will($this->returnValue(111));
$node2 = $this->createMock('\OCP\Files\File');
$node2->expects($this->any())
->method('getId')
->will($this->returnValue(222));
$node->expects($this->once())
->method('getDirectoryListing')
->will($this->returnValue([$node1, $node2]));
$this->userFolder->expects($this->once())
->method('get')
->with('/subdir')
->will($this->returnValue($node));
$requestedShareTypes = [
\OCP\Share::SHARE_TYPE_USER,
\OCP\Share::SHARE_TYPE_GROUP,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment