Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions apps/dav/appinfo/v1/publicwebdav.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@
$requestUri = \OC::$server->getRequest()->getRequestUri();

$linkCheckPlugin = new \OCA\DAV\Files\Sharing\PublicLinkCheckPlugin();
$filesDropPlugin = new \OCA\DAV\Files\Sharing\FilesDropPlugin();

$server = $serverFactory->createServer($baseuri, $requestUri, $authBackend, function (\Sabre\DAV\Server $server) use ($authBackend, $linkCheckPlugin) {
$server = $serverFactory->createServer($baseuri, $requestUri, $authBackend, function (\Sabre\DAV\Server $server) use ($authBackend, $linkCheckPlugin, $filesDropPlugin) {
$isAjax = (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest');
$federatedSharingApp = new \OCA\FederatedFileSharing\AppInfo\Application();
$federatedShareProvider = $federatedSharingApp->getFederatedShareProvider();
Expand All @@ -72,9 +73,10 @@
$isReadable = $share->getPermissions() & \OCP\Constants::PERMISSION_READ;
$fileId = $share->getNodeId();

/*
if (!$isReadable) {
return false;
}
}*/

\OC\Files\Filesystem::addStorageWrapper('sharePermissions', function ($mountPoint, $storage) use ($share) {
return new \OC\Files\Storage\Wrapper\PermissionsMask(array('storage' => $storage, 'mask' => $share->getPermissions() | \OCP\Constants::PERMISSION_SHARE));
Expand All @@ -86,10 +88,19 @@
$fileInfo = $ownerView->getFileInfo($path);
$linkCheckPlugin->setFileInfo($fileInfo);

return new \OC\Files\View($ownerView->getAbsolutePath($path));
// If not readble (files_drop) enable the filesdrop plugin
if (!$isReadable) {
$filesDropPlugin->enable();
}

$view = new \OC\Files\View($ownerView->getAbsolutePath($path));
$filesDropPlugin->setView($view);

return $view;
});

$server->addPlugin($linkCheckPlugin);
$server->addPlugin($filesDropPlugin);

// And off we go!
$server->exec();
51 changes: 51 additions & 0 deletions apps/dav/lib/Connector/Sabre/FilesPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
use OCP\Files\StorageNotAvailableException;
use OCP\IConfig;
use OCP\IRequest;
use Sabre\DAV\Exception\BadRequest;
use OCA\DAV\Connector\Sabre\Directory;

class FilesPlugin extends ServerPlugin {

Expand Down Expand Up @@ -170,6 +172,8 @@ public function initialize(\Sabre\DAV\Server $server) {
$this->server = $server;
$this->server->on('propFind', array($this, 'handleGetProperties'));
$this->server->on('propPatch', array($this, 'handleUpdateProperties'));
// RFC5995 to add file to the collection with a suggested name
$this->server->on('method:POST', [$this, 'httpPost']);
$this->server->on('afterBind', array($this, 'sendFileIdHeader'));
$this->server->on('afterWriteContent', array($this, 'sendFileIdHeader'));
$this->server->on('afterMethod:GET', [$this,'httpGet']);
Expand Down Expand Up @@ -432,4 +436,51 @@ public function sendFileIdHeader($filePath, \Sabre\DAV\INode $node = null) {
}
}

/**
* POST operation on directories to create a new file
* with suggested name
*
* @param RequestInterface $request request object
* @param ResponseInterface $response response object
* @return null|false
*/
public function httpPost(RequestInterface $request, ResponseInterface $response) {
// TODO: move this to another plugin ?
if (!\OC::$CLI && !\OC::$server->getRequest()->passesCSRFCheck()) {
throw new BadRequest('Invalid CSRF token');
}

list($parentPath, $name) = \Sabre\HTTP\URLUtil::splitPath($request->getPath());

// Making sure the parent node exists and is a directory
$node = $this->tree->getNodeForPath($parentPath);

if ($node instanceof Directory) {
// no Add-Member found
if (empty($name) || $name[0] !== '&') {
// suggested name required
throw new BadRequest('Missing suggested file name');
}

$name = substr($name, 1);

if (empty($name)) {
// suggested name required
throw new BadRequest('Missing suggested file name');
}

// make sure the name is unique
$name = basename(\OC_Helper::buildNotExistingFileNameForView($parentPath, $name, $this->fileView));

$node->createFile($name, $request->getBodyAsStream());

list($parentUrl, ) = \Sabre\HTTP\URLUtil::splitPath($request->getUrl());

$response->setHeader('Content-Location', $parentUrl . '/' . rawurlencode($name));

// created
$response->setStatus(201);
return false;
}
}
}
94 changes: 94 additions & 0 deletions apps/dav/lib/Files/Sharing/FilesDropPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
/**
* @copyright Copyright (c) 2016, Roeland Jago Douma <[email protected]>
*
* @author Roeland Jago Douma <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\DAV\Files\Sharing;

use OC\Files\View;
use Sabre\DAV\ServerPlugin;
use Sabre\HTTP\RequestInterface;
use Sabre\HTTP\ResponseInterface;

/**
* Make sure that the destination is writable
*/
class FilesDropPlugin extends ServerPlugin {

/** @var View */
private $view;

/** @var bool */
private $enabled = false;

/**
* @param View $view
*/
public function setView($view) {
$this->view = $view;
}

public function enable() {
$this->enabled = true;
}


/**
* This initializes the plugin.
*
* @param \Sabre\DAV\Server $server Sabre server
*
* @return void
*/
public function initialize(\Sabre\DAV\Server $server) {
$server->on('beforeMethod:PUT', [$this, 'beforeMethod']);
$this->enabled = false;
}

public function beforeMethod(RequestInterface $request, ResponseInterface $response){

if (!$this->enabled) {
return;
}

$path = $request->getPath();

if ($this->view->file_exists($path)) {
$newName = \OC_Helper::buildNotExistingFileNameForView('/', $path, $this->view);

$url = $request->getBaseUrl() . $newName . '?';
$parms = $request->getQueryParameters();
$first = true;
foreach ($parms as $k => $v) {
if ($first) {
$url .= '?';
$first = false;
} else {
$url .= '&';
}
$url .= $k . '=' . $v;
}

$request->setUrl($url);
}


}
}
85 changes: 83 additions & 2 deletions apps/dav/tests/unit/Connector/Sabre/FilesPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function setUp() {
* @param string $class
* @return \PHPUnit_Framework_MockObject_MockObject
*/
private function createTestNode($class) {
private function createTestNode($class, $path = '/dummypath') {
$node = $this->getMockBuilder($class)
->disableOriginalConstructor()
->getMock();
Expand All @@ -134,7 +134,7 @@ private function createTestNode($class) {

$this->tree->expects($this->any())
->method('getNodeForPath')
->with('/dummypath')
->with($path)
->will($this->returnValue($node));

$node->expects($this->any())
Expand Down Expand Up @@ -547,4 +547,85 @@ public function testHasPreview() {

$this->assertEquals("false", $propFind->get(self::HAS_PREVIEW_PROPERTYNAME));
}

public function postCreateFileProvider() {
$baseUrl = 'http://example.com/owncloud/remote.php/webdav/subdir/';
return [
['test.txt', 'some file.txt', 'some file.txt', $baseUrl . 'some%20file.txt'],
['some file.txt', 'some file.txt', 'some file (2).txt', $baseUrl . 'some%20file%20%282%29.txt'],
];
}

/**
* @dataProvider postCreateFileProvider
*/
public function testPostWithAddMember($existingFile, $wantedName, $deduplicatedName, $expectedLocation) {
$request = $this->getMock('Sabre\HTTP\RequestInterface');
$response = $this->getMock('Sabre\HTTP\ResponseInterface');

$request->expects($this->any())
->method('getUrl')
->will($this->returnValue('http://example.com/owncloud/remote.php/webdav/subdir/&' . $wantedName));

$request->expects($this->any())
->method('getPath')
->will($this->returnValue('/subdir/&' . $wantedName));

$request->expects($this->once())
->method('getBodyAsStream')
->will($this->returnValue(fopen('data://text/plain,hello', 'r')));

$this->view->expects($this->any())
->method('file_exists')
->will($this->returnCallback(function($path) use ($existingFile) {
return ($path === '/subdir/' . $existingFile);
}));

$node = $this->createTestNode('\OCA\DAV\Connector\Sabre\Directory', '/subdir');

$node->expects($this->once())
->method('createFile')
->with($deduplicatedName, $this->isType('resource'));

$response->expects($this->once())
->method('setStatus')
->with(201);
$response->expects($this->once())
->method('setHeader')
->with('Content-Location', $expectedLocation);

$this->assertFalse($this->plugin->httpPost($request, $response));
}

public function testPostOnNonDirectory() {
$request = $this->getMock('Sabre\HTTP\RequestInterface');
$response = $this->getMock('Sabre\HTTP\ResponseInterface');

$request->expects($this->any())
->method('getPath')
->will($this->returnValue('/subdir/test.txt/&abc'));

$this->createTestNode('\OCA\DAV\Connector\Sabre\File', '/subdir/test.txt');

$this->assertNull($this->plugin->httpPost($request, $response));
}

/**
* @expectedException \Sabre\DAV\Exception\BadRequest
*/
public function testPostWithoutAddMember() {
$request = $this->getMock('Sabre\HTTP\RequestInterface');
$response = $this->getMock('Sabre\HTTP\ResponseInterface');

$request->expects($this->any())
->method('getPath')
->will($this->returnValue('/subdir/&'));

$node = $this->createTestNode('\OCA\DAV\Connector\Sabre\Directory', '/subdir');

$node->expects($this->never())
->method('createFile');

$this->plugin->httpPost($request, $response);
}
}
Loading