diff options
author | Roeland Jago Douma <rullzer@users.noreply.github.com> | 2018-05-01 14:24:38 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-05-01 14:24:38 +0200 |
commit | 7f194be3ab4ad0ee8edf095bf6d79a17f116f782 (patch) | |
tree | 02e9ab8ad1e1ab6bcc0e724c3ee94ab3c27172e6 /apps/files_trashbin/lib | |
parent | c8cb42fd2671c8b6ed3257e62fa9f6a24474acb8 (diff) | |
parent | c625fc5931c904e5d6bafe855429eebeae23467f (diff) | |
download | nextcloud-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/lib')
-rw-r--r-- | apps/files_trashbin/lib/AppInfo/Application.php | 13 | ||||
-rw-r--r-- | apps/files_trashbin/lib/Sabre/ITrash.php | 32 | ||||
-rw-r--r-- | apps/files_trashbin/lib/Sabre/PropfindPlugin.php | 64 | ||||
-rw-r--r-- | apps/files_trashbin/lib/Sabre/RestoreFolder.php | 86 | ||||
-rw-r--r-- | apps/files_trashbin/lib/Sabre/RootCollection.php | 59 | ||||
-rw-r--r-- | apps/files_trashbin/lib/Sabre/TrashFile.php | 91 | ||||
-rw-r--r-- | apps/files_trashbin/lib/Sabre/TrashFolder.php | 120 | ||||
-rw-r--r-- | apps/files_trashbin/lib/Sabre/TrashFolderFile.php | 102 | ||||
-rw-r--r-- | apps/files_trashbin/lib/Sabre/TrashFolderFolder.php | 133 | ||||
-rw-r--r-- | apps/files_trashbin/lib/Sabre/TrashHome.php | 89 | ||||
-rw-r--r-- | apps/files_trashbin/lib/Sabre/TrashRoot.php | 103 |
11 files changed, 892 insertions, 0 deletions
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; + } +} |