Merge pull request #9202 from nextcloud/feature/1332/trashbin_dav

Trashbin endpoint in DAV
This commit is contained in:
Roeland Jago Douma 2018-05-01 14:24:38 +02:00 committed by GitHub
commit 7f194be3ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 923 additions and 1 deletions

View File

@ -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>

View File

@ -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',
);

View File

@ -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',
);

View File

@ -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()
);
});
}
}

View File

@ -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;
}

View File

@ -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();
});
}
}

View File

@ -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();
}
}

View File

@ -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';
}
}

View File

@ -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'];
}
}

View File

@ -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'];
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}