Commit 3ee5d6f3 authored by Vincent Petry's avatar Vincent Petry Committed by Thomas Müller
Browse files

Add more files plugins to new DAV endpoint (#26186)

* Add more files plugins to new DAV endpoint

Also fix report plugin to properly retrieve the path from the
prolongated URL

* In case the report is not for this plugin -> simply return to allow other plugins to get executed

* Adjust onReport tests to match new behavior
parent 58277310
......@@ -169,7 +169,7 @@ class FilesReportPlugin extends ServerPlugin {
public function onReport($reportName, $report, $uri) {
$reportTargetNode = $this->server->tree->getNodeForPath($uri);
if (!$reportTargetNode instanceof Directory || $reportName !== self::REPORT_NAME) {
throw new ReportNotSupported();
return;
}
$ns = '{' . $this::NS_OWNCLOUD . '}';
......@@ -204,7 +204,8 @@ class FilesReportPlugin extends ServerPlugin {
// find sabre nodes by file id, restricted to the root node path
$results = $this->findNodesByFileIds($reportTargetNode, $resultFileIds);
$responses = $this->prepareResponses($requestedProps, $results);
$filesUri = $this->getFilesBaseUri($uri, $reportTargetNode->getPath());
$responses = $this->prepareResponses($filesUri, $requestedProps, $results);
$xml = $this->server->xml->write(
'{DAV:}multistatus',
......@@ -218,6 +219,31 @@ class FilesReportPlugin extends ServerPlugin {
return false;
}
/**
* Returns the base uri of the files root by removing
* the subpath from the URI
*
* @param string $uri URI from this request
* @param string $subPath subpath to remove from the URI
*
* @return string files base uri
*/
private function getFilesBaseUri($uri, $subPath) {
$uri = trim($uri, '/');
$subPath = trim($subPath, '/');
$filesUri = '';
if (empty($subPath)) {
$filesUri = $uri;
} else {
$filesUri = substr($uri, 0, strlen($uri) - strlen($subPath));
}
$filesUri = trim($filesUri, '/');
if (empty($filesUri)) {
return '';
}
return '/' . $filesUri;
}
/**
* Find file ids matching the given filter rules
*
......@@ -305,14 +331,16 @@ class FilesReportPlugin extends ServerPlugin {
/**
* Prepare propfind response for the given nodes
*
* @param string $filesUri $filesUri URI leading to root of the files URI,
* with a leading slash but no trailing slash
* @param string[] $requestedProps requested properties
* @param Node[] nodes nodes for which to fetch and prepare responses
* @return Response[]
*/
public function prepareResponses($requestedProps, $nodes) {
public function prepareResponses($filesUri, $requestedProps, $nodes) {
$responses = [];
foreach ($nodes as $node) {
$propFind = new PropFind($node->getPath(), $requestedProps);
$propFind = new PropFind($filesUri . $node->getPath(), $requestedProps);
$this->server->getPropertiesByNode($propFind, $node);
// copied from Sabre Server's getPropertiesForPath
......@@ -325,7 +353,7 @@ class FilesReportPlugin extends ServerPlugin {
}
$responses[] = new Response(
rtrim($this->server->getBaseUri(), '/') . $node->getPath(),
rtrim($this->server->getBaseUri(), '/') . $filesUri . $node->getPath(),
$result,
200
);
......
......@@ -152,7 +152,7 @@ class SharesPlugin extends \Sabre\DAV\ServerPlugin {
&& $propFind->getDepth() !== 0
&& !is_null($propFind->getStatus(self::SHARETYPES_PROPERTYNAME))
) {
$folderNode = $this->userFolder->get($propFind->getPath());
$folderNode = $this->userFolder->get($sabreNode->getPath());
$children = $folderNode->getDirectoryListing();
$this->cachedShareTypes[$folderNode->getId()] = $this->getShareTypes($folderNode);
......
......@@ -155,7 +155,8 @@ class Server {
// wait with registering these until auth is handled and the filesystem is setup
$this->server->on('beforeMethod', function () {
// custom properties plugin must be the last one
$user = \OC::$server->getUserSession()->getUser();
$userSession = \OC::$server->getUserSession();
$user = $userSession->getUser();
if (!is_null($user)) {
$view = \OC\Files\Filesystem::getView();
$this->server->addPlugin(
......@@ -185,7 +186,30 @@ class Server {
$this->server->tree, \OC::$server->getTagManager()
)
);
// TODO: switch to LazyUserFolder
$userFolder = \OC::$server->getUserFolder();
$this->server->addPlugin(new \OCA\DAV\Connector\Sabre\SharesPlugin(
$this->server->tree,
$userSession,
$userFolder,
\OC::$server->getShareManager()
));
$this->server->addPlugin(new \OCA\DAV\Connector\Sabre\CommentPropertiesPlugin(
\OC::$server->getCommentsManager(),
$userSession
));
$this->server->addPlugin(new \OCA\DAV\Connector\Sabre\FilesReportPlugin(
$this->server->tree,
$view,
\OC::$server->getSystemTagManager(),
\OC::$server->getSystemTagObjectMapper(),
\OC::$server->getTagManager(),
$userSession,
\OC::$server->getGroupManager(),
$userFolder
));
}
$this->server->addPlugin(new \OCA\DAV\Connector\Sabre\CopyEtagHeaderPlugin());
});
}
......
......@@ -74,9 +74,13 @@ class FilesReportPluginTest extends \Test\TestCase {
$this->server = $this->getMockBuilder('\Sabre\DAV\Server')
->setConstructorArgs([$this->tree])
->setMethods(['getRequestUri'])
->setMethods(['getRequestUri', 'getBaseUri'])
->getMock();
$this->server->expects($this->any())
->method('getBaseUri')
->will($this->returnValue('http://example.com/owncloud/remote.php/dav'));
$this->groupManager = $this->getMockBuilder('\OCP\IGroupManager')
->disableOriginalConstructor()
->getMock();
......@@ -115,9 +119,6 @@ class FilesReportPluginTest extends \Test\TestCase {
);
}
/**
* @expectedException \Sabre\DAV\Exception\ReportNotSupported
*/
public function testOnReportInvalidNode() {
$path = 'totally/unrelated/13';
......@@ -131,12 +132,9 @@ class FilesReportPluginTest extends \Test\TestCase {
->will($this->returnValue($path));
$this->plugin->initialize($this->server);
$this->plugin->onReport(FilesReportPluginImplementation::REPORT_NAME, [], '/' . $path);
$this->assertNull($this->plugin->onReport(FilesReportPluginImplementation::REPORT_NAME, [], '/' . $path));
}
/**
* @expectedException \Sabre\DAV\Exception\ReportNotSupported
*/
public function testOnReportInvalidReportName() {
$path = 'test';
......@@ -150,7 +148,7 @@ class FilesReportPluginTest extends \Test\TestCase {
->will($this->returnValue($path));
$this->plugin->initialize($this->server);
$this->plugin->onReport('{whoever}whatever', [], '/' . $path);
$this->assertNull($this->plugin->onReport('{whoever}whatever', [], '/' . $path));
}
public function testOnReport() {
......@@ -232,7 +230,7 @@ class FilesReportPluginTest extends \Test\TestCase {
$this->server->httpResponse = $response;
$this->plugin->initialize($this->server);
$this->plugin->onReport(FilesReportPluginImplementation::REPORT_NAME, $parameters, '/' . $path);
$this->assertFalse($this->plugin->onReport(FilesReportPluginImplementation::REPORT_NAME, $parameters, '/' . $path));
}
public function testFindNodesByFileIdsRoot() {
......@@ -340,12 +338,18 @@ class FilesReportPluginTest extends \Test\TestCase {
$node1->expects($this->once())
->method('getInternalFileId')
->will($this->returnValue('111'));
$node1->expects($this->any())
->method('getPath')
->will($this->returnValue('/node1'));
$node2->expects($this->once())
->method('getInternalFileId')
->will($this->returnValue('222'));
$node2->expects($this->once())
->method('getSize')
->will($this->returnValue(1024));
$node2->expects($this->any())
->method('getPath')
->will($this->returnValue('/sub/node2'));
$config = $this->createMock('\OCP\IConfig');
......@@ -358,13 +362,16 @@ class FilesReportPluginTest extends \Test\TestCase {
)
);
$this->plugin->initialize($this->server);
$responses = $this->plugin->prepareResponses($requestedProps, [$node1, $node2]);
$responses = $this->plugin->prepareResponses('/files/username', $requestedProps, [$node1, $node2]);
$this->assertCount(2, $responses);
$this->assertEquals(200, $responses[0]->getHttpStatus());
$this->assertEquals(200, $responses[1]->getHttpStatus());
$this->assertEquals('http://example.com/owncloud/remote.php/dav/files/username/node1', $responses[0]->getHref());
$this->assertEquals('http://example.com/owncloud/remote.php/dav/files/username/sub/node2', $responses[1]->getHref());
$props1 = $responses[0]->getResponseProperties();
$this->assertEquals('111', $props1[200]['{http://owncloud.org/ns}fileid']);
$this->assertNull($props1[404]['{DAV:}getcontentlength']);
......@@ -632,4 +639,21 @@ class FilesReportPluginTest extends \Test\TestCase {
$this->assertEquals(['456', '789'], array_values($this->invokePrivate($this->plugin, 'processFilterRules', [$rules])));
}
public function filesBaseUriProvider() {
return [
['', '', ''],
['files/username', '', '/files/username'],
['files/username/test', '/test', '/files/username'],
['files/username/test/sub', '/test/sub', '/files/username'],
['test', '/test', ''],
];
}
/**
* @dataProvider filesBaseUriProvider
*/
public function testFilesBaseUri($uri, $reportPath, $expectedUri) {
$this->assertEquals($expectedUri, $this->invokePrivate($this->plugin, 'getFilesBaseUri', [$uri, $reportPath]));
}
}
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