Skip to content

Commit

Permalink
Refactor into sabre plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
VicDeo authored and gitmate-bot committed May 14, 2018
1 parent 511b49f commit e9c3afe
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 150 deletions.
83 changes: 0 additions & 83 deletions apps/dav/lib/BaseTree.php

This file was deleted.

3 changes: 1 addition & 2 deletions apps/dav/lib/Connector/Sabre/ObjectTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,12 @@
use OCA\DAV\Connector\Sabre\Exception\FileLocked;
use OCA\DAV\Connector\Sabre\Exception\Forbidden;
use OCA\DAV\Connector\Sabre\Exception\InvalidPath;
use OCA\DAV\BaseTree;
use OCP\Files\ForbiddenException;
use OCP\Files\StorageInvalidException;
use OCP\Files\StorageNotAvailableException;
use OCP\Lock\LockedException;

class ObjectTree extends BaseTree {
class ObjectTree extends \Sabre\DAV\Tree {

/**
* @var \OC\Files\View
Expand Down
3 changes: 2 additions & 1 deletion apps/dav/lib/Connector/Sabre/ServerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
namespace OCA\DAV\Connector\Sabre;

use OCA\DAV\DAV\FileCustomPropertiesBackend;
use OCA\DAV\DAV\FileCustomPropertiesPlugin;
use OCA\DAV\Files\BrowserErrorPagePlugin;
use OCP\Files\Mount\IMountManager;
use OCP\IConfig;
Expand Down Expand Up @@ -182,7 +183,7 @@ public function createServer($baseUri,

// custom properties plugin must be the last one
$server->addPlugin(
new \Sabre\DAV\PropertyStorage\Plugin(
new FileCustomPropertiesPlugin(
new FileCustomPropertiesBackend(
$objectTree,
$this->databaseConnection,
Expand Down
41 changes: 37 additions & 4 deletions apps/dav/lib/DAV/FileCustomPropertiesBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Doctrine\DBAL\Platforms\SqlitePlatform;
use OCA\DAV\Connector\Sabre\Directory;
use OCA\DAV\Connector\Sabre\Node;
use OC\Cache\CappedMemoryCache;
use Sabre\DAV\INode;

/**
Expand All @@ -40,6 +41,11 @@
*/
class FileCustomPropertiesBackend extends AbstractCustomPropertiesBackend {

/**
* @var CappedMemoryCache
*/
protected $deletedItemsCache;

const SELECT_BY_ID_STMT = 'SELECT * FROM `*PREFIX*properties` WHERE `fileid` = ?';
const INSERT_BY_ID_STMT = 'INSERT INTO `*PREFIX*properties`'
. ' (`fileid`,`propertyname`,`propertyvalue`) VALUES(?,?,?)';
Expand All @@ -56,23 +62,50 @@ class FileCustomPropertiesBackend extends AbstractCustomPropertiesBackend {
*/
private $moveSource = null;

/**
* Store fileId before deletion
*
* @param string $path
*
* @return void
*/
public function beforeDelete($path) {
try {
$node = $this->getNodeForPath($path);
if (!\is_null($node) && $node->getId()) {
if ($this->deletedItemsCache === null) {
$this->deletedItemsCache = new CappedMemoryCache();
}
$this->deletedItemsCache->set($path, $node->getId());
}
} catch (\Exception $e) {
// do nothing, delete will throw the same exception anyway
}
}

/**
* This method is called after a node is deleted.
*
* @param string $path path of node for which to delete properties
*
* @return void
*/
public function delete($path) {
$moveSource = $this->moveSource;
$this->moveSource = null;

if ($moveSource === $path) {
// trying to delete a file that has been moved -> ignoring because the file
// exists in another path
// trying to delete a file that has been moved -> ignoring because
// the file exists in another path
return;
}

if ($this->deletedItemsCache === null) {
return;
}

$fileId = $this->tree->getDeletedItemFileId($path);
if ($fileId !== false) {
$fileId = $this->deletedItemsCache->get($path);
if ($fileId !== null) {
$statement = $this->connection->prepare(self::DELETE_BY_ID_STMT);
$statement->execute([$fileId]);
$this->offsetUnset($fileId);
Expand Down
62 changes: 62 additions & 0 deletions apps/dav/lib/DAV/FileCustomPropertiesPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* @author Viktar Dubiniuk <[email protected]>
*
* @copyright Copyright (c) 2018, ownCloud GmbH
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* 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, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace OCA\DAV\DAV;

use Sabre\DAV\PropertyStorage\Plugin;
use Sabre\DAV\Server;

/**
* Class FileCustomPropertiesPlugin
*
* Provides ability to store/retrieve fileId and path before deleting
* the node from tree otherwise it will not be possible to resolve the path by
* fileId in the backend and delete properties for this fileId
*
* @package OCA\DAV\DAV
*/
class FileCustomPropertiesPlugin extends Plugin {

/**
* @param Server $server
*
* @return void
*/
function initialize(Server $server) {
$server->on('beforeUnbind', [$this, 'beforeUnbind'], 90);
parent::initialize($server);
}

/**
* Store fileId before deletion
*
* @param string $path
*
* @return void
*/
function beforeUnbind($path) {
$pathFilter = $this->pathFilter;
if ($pathFilter && !$pathFilter($path)) {
return;
}
$this->backend->beforeDelete($path);
}
}
3 changes: 2 additions & 1 deletion apps/dav/lib/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
use OCA\DAV\Connector\Sabre\TagsPlugin;
use OCA\DAV\Connector\Sabre\ValidateRequestPlugin;
use OCA\DAV\DAV\FileCustomPropertiesBackend;
use OCA\DAV\DAV\FileCustomPropertiesPlugin;
use OCA\DAV\DAV\MiscCustomPropertiesBackend;
use OCA\DAV\DAV\PublicAuth;
use OCA\DAV\Files\BrowserErrorPagePlugin;
Expand Down Expand Up @@ -214,7 +215,7 @@ public function __construct(IRequest $request, $baseUri) {
)
);

$filePropertiesPlugin = new \Sabre\DAV\PropertyStorage\Plugin(
$filePropertiesPlugin = new FileCustomPropertiesPlugin(
new FileCustomPropertiesBackend(
$this->server->tree,
\OC::$server->getDatabaseConnection(),
Expand Down
14 changes: 1 addition & 13 deletions apps/dav/lib/Tree.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,11 @@
* Provides a shortcut when accessing the "files/" subtree to avoid
* having to walk through every node and trigger unnecessary extra queries.
*/
class Tree extends BaseTree {

/**
* @param string $path
*
* @return void
*/
public function delete($path) {
$this->beforeDelete($path);
parent::delete($path);
}

class Tree extends \Sabre\DAV\Tree {
/**
* Returns the INode object for the requested path
*
* @param string $path
*
* @return \Sabre\DAV\INode
* @throws NotFound
*/
Expand Down
Loading

0 comments on commit e9c3afe

Please sign in to comment.