summaryrefslogtreecommitdiffstats
path: root/apps/files_trashbin
diff options
context:
space:
mode:
authorRoeland Jago Douma <rullzer@users.noreply.github.com>2018-05-01 14:24:38 +0200
committerGitHub <noreply@github.com>2018-05-01 14:24:38 +0200
commit7f194be3ab4ad0ee8edf095bf6d79a17f116f782 (patch)
tree02e9ab8ad1e1ab6bcc0e724c3ee94ab3c27172e6 /apps/files_trashbin
parentc8cb42fd2671c8b6ed3257e62fa9f6a24474acb8 (diff)
parentc625fc5931c904e5d6bafe855429eebeae23467f (diff)
downloadnextcloud-server-7f194be3ab4ad0ee8edf095bf6d79a17f116f782.tar.gz
nextcloud-server-7f194be3ab4ad0ee8edf095bf6d79a17f116f782.zip
Merge pull request #9202 from nextcloud/feature/1332/trashbin_dav
Trashbin endpoint in DAV
Diffstat (limited to 'apps/files_trashbin')
-rw-r--r--apps/files_trashbin/appinfo/info.xml12
-rw-r--r--apps/files_trashbin/composer/composer/autoload_classmap.php10
-rw-r--r--apps/files_trashbin/composer/composer/autoload_static.php10
-rw-r--r--apps/files_trashbin/lib/AppInfo/Application.php13
-rw-r--r--apps/files_trashbin/lib/Sabre/ITrash.php32
-rw-r--r--apps/files_trashbin/lib/Sabre/PropfindPlugin.php64
-rw-r--r--apps/files_trashbin/lib/Sabre/RestoreFolder.php86
-rw-r--r--apps/files_trashbin/lib/Sabre/RootCollection.php59
-rw-r--r--apps/files_trashbin/lib/Sabre/TrashFile.php91
-rw-r--r--apps/files_trashbin/lib/Sabre/TrashFolder.php120
-rw-r--r--apps/files_trashbin/lib/Sabre/TrashFolderFile.php102
-rw-r--r--apps/files_trashbin/lib/Sabre/TrashFolderFolder.php133
-rw-r--r--apps/files_trashbin/lib/Sabre/TrashHome.php89
-rw-r--r--apps/files_trashbin/lib/Sabre/TrashRoot.php103
14 files changed, 923 insertions, 1 deletions
diff --git a/apps/files_trashbin/appinfo/info.xml b/apps/files_trashbin/appinfo/info.xml
index 585e61fe1e8..4ca60c0e269 100644
--- a/apps/files_trashbin/appinfo/info.xml
+++ b/apps/files_trashbin/appinfo/info.xml
@@ -9,13 +9,14 @@ This application enables users to restore files that were deleted from the syste
To prevent a user from running out of disk space, the Deleted files app will not utilize more than 50% of the currently available free quota for deleted files. If the deleted files exceed this limit, the app deletes the oldest files until it gets below this limit. More information is available in the Deleted Files documentation.
</description>
- <version>1.4.0</version>
+ <version>1.4.1</version>
<licence>agpl</licence>
<author>Bjoern Schiessle</author>
<namespace>Files_Trashbin</namespace>
<default_enable/>
<types>
<filesystem/>
+ <dav/>
</types>
<documentation>
<user>user-trashbin</user>
@@ -34,4 +35,13 @@ To prevent a user from running out of disk space, the Deleted files app will not
<command>OCA\Files_Trashbin\Command\CleanUp</command>
<command>OCA\Files_Trashbin\Command\ExpireTrash</command>
</commands>
+
+ <sabre>
+ <collections>
+ <collection>OCA\Files_Trashbin\Sabre\RootCollection</collection>
+ </collections>
+ <plugins>
+ <plugin>OCA\Files_Trashbin\Sabre\PropfindPlugin</plugin>
+ </plugins>
+ </sabre>
</info>
diff --git a/apps/files_trashbin/composer/composer/autoload_classmap.php b/apps/files_trashbin/composer/composer/autoload_classmap.php
index 2e58c7b1c6c..3713de530c6 100644
--- a/apps/files_trashbin/composer/composer/autoload_classmap.php
+++ b/apps/files_trashbin/composer/composer/autoload_classmap.php
@@ -18,6 +18,16 @@ return array(
'OCA\\Files_Trashbin\\Expiration' => $baseDir . '/../lib/Expiration.php',
'OCA\\Files_Trashbin\\Helper' => $baseDir . '/../lib/Helper.php',
'OCA\\Files_Trashbin\\Hooks' => $baseDir . '/../lib/Hooks.php',
+ 'OCA\\Files_Trashbin\\Sabre\\ITrash' => $baseDir . '/../lib/Sabre/ITrash.php',
+ 'OCA\\Files_Trashbin\\Sabre\\PropfindPlugin' => $baseDir . '/../lib/Sabre/PropfindPlugin.php',
+ 'OCA\\Files_Trashbin\\Sabre\\RestoreFolder' => $baseDir . '/../lib/Sabre/RestoreFolder.php',
+ 'OCA\\Files_Trashbin\\Sabre\\RootCollection' => $baseDir . '/../lib/Sabre/RootCollection.php',
+ 'OCA\\Files_Trashbin\\Sabre\\TrashFile' => $baseDir . '/../lib/Sabre/TrashFile.php',
+ 'OCA\\Files_Trashbin\\Sabre\\TrashFolder' => $baseDir . '/../lib/Sabre/TrashFolder.php',
+ 'OCA\\Files_Trashbin\\Sabre\\TrashFolderFile' => $baseDir . '/../lib/Sabre/TrashFolderFile.php',
+ 'OCA\\Files_Trashbin\\Sabre\\TrashFolderFolder' => $baseDir . '/../lib/Sabre/TrashFolderFolder.php',
+ 'OCA\\Files_Trashbin\\Sabre\\TrashHome' => $baseDir . '/../lib/Sabre/TrashHome.php',
+ 'OCA\\Files_Trashbin\\Sabre\\TrashRoot' => $baseDir . '/../lib/Sabre/TrashRoot.php',
'OCA\\Files_Trashbin\\Storage' => $baseDir . '/../lib/Storage.php',
'OCA\\Files_Trashbin\\Trashbin' => $baseDir . '/../lib/Trashbin.php',
);
diff --git a/apps/files_trashbin/composer/composer/autoload_static.php b/apps/files_trashbin/composer/composer/autoload_static.php
index 01520e2d14d..b00778741b3 100644
--- a/apps/files_trashbin/composer/composer/autoload_static.php
+++ b/apps/files_trashbin/composer/composer/autoload_static.php
@@ -33,6 +33,16 @@ class ComposerStaticInitFiles_Trashbin
'OCA\\Files_Trashbin\\Expiration' => __DIR__ . '/..' . '/../lib/Expiration.php',
'OCA\\Files_Trashbin\\Helper' => __DIR__ . '/..' . '/../lib/Helper.php',
'OCA\\Files_Trashbin\\Hooks' => __DIR__ . '/..' . '/../lib/Hooks.php',
+ 'OCA\\Files_Trashbin\\Sabre\\ITrash' => __DIR__ . '/..' . '/../lib/Sabre/ITrash.php',
+ 'OCA\\Files_Trashbin\\Sabre\\PropfindPlugin' => __DIR__ . '/..' . '/../lib/Sabre/PropfindPlugin.php',
+ 'OCA\\Files_Trashbin\\Sabre\\RestoreFolder' => __DIR__ . '/..' . '/../lib/Sabre/RestoreFolder.php',
+ 'OCA\\Files_Trashbin\\Sabre\\RootCollection' => __DIR__ . '/..' . '/../lib/Sabre/RootCollection.php',
+ 'OCA\\Files_Trashbin\\Sabre\\TrashFile' => __DIR__ . '/..' . '/../lib/Sabre/TrashFile.php',
+ 'OCA\\Files_Trashbin\\Sabre\\TrashFolder' => __DIR__ . '/..' . '/../lib/Sabre/TrashFolder.php',
+ 'OCA\\Files_Trashbin\\Sabre\\TrashFolderFile' => __DIR__ . '/..' . '/../lib/Sabre/TrashFolderFile.php',
+ 'OCA\\Files_Trashbin\\Sabre\\TrashFolderFolder' => __DIR__ . '/..' . '/../lib/Sabre/TrashFolderFolder.php',
+ 'OCA\\Files_Trashbin\\Sabre\\TrashHome' => __DIR__ . '/..' . '/../lib/Sabre/TrashHome.php',
+ 'OCA\\Files_Trashbin\\Sabre\\TrashRoot' => __DIR__ . '/..' . '/../lib/Sabre/TrashRoot.php',
'OCA\\Files_Trashbin\\Storage' => __DIR__ . '/..' . '/../lib/Storage.php',
'OCA\\Files_Trashbin\\Trashbin' => __DIR__ . '/..' . '/../lib/Trashbin.php',
);
diff --git a/apps/files_trashbin/lib/AppInfo/Application.php b/apps/files_trashbin/lib/AppInfo/Application.php
index e9d4e6ecc1d..ea27c8c16ea 100644
--- a/apps/files_trashbin/lib/AppInfo/Application.php
+++ b/apps/files_trashbin/lib/AppInfo/Application.php
@@ -23,6 +23,7 @@
namespace OCA\Files_Trashbin\AppInfo;
+use OCA\DAV\Connector\Sabre\Principal;
use OCP\AppFramework\App;
use OCA\Files_Trashbin\Expiration;
use OCP\AppFramework\Utility\ITimeFactory;
@@ -47,5 +48,17 @@ class Application extends App {
$c->query(ITimeFactory::class)
);
});
+
+ /*
+ * Register $principalBackend for the DAV collection
+ */
+ $container->registerService('principalBackend', function () {
+ return new Principal(
+ \OC::$server->getUserManager(),
+ \OC::$server->getGroupManager(),
+ \OC::$server->getShareManager(),
+ \OC::$server->getUserSession()
+ );
+ });
}
}
diff --git a/apps/files_trashbin/lib/Sabre/ITrash.php b/apps/files_trashbin/lib/Sabre/ITrash.php
new file mode 100644
index 00000000000..b0ff2b1570a
--- /dev/null
+++ b/apps/files_trashbin/lib/Sabre/ITrash.php
@@ -0,0 +1,32 @@
+<?php
+declare(strict_types=1);
+/**
+ * @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl>
+ *
+ * @author Roeland Jago Douma <roeland@famdouma.nl>
+ *
+ * @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\Files_Trashbin\Sabre;
+
+interface ITrash {
+ public function restore(): bool;
+
+ public function getFilename(): string;
+
+ public function getOriginalLocation(): string;
+}
diff --git a/apps/files_trashbin/lib/Sabre/PropfindPlugin.php b/apps/files_trashbin/lib/Sabre/PropfindPlugin.php
new file mode 100644
index 00000000000..e50ee08590a
--- /dev/null
+++ b/apps/files_trashbin/lib/Sabre/PropfindPlugin.php
@@ -0,0 +1,64 @@
+<?php
+declare(strict_types=1);
+/**
+ * @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl>
+ *
+ * @author Roeland Jago Douma <roeland@famdouma.nl>
+ *
+ * @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\Files_Trashbin\Sabre;
+
+use Sabre\DAV\INode;
+use Sabre\DAV\PropFind;
+use Sabre\DAV\Server;
+use Sabre\DAV\ServerPlugin;
+
+class PropfindPlugin extends ServerPlugin {
+
+ const TRASHBIN_FILENAME = '{http://nextcloud.org/ns}trashbin-filename';
+ const TRASHBIN_ORIGINAL_LOCATION = '{http://nextcloud.org/ns}trashbin-original-location';
+
+ /** @var Server */
+ private $server;
+
+ public function __construct() {
+ }
+
+ public function initialize(Server $server) {
+ $this->server = $server;
+
+ $this->server->on('propFind', [$this, 'propFind']);
+ }
+
+
+ public function propFind(PropFind $propFind, INode $node) {
+ if (!($node instanceof ITrash)) {
+ return;
+ }
+
+ $propFind->handle(self::TRASHBIN_FILENAME, function() use ($node) {
+ return $node->getFilename();
+ });
+
+ $propFind->handle(self::TRASHBIN_ORIGINAL_LOCATION, function() use ($node) {
+ return $node->getOriginalLocation();
+ });
+ }
+
+}
diff --git a/apps/files_trashbin/lib/Sabre/RestoreFolder.php b/apps/files_trashbin/lib/Sabre/RestoreFolder.php
new file mode 100644
index 00000000000..04f23db0ed4
--- /dev/null
+++ b/apps/files_trashbin/lib/Sabre/RestoreFolder.php
@@ -0,0 +1,86 @@
+<?php
+declare(strict_types=1);
+/**
+ * @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl>
+ *
+ * @author Roeland Jago Douma <roeland@famdouma.nl>
+ *
+ * @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\Files_Trashbin\Sabre;
+
+use Sabre\DAV\Exception\Forbidden;
+use Sabre\DAV\ICollection;
+use Sabre\DAV\IMoveTarget;
+use Sabre\DAV\INode;
+
+
+class RestoreFolder implements ICollection, IMoveTarget {
+
+ /** @var string */
+ protected $userId;
+
+ public function __construct(string $userId) {
+ $this->userId = $userId;
+ }
+
+ public function createFile($name, $data = null) {
+ throw new Forbidden();
+ }
+
+ public function createDirectory($name) {
+ throw new Forbidden();
+ }
+
+ public function getChild($name) {
+ return null;
+ }
+
+ public function delete() {
+ throw new Forbidden();
+ }
+
+ public function getName() {
+ return 'restore';
+ }
+
+ public function setName($name) {
+ throw new Forbidden();
+ }
+
+ public function getLastModified(): int {
+ return 0;
+ }
+
+ public function getChildren(): array {
+ return [];
+ }
+
+ public function childExists($name): bool {
+ return false;
+ }
+
+ public function moveInto($targetName, $sourcePath, INode $sourceNode): bool {
+ if (!($sourceNode instanceof ITrash)) {
+ return false;
+ }
+
+ return $sourceNode->restore();
+ }
+
+}
diff --git a/apps/files_trashbin/lib/Sabre/RootCollection.php b/apps/files_trashbin/lib/Sabre/RootCollection.php
new file mode 100644
index 00000000000..e425fb448e2
--- /dev/null
+++ b/apps/files_trashbin/lib/Sabre/RootCollection.php
@@ -0,0 +1,59 @@
+<?php
+declare(strict_types=1);
+/**
+ * @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl>
+ *
+ * @author Roeland Jago Douma <roeland@famdouma.nl>
+ *
+ * @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\Files_Trashbin\Sabre;
+
+use Sabre\DAV\INode;
+use Sabre\DAVACL\AbstractPrincipalCollection;
+use Sabre\DAVACL\PrincipalBackend;
+
+class RootCollection extends AbstractPrincipalCollection {
+
+ public function __construct(PrincipalBackend\BackendInterface $principalBackend) {
+ parent::__construct($principalBackend, 'principals/users');
+ }
+
+ /**
+ * This method returns a node for a principal.
+ *
+ * The passed array contains principal information, and is guaranteed to
+ * at least contain a uri item. Other properties may or may not be
+ * supplied by the authentication backend.
+ *
+ * @param array $principalInfo
+ * @return INode
+ */
+ public function getChildForPrincipal(array $principalInfo): TrashHome {
+ list(,$name) = \Sabre\Uri\split($principalInfo['uri']);
+ $user = \OC::$server->getUserSession()->getUser();
+ if (is_null($user) || $name !== $user->getUID()) {
+ throw new \Sabre\DAV\Exception\Forbidden();
+ }
+ return new TrashHome($principalInfo);
+ }
+
+ public function getName(): string {
+ return 'trashbin';
+ }
+
+}
diff --git a/apps/files_trashbin/lib/Sabre/TrashFile.php b/apps/files_trashbin/lib/Sabre/TrashFile.php
new file mode 100644
index 00000000000..29e7a955623
--- /dev/null
+++ b/apps/files_trashbin/lib/Sabre/TrashFile.php
@@ -0,0 +1,91 @@
+<?php
+declare(strict_types=1);
+/**
+ * @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl>
+ *
+ * @author Roeland Jago Douma <roeland@famdouma.nl>
+ *
+ * @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\Files_Trashbin\Sabre;
+
+use OCP\Files\FileInfo;
+use Sabre\DAV\Exception\Forbidden;
+use Sabre\DAV\IFile;
+
+class TrashFile implements IFile, ITrash {
+ /** @var string */
+ private $userId;
+
+ /** @var FileInfo */
+ private $data;
+
+ public function __construct(string $userId, FileInfo $data) {
+ $this->userId = $userId;
+ $this->data = $data;
+ }
+
+ public function put($data) {
+ throw new Forbidden();
+ }
+
+ public function get() {
+ return $this->data->getStorage()->fopen($this->data->getInternalPath().'.d'.$this->getLastModified(), 'rb');
+ }
+
+ public function getContentType(): string {
+ return $this->data->getMimetype();
+ }
+
+ public function getETag(): string {
+ return $this->data->getEtag();
+ }
+
+ public function getSize(): int {
+ return $this->data->getSize();
+ }
+
+ public function delete() {
+ \OCA\Files_Trashbin\Trashbin::delete($this->data->getName(), $this->userId, $this->getLastModified());
+ }
+
+ public function getName(): string {
+ return $this->data->getName() . '.d' . $this->getLastModified();
+ }
+
+ public function setName($name) {
+ throw new Forbidden();
+ }
+
+ public function getLastModified(): int {
+ return $this->data->getMtime();
+ }
+
+ public function restore(): bool {
+ return \OCA\Files_Trashbin\Trashbin::restore($this->getName(), $this->data->getName(), $this->getLastModified());
+ }
+
+ public function getFilename(): string {
+ return $this->data->getName();
+ }
+
+ public function getOriginalLocation(): string {
+ return $this->data['extraData'];
+ }
+
+
+}
diff --git a/apps/files_trashbin/lib/Sabre/TrashFolder.php b/apps/files_trashbin/lib/Sabre/TrashFolder.php
new file mode 100644
index 00000000000..33236eea262
--- /dev/null
+++ b/apps/files_trashbin/lib/Sabre/TrashFolder.php
@@ -0,0 +1,120 @@
+<?php
+declare(strict_types=1);
+/**
+ * @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl>
+ *
+ * @author Roeland Jago Douma <roeland@famdouma.nl>
+ *
+ * @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\Files_Trashbin\Sabre;
+
+use OCP\Files\FileInfo;
+use Sabre\DAV\Exception\Forbidden;
+use Sabre\DAV\Exception\NotFound;
+use Sabre\DAV\ICollection;
+
+class TrashFolder implements ICollection, ITrash {
+ /** @var string */
+ private $userId;
+
+ /** @var FileInfo */
+ private $data;
+
+ public function __construct(string $root, string $userId, FileInfo $data) {
+ $this->userId = $userId;
+ $this->data = $data;
+ }
+
+ public function createFile($name, $data = null) {
+ throw new Forbidden();
+ }
+
+ public function createDirectory($name) {
+ throw new Forbidden();
+ }
+
+ public function getChild($name): ITrash {
+ $entries = \OCA\Files_Trashbin\Helper::getTrashFiles($this->getName(), $this->userId);
+
+ foreach ($entries as $entry) {
+ if ($entry->getName() === $name) {
+ if ($entry->getType() === FileInfo::TYPE_FOLDER) {
+ return new TrashFolderFolder($this->getName(), $this->userId, $entry, $this->getOriginalLocation());
+ }
+ return new TrashFolderFile($this->getName(), $this->userId, $entry, $this->getOriginalLocation());
+ }
+ }
+
+ throw new NotFound();
+ }
+
+ public function getChildren(): array {
+ $entries = \OCA\Files_Trashbin\Helper::getTrashFiles($this->getName(), $this->userId);
+
+ $children = array_map(function (FileInfo $entry) {
+ if ($entry->getType() === FileInfo::TYPE_FOLDER) {
+ return new TrashFolderFolder($this->getName(), $this->userId, $entry, $this->getOriginalLocation());
+ }
+ return new TrashFolderFile($this->getName(), $this->userId, $entry, $this->getOriginalLocation());
+ }, $entries);
+
+ return $children;
+ }
+
+ public function childExists($name): bool {
+ $entries = \OCA\Files_Trashbin\Helper::getTrashFiles($this->getName(), $this->userId);
+
+ foreach ($entries as $entry) {
+ if ($entry->getName() === $name) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ public function delete() {
+ \OCA\Files_Trashbin\Trashbin::delete($this->data->getName(), $this->userId, $this->getLastModified());
+ }
+
+ public function getName(): string {
+ return $this->data->getName() . '.d' . $this->getLastModified();
+ }
+
+ public function setName($name) {
+ throw new Forbidden();
+ }
+
+ public function getLastModified(): int {
+ return $this->data->getMtime();
+ }
+
+ public function restore(): bool {
+ return \OCA\Files_Trashbin\Trashbin::restore($this->getName(), $this->data->getName(), $this->getLastModified());
+ }
+
+ public function getFilename(): string {
+ return $this->data->getName();
+ }
+
+ public function getOriginalLocation(): string {
+ return $this->data['extraData'];
+ }
+
+
+}
diff --git a/apps/files_trashbin/lib/Sabre/TrashFolderFile.php b/apps/files_trashbin/lib/Sabre/TrashFolderFile.php
new file mode 100644
index 00000000000..95e82d95a6e
--- /dev/null
+++ b/apps/files_trashbin/lib/Sabre/TrashFolderFile.php
@@ -0,0 +1,102 @@
+<?php
+declare(strict_types=1);
+/**
+ * @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl>
+ *
+ * @author Roeland Jago Douma <roeland@famdouma.nl>
+ *
+ * @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\Files_Trashbin\Sabre;
+
+use OCP\Files\FileInfo;
+use Sabre\DAV\Exception\Forbidden;
+use Sabre\DAV\IFile;
+
+class TrashFolderFile implements IFile, ITrash {
+ /** @var string */
+ private $root;
+
+ /** @var string */
+ private $userId;
+
+ /** @var FileInfo */
+ private $data;
+
+ /** @var string */
+ private $location;
+
+ public function __construct(string $root,
+ string $userId,
+ FileInfo $data,
+ string $location) {
+ $this->root = $root;
+ $this->userId = $userId;
+ $this->data = $data;
+ $this->location = $location;
+ }
+
+ public function put($data) {
+ throw new Forbidden();
+ }
+
+ public function get() {
+ return $this->data->getStorage()->fopen($this->data->getInternalPath(), 'rb');
+ }
+
+ public function getContentType(): string {
+ return $this->data->getMimetype();
+ }
+
+ public function getETag(): string {
+ return $this->data->getEtag();
+ }
+
+ public function getSize(): int {
+ return $this->data->getSize();
+ }
+
+ public function delete() {
+ \OCA\Files_Trashbin\Trashbin::delete($this->root . '/' . $this->getName(), $this->userId, null);
+ }
+
+ public function getName(): string {
+ return $this->data->getName();
+ }
+
+ public function setName($name) {
+ throw new Forbidden();
+ }
+
+ public function getLastModified(): int {
+ return $this->data->getMtime();
+ }
+
+ public function restore(): bool {
+ return \OCA\Files_Trashbin\Trashbin::restore($this->root . '/' . $this->getName(), $this->data->getName(), null);
+ }
+
+ public function getFilename(): string {
+ return $this->data->getName();
+ }
+
+ public function getOriginalLocation(): string {
+ return $this->location . '/' . $this->getFilename();
+ }
+
+
+}
diff --git a/apps/files_trashbin/lib/Sabre/TrashFolderFolder.php b/apps/files_trashbin/lib/Sabre/TrashFolderFolder.php
new file mode 100644
index 00000000000..d2923c58916
--- /dev/null
+++ b/apps/files_trashbin/lib/Sabre/TrashFolderFolder.php
@@ -0,0 +1,133 @@
+<?php
+declare(strict_types=1);
+/**
+ * @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl>
+ *
+ * @author Roeland Jago Douma <roeland@famdouma.nl>
+ *
+ * @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\Files_Trashbin\Sabre;
+
+use OCP\Files\FileInfo;
+use Sabre\DAV\Exception\Forbidden;
+use Sabre\DAV\Exception\NotFound;
+use Sabre\DAV\ICollection;
+
+class TrashFolderFolder implements ICollection, ITrash {
+
+ /** @var string */
+ private $root;
+
+ /** @var string */
+ private $userId;
+
+ /** @var FileInfo */
+ private $data;
+
+ /** @var string */
+ private $location;
+
+ public function __construct(string $root,
+ string $userId,
+ FileInfo $data,
+ string $location) {
+ $this->root = $root;
+ $this->userId = $userId;
+ $this->data = $data;
+ $this->location = $location;
+ }
+
+ public function createFile($name, $data = null) {
+ throw new Forbidden();
+ }
+
+ public function createDirectory($name) {
+ throw new Forbidden();
+ }
+
+ public function getChild($name): ITrash {
+ $entries = \OCA\Files_Trashbin\Helper::getTrashFiles($this->root . '/' . $this->getName(), $this->userId);
+
+ foreach ($entries as $entry) {
+ if ($entry->getName() === $name) {
+ if ($entry->getType() === FileInfo::TYPE_FOLDER) {
+ return new TrashFolderFolder($this->root . '/' . $this->getName(), $this->userId, $entry, $this->getOriginalLocation());
+ }
+ return new TrashFolderFile($this->root . '/' . $this->getName(), $this->userId, $entry, $this->getOriginalLocation());
+ }
+ }
+
+ throw new NotFound();
+ }
+
+ public function getChildren(): array {
+ $entries = \OCA\Files_Trashbin\Helper::getTrashFiles($this->root . '/' . $this->getName(), $this->userId);
+
+ $children = array_map(function (FileInfo $entry) {
+ if ($entry->getType() === FileInfo::TYPE_FOLDER) {
+ return new TrashFolderFolder($this->root.'/'.$this->getName(), $this->userId, $entry, $this->getOriginalLocation());
+ }
+ return new TrashFolderFile($this->root.'/'.$this->getName(), $this->userId, $entry, $this->getOriginalLocation());
+ }, $entries);
+
+ return $children;
+ }
+
+ public function childExists($name): bool {
+ $entries = \OCA\Files_Trashbin\Helper::getTrashFiles($this->root . '/' . $this->getName(), $this->userId);
+
+ foreach ($entries as $entry) {
+ if ($entry->getName() === $name) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ public function delete() {
+ \OCA\Files_Trashbin\Trashbin::delete($this->root . '/' . $this->getName(), $this->userId, null);
+ }
+
+ public function getName(): string {
+ return $this->data->getName();
+
+ }
+
+ public function setName($name) {
+ throw new Forbidden();
+ }
+
+ public function getLastModified(): int {
+ return $this->data->getMtime();
+ }
+
+ public function restore(): bool {
+ return \OCA\Files_Trashbin\Trashbin::restore($this->root . '/' . $this->getName(), $this->data->getName(), null);
+ }
+
+ public function getFilename(): string {
+ return $this->data->getName();
+ }
+
+ public function getOriginalLocation(): string {
+ return $this->location . '/' . $this->getFilename();
+ }
+
+
+}
diff --git a/apps/files_trashbin/lib/Sabre/TrashHome.php b/apps/files_trashbin/lib/Sabre/TrashHome.php
new file mode 100644
index 00000000000..d1c50c9c6a3
--- /dev/null
+++ b/apps/files_trashbin/lib/Sabre/TrashHome.php
@@ -0,0 +1,89 @@
+<?php
+declare(strict_types=1);
+/**
+ * @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl>
+ *
+ * @author Roeland Jago Douma <roeland@famdouma.nl>
+ *
+ * @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\Files_Trashbin\Sabre;
+
+use Sabre\DAV\Exception\Forbidden;
+use Sabre\DAV\Exception\NotFound;
+use Sabre\DAV\ICollection;
+
+class TrashHome implements ICollection {
+
+ /** @var array */
+ private $principalInfo;
+
+ public function __construct(array $principalInfo) {
+ $this->principalInfo = $principalInfo;
+ }
+
+ public function delete() {
+ throw new Forbidden();
+ }
+
+ public function getName(): string {
+ list(,$name) = \Sabre\Uri\split($this->principalInfo['uri']);
+ return $name;
+ }
+
+ public function setName($name) {
+ throw new Forbidden('Permission denied to rename this trashbin');
+ }
+
+ public function createFile($name, $data = null) {
+ throw new Forbidden('Not allowed to create files in the trashbin');
+ }
+
+ public function createDirectory($name) {
+ throw new Forbidden('Not allowed to create folders in the trashbin');
+ }
+
+ public function getChild($name) {
+ list(,$userId) = \Sabre\Uri\split($this->principalInfo['uri']);
+
+ if ($name === 'restore') {
+ return new RestoreFolder($userId);
+ }
+ if ($name === 'trash') {
+ return new TrashRoot($userId);
+ }
+
+ throw new NotFound();
+ }
+
+ public function getChildren(): array {
+ list(,$userId) = \Sabre\Uri\split($this->principalInfo['uri']);
+
+ return [
+ new RestoreFolder($userId),
+ new TrashRoot($userId),
+ ];
+ }
+
+ public function childExists($name): bool {
+ return $name === 'restore' || $name === 'trash';
+ }
+
+ public function getLastModified(): int {
+ return 0;
+ }
+}
diff --git a/apps/files_trashbin/lib/Sabre/TrashRoot.php b/apps/files_trashbin/lib/Sabre/TrashRoot.php
new file mode 100644
index 00000000000..73b9d44d7e1
--- /dev/null
+++ b/apps/files_trashbin/lib/Sabre/TrashRoot.php
@@ -0,0 +1,103 @@
+<?php
+declare(strict_types=1);
+/**
+ * @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl>
+ *
+ * @author Roeland Jago Douma <roeland@famdouma.nl>
+ *
+ * @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\Files_Trashbin\Sabre;
+
+use OCP\Files\FileInfo;
+use Sabre\DAV\Exception\Forbidden;
+use Sabre\DAV\Exception\NotFound;
+use Sabre\DAV\ICollection;
+
+class TrashRoot implements ICollection {
+
+ /** @var string */
+ private $userId;
+
+ public function __construct(string $userId) {
+ $this->userId = $userId;
+ }
+
+ public function delete() {
+ \OCA\Files_Trashbin\Trashbin::deleteAll();
+ }
+
+ public function getName(): string {
+ return 'trash';
+ }
+
+ public function setName($name) {
+ throw new Forbidden('Permission denied to rename this trashbin');
+ }
+
+ public function createFile($name, $data = null) {
+ throw new Forbidden('Not allowed to create files in the trashbin');
+ }
+
+ public function createDirectory($name) {
+ throw new Forbidden('Not allowed to create folders in the trashbin');
+ }
+
+ public function getChild($name): ITrash {
+ $entries = \OCA\Files_Trashbin\Helper::getTrashFiles('/', $this->userId);
+
+ foreach ($entries as $entry) {
+ if ($entry->getName() . '.d'.$entry->getMtime() === $name) {
+ if ($entry->getType() === FileInfo::TYPE_FOLDER) {
+ return new TrashFolder('/', $this->userId, $entry);
+ }
+ return new TrashFile($this->userId, $entry);
+ }
+ }
+
+ throw new NotFound();
+ }
+
+ public function getChildren(): array {
+ $entries = \OCA\Files_Trashbin\Helper::getTrashFiles('/', $this->userId);
+
+ $children = array_map(function (FileInfo $entry) {
+ if ($entry->getType() === FileInfo::TYPE_FOLDER) {
+ return new TrashFolder('/', $this->userId, $entry);
+ }
+ return new TrashFile($this->userId, $entry);
+ }, $entries);
+
+ return $children;
+ }
+
+ public function childExists($name): bool {
+ $entries = \OCA\Files_Trashbin\Helper::getTrashFiles('/', $this->userId);
+
+ foreach ($entries as $entry) {
+ if ($entry->getName() . '.d'.$entry->getMtime() === $name) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ public function getLastModified(): int {
+ return 0;
+ }
+}