From 6372ae3a98537e8d1b5c5adf4fc446bc0651c2b1 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Mon, 3 Sep 2018 18:30:10 +0200 Subject: [PATCH] expose additional props from trashbin sabre endpoint Signed-off-by: Robin Appelman --- .../composer/composer/autoload_classmap.php | 1 + .../composer/composer/autoload_static.php | 1 + .../lib/Sabre/AbstractTrash.php | 69 +++++++++++++++++++ apps/files_trashbin/lib/Sabre/ITrash.php | 4 ++ .../lib/Sabre/PropfindPlugin.php | 36 +++++++++- apps/files_trashbin/lib/Sabre/TrashFile.php | 37 +--------- apps/files_trashbin/lib/Sabre/TrashFolder.php | 27 +------- .../lib/Sabre/TrashFolderFile.php | 39 +---------- .../lib/Sabre/TrashFolderFolder.php | 32 +-------- 9 files changed, 116 insertions(+), 130 deletions(-) create mode 100644 apps/files_trashbin/lib/Sabre/AbstractTrash.php diff --git a/apps/files_trashbin/composer/composer/autoload_classmap.php b/apps/files_trashbin/composer/composer/autoload_classmap.php index 3713de530c..164d64333c 100644 --- a/apps/files_trashbin/composer/composer/autoload_classmap.php +++ b/apps/files_trashbin/composer/composer/autoload_classmap.php @@ -18,6 +18,7 @@ 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\\AbstractTrash' => $baseDir . '/../lib/Sabre/AbstractTrash.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', diff --git a/apps/files_trashbin/composer/composer/autoload_static.php b/apps/files_trashbin/composer/composer/autoload_static.php index b00778741b..6ebb8c35f3 100644 --- a/apps/files_trashbin/composer/composer/autoload_static.php +++ b/apps/files_trashbin/composer/composer/autoload_static.php @@ -33,6 +33,7 @@ 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\\AbstractTrash' => __DIR__ . '/..' . '/../lib/Sabre/AbstractTrash.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', diff --git a/apps/files_trashbin/lib/Sabre/AbstractTrash.php b/apps/files_trashbin/lib/Sabre/AbstractTrash.php new file mode 100644 index 0000000000..43f9cc0274 --- /dev/null +++ b/apps/files_trashbin/lib/Sabre/AbstractTrash.php @@ -0,0 +1,69 @@ + + * + * @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 . + * + */ + +namespace OCA\Files_Trashbin\Sabre; + +use OCP\Files\FileInfo; + +abstract class AbstractTrash implements ITrash { + /** @var FileInfo */ + protected $data; + + public function __construct(FileInfo $data) { + $this->data = $data; + } + + public function getFilename(): string { + return $this->data->getName(); + } + + public function getDeletionTime(): int { + return $this->data->getMtime(); + } + + public function getFileId(): int { + return $this->data->getId(); + } + + public function getFileInfo(): FileInfo { + return $this->data; + } + + public function getSize(): int { + return $this->data->getSize(); + } + + public function getLastModified(): int { + return $this->data->getMtime(); + } + + public function getContentType(): string { + return $this->data->getMimetype(); + } + + public function getETag(): string { + return $this->data->getEtag(); + } + + public function getName(): string { + return $this->data->getName(); + } +} diff --git a/apps/files_trashbin/lib/Sabre/ITrash.php b/apps/files_trashbin/lib/Sabre/ITrash.php index 6db9bccf0a..49c600c3f1 100644 --- a/apps/files_trashbin/lib/Sabre/ITrash.php +++ b/apps/files_trashbin/lib/Sabre/ITrash.php @@ -23,6 +23,8 @@ declare(strict_types=1); */ namespace OCA\Files_Trashbin\Sabre; +use OCP\Files\FileInfo; + interface ITrash { public function restore(): bool; @@ -35,4 +37,6 @@ interface ITrash { public function getSize(); public function getFileId(): int; + + public function getFileInfo(): FileInfo; } diff --git a/apps/files_trashbin/lib/Sabre/PropfindPlugin.php b/apps/files_trashbin/lib/Sabre/PropfindPlugin.php index 492035304b..19da79fd2a 100644 --- a/apps/files_trashbin/lib/Sabre/PropfindPlugin.php +++ b/apps/files_trashbin/lib/Sabre/PropfindPlugin.php @@ -25,6 +25,8 @@ declare(strict_types=1); namespace OCA\Files_Trashbin\Sabre; use OCA\DAV\Connector\Sabre\FilesPlugin; +use OCP\Constants; +use OCP\IPreview; use Sabre\DAV\INode; use Sabre\DAV\PropFind; use Sabre\DAV\Server; @@ -39,7 +41,13 @@ class PropfindPlugin extends ServerPlugin { /** @var Server */ private $server; - public function __construct() { + /** @var IPreview */ + private $previewManager; + + public function __construct( + IPreview $previewManager + ) { + $this->previewManager = $previewManager; } public function initialize(Server $server) { @@ -54,11 +62,11 @@ class PropfindPlugin extends ServerPlugin { return; } - $propFind->handle(self::TRASHBIN_FILENAME, function() use ($node) { + $propFind->handle(self::TRASHBIN_FILENAME, function () use ($node) { return $node->getFilename(); }); - $propFind->handle(self::TRASHBIN_ORIGINAL_LOCATION, function() use ($node) { + $propFind->handle(self::TRASHBIN_ORIGINAL_LOCATION, function () use ($node) { return $node->getOriginalLocation(); }); @@ -73,6 +81,28 @@ class PropfindPlugin extends ServerPlugin { $propFind->handle(FilesPlugin::FILEID_PROPERTYNAME, function () use ($node) { return $node->getFileId(); }); + + $propFind->handle(FilesPlugin::PERMISSIONS_PROPERTYNAME, function () { + return 'GD'; // read + delete + }); + + $propFind->handle(FilesPlugin::GETETAG_PROPERTYNAME, function () use ($node) { + // add fake etag, it is only needed to identify the preview image + return $node->getLastModified(); + }); + + $propFind->handle(FilesPlugin::INTERNAL_FILEID_PROPERTYNAME, function () use ($node) { + // add fake etag, it is only needed to identify the preview image + return $node->getFileId(); + }); + + $propFind->handle(FilesPlugin::HAS_PREVIEW_PROPERTYNAME, function () use ($node) { + return $this->previewManager->isAvailable($node->getFileInfo()); + }); + + $propFind->handle(FilesPlugin::MOUNT_TYPE_PROPERTYNAME, function () { + return ''; + }); } } diff --git a/apps/files_trashbin/lib/Sabre/TrashFile.php b/apps/files_trashbin/lib/Sabre/TrashFile.php index eba9eee641..840ca6a193 100644 --- a/apps/files_trashbin/lib/Sabre/TrashFile.php +++ b/apps/files_trashbin/lib/Sabre/TrashFile.php @@ -27,16 +27,13 @@ use OCP\Files\FileInfo; use Sabre\DAV\Exception\Forbidden; use Sabre\DAV\IFile; -class TrashFile implements IFile, ITrash { +class TrashFile extends AbstractTrash implements IFile, ITrash { /** @var string */ private $userId; - /** @var FileInfo */ - private $data; - public function __construct(string $userId, FileInfo $data) { $this->userId = $userId; - $this->data = $data; + parent::__construct($data); } public function put($data) { @@ -47,18 +44,6 @@ class TrashFile implements IFile, ITrash { 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()); } @@ -71,29 +56,11 @@ class TrashFile implements IFile, ITrash { 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']; } - - public function getDeletionTime(): int { - return $this->getLastModified(); - } - - public function getFileId(): int { - return $this->data->getId(); - } - - } diff --git a/apps/files_trashbin/lib/Sabre/TrashFolder.php b/apps/files_trashbin/lib/Sabre/TrashFolder.php index 6b7d71b80e..d884eefcc9 100644 --- a/apps/files_trashbin/lib/Sabre/TrashFolder.php +++ b/apps/files_trashbin/lib/Sabre/TrashFolder.php @@ -28,16 +28,13 @@ use Sabre\DAV\Exception\Forbidden; use Sabre\DAV\Exception\NotFound; use Sabre\DAV\ICollection; -class TrashFolder implements ICollection, ITrash { +class TrashFolder extends AbstractTrash 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; + parent::__construct($data); } public function createFile($name, $data = null) { @@ -100,31 +97,11 @@ class TrashFolder implements ICollection, ITrash { 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']; } - - public function getDeletionTime(): int { - return $this->getLastModified(); - } - - public function getSize(): int { - return $this->data->getSize(); - } - - public function getFileId(): int { - return $this->data->getId(); - } } diff --git a/apps/files_trashbin/lib/Sabre/TrashFolderFile.php b/apps/files_trashbin/lib/Sabre/TrashFolderFile.php index 921c98b02f..3e28d048b4 100644 --- a/apps/files_trashbin/lib/Sabre/TrashFolderFile.php +++ b/apps/files_trashbin/lib/Sabre/TrashFolderFile.php @@ -27,16 +27,13 @@ use OCP\Files\FileInfo; use Sabre\DAV\Exception\Forbidden; use Sabre\DAV\IFile; -class TrashFolderFile implements IFile, ITrash { +class TrashFolderFile extends AbstractTrash implements IFile, ITrash { /** @var string */ private $root; /** @var string */ private $userId; - /** @var FileInfo */ - private $data; - /** @var string */ private $location; @@ -46,8 +43,8 @@ class TrashFolderFile implements IFile, ITrash { string $location) { $this->root = $root; $this->userId = $userId; - $this->data = $data; $this->location = $location; + parent::__construct($data); } public function put($data) { @@ -58,51 +55,19 @@ class TrashFolderFile implements IFile, ITrash { 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(); } - - public function getDeletionTime(): int { - return $this->getLastModified(); - } - - public function getFileId(): int { - return $this->data->getId(); - } } diff --git a/apps/files_trashbin/lib/Sabre/TrashFolderFolder.php b/apps/files_trashbin/lib/Sabre/TrashFolderFolder.php index 2fe75479c1..4ee9a0e2db 100644 --- a/apps/files_trashbin/lib/Sabre/TrashFolderFolder.php +++ b/apps/files_trashbin/lib/Sabre/TrashFolderFolder.php @@ -28,7 +28,7 @@ use Sabre\DAV\Exception\Forbidden; use Sabre\DAV\Exception\NotFound; use Sabre\DAV\ICollection; -class TrashFolderFolder implements ICollection, ITrash { +class TrashFolderFolder extends AbstractTrash implements ICollection, ITrash { /** @var string */ private $root; @@ -36,9 +36,6 @@ class TrashFolderFolder implements ICollection, ITrash { /** @var string */ private $userId; - /** @var FileInfo */ - private $data; - /** @var string */ private $location; @@ -48,8 +45,8 @@ class TrashFolderFolder implements ICollection, ITrash { string $location) { $this->root = $root; $this->userId = $userId; - $this->data = $data; $this->location = $location; + parent::__construct($data); } public function createFile($name, $data = null) { @@ -104,40 +101,15 @@ class TrashFolderFolder implements ICollection, ITrash { \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(); } - - public function getDeletionTime(): int { - return $this->getLastModified(); - } - - public function getSize(): int { - return $this->data->getSize(); - } - - public function getFileId(): int { - return $this->data->getId(); - } }