aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files_versions/lib/Sabre
diff options
context:
space:
mode:
Diffstat (limited to 'apps/files_versions/lib/Sabre')
-rw-r--r--apps/files_versions/lib/Sabre/Plugin.php99
-rw-r--r--apps/files_versions/lib/Sabre/RestoreFolder.php61
-rw-r--r--apps/files_versions/lib/Sabre/RootCollection.php55
-rw-r--r--apps/files_versions/lib/Sabre/VersionCollection.php81
-rw-r--r--apps/files_versions/lib/Sabre/VersionFile.php108
-rw-r--r--apps/files_versions/lib/Sabre/VersionHome.php82
-rw-r--r--apps/files_versions/lib/Sabre/VersionRoot.php81
7 files changed, 567 insertions, 0 deletions
diff --git a/apps/files_versions/lib/Sabre/Plugin.php b/apps/files_versions/lib/Sabre/Plugin.php
new file mode 100644
index 00000000000..984c4a36e5b
--- /dev/null
+++ b/apps/files_versions/lib/Sabre/Plugin.php
@@ -0,0 +1,99 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+namespace OCA\Files_Versions\Sabre;
+
+use OC\AppFramework\Http\Request;
+use OCA\DAV\Connector\Sabre\FilesPlugin;
+use OCP\IPreview;
+use OCP\IRequest;
+use Sabre\DAV\Exception\NotFound;
+use Sabre\DAV\INode;
+use Sabre\DAV\PropFind;
+use Sabre\DAV\PropPatch;
+use Sabre\DAV\Server;
+use Sabre\DAV\ServerPlugin;
+use Sabre\HTTP\RequestInterface;
+use Sabre\HTTP\ResponseInterface;
+
+class Plugin extends ServerPlugin {
+ private Server $server;
+
+ public const LABEL = 'label';
+
+ public const AUTHOR = 'author';
+
+ public const VERSION_LABEL = '{http://nextcloud.org/ns}version-label';
+
+ public const VERSION_AUTHOR = '{http://nextcloud.org/ns}version-author'; // dav property for author
+
+ public function __construct(
+ private IRequest $request,
+ private IPreview $previewManager,
+ ) {
+ $this->request = $request;
+ }
+
+ public function initialize(Server $server) {
+ $this->server = $server;
+
+ $server->on('afterMethod:GET', [$this, 'afterGet']);
+ $server->on('propFind', [$this, 'propFind']);
+ $server->on('propPatch', [$this, 'propPatch']);
+ }
+
+ public function afterGet(RequestInterface $request, ResponseInterface $response) {
+ $path = $request->getPath();
+ if (!str_starts_with($path, 'versions')) {
+ return;
+ }
+
+ try {
+ $node = $this->server->tree->getNodeForPath($path);
+ } catch (NotFound $e) {
+ return;
+ }
+
+ if (!($node instanceof VersionFile)) {
+ return;
+ }
+
+ $filename = $node->getVersion()->getSourceFileName();
+
+ if ($this->request->isUserAgent(
+ [
+ Request::USER_AGENT_IE,
+ Request::USER_AGENT_ANDROID_MOBILE_CHROME,
+ Request::USER_AGENT_FREEBOX,
+ ])) {
+ $response->addHeader('Content-Disposition', 'attachment; filename="' . rawurlencode($filename) . '"');
+ } else {
+ $response->addHeader('Content-Disposition', 'attachment; filename*=UTF-8\'\'' . rawurlencode($filename)
+ . '; filename="' . rawurlencode($filename) . '"');
+ }
+ }
+
+ public function propFind(PropFind $propFind, INode $node): void {
+ if ($node instanceof VersionFile) {
+ $propFind->handle(self::VERSION_LABEL, fn () => $node->getMetadataValue(self::LABEL));
+ $propFind->handle(self::VERSION_AUTHOR, fn () => $node->getMetadataValue(self::AUTHOR));
+ $propFind->handle(
+ FilesPlugin::HAS_PREVIEW_PROPERTYNAME,
+ fn (): string => $this->previewManager->isMimeSupported($node->getContentType()) ? 'true' : 'false',
+ );
+ }
+ }
+
+ public function propPatch($path, PropPatch $propPatch): void {
+ $node = $this->server->tree->getNodeForPath($path);
+
+ if ($node instanceof VersionFile) {
+ $propPatch->handle(self::VERSION_LABEL, fn (string $label) => $node->setMetadataValue(self::LABEL, $label));
+ }
+ }
+}
diff --git a/apps/files_versions/lib/Sabre/RestoreFolder.php b/apps/files_versions/lib/Sabre/RestoreFolder.php
new file mode 100644
index 00000000000..7904b098a4f
--- /dev/null
+++ b/apps/files_versions/lib/Sabre/RestoreFolder.php
@@ -0,0 +1,61 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+namespace OCA\Files_Versions\Sabre;
+
+use Sabre\DAV\Exception\Forbidden;
+use Sabre\DAV\ICollection;
+use Sabre\DAV\IMoveTarget;
+use Sabre\DAV\INode;
+
+class RestoreFolder implements ICollection, IMoveTarget {
+ 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 VersionFile)) {
+ return false;
+ }
+
+ $sourceNode->rollBack();
+ return true;
+ }
+}
diff --git a/apps/files_versions/lib/Sabre/RootCollection.php b/apps/files_versions/lib/Sabre/RootCollection.php
new file mode 100644
index 00000000000..1e7129f23da
--- /dev/null
+++ b/apps/files_versions/lib/Sabre/RootCollection.php
@@ -0,0 +1,55 @@
+<?php
+
+/**
+ * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+namespace OCA\Files_Versions\Sabre;
+
+use OCA\Files_Versions\Versions\IVersionManager;
+use OCP\Files\IRootFolder;
+use OCP\IConfig;
+use OCP\IUserManager;
+use OCP\IUserSession;
+use Sabre\DAV\INode;
+use Sabre\DAVACL\AbstractPrincipalCollection;
+use Sabre\DAVACL\PrincipalBackend;
+
+class RootCollection extends AbstractPrincipalCollection {
+
+ public function __construct(
+ PrincipalBackend\BackendInterface $principalBackend,
+ private IRootFolder $rootFolder,
+ IConfig $config,
+ private IUserManager $userManager,
+ private IVersionManager $versionManager,
+ private IUserSession $userSession,
+ ) {
+ parent::__construct($principalBackend, 'principals/users');
+
+ $this->disableListing = !$config->getSystemValue('debug', false);
+ }
+
+ /**
+ * 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) {
+ [, $name] = \Sabre\Uri\split($principalInfo['uri']);
+ $user = $this->userSession->getUser();
+ if (is_null($user) || $name !== $user->getUID()) {
+ throw new \Sabre\DAV\Exception\Forbidden();
+ }
+ return new VersionHome($principalInfo, $this->rootFolder, $this->userManager, $this->versionManager);
+ }
+
+ public function getName() {
+ return 'versions';
+ }
+}
diff --git a/apps/files_versions/lib/Sabre/VersionCollection.php b/apps/files_versions/lib/Sabre/VersionCollection.php
new file mode 100644
index 00000000000..375d5cf99f2
--- /dev/null
+++ b/apps/files_versions/lib/Sabre/VersionCollection.php
@@ -0,0 +1,81 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+namespace OCA\Files_Versions\Sabre;
+
+use OCA\Files_Versions\Versions\IVersion;
+use OCA\Files_Versions\Versions\IVersionManager;
+use OCP\Files\File;
+use OCP\IUser;
+use Sabre\DAV\Exception\Forbidden;
+use Sabre\DAV\Exception\NotFound;
+use Sabre\DAV\ICollection;
+
+class VersionCollection implements ICollection {
+
+ public function __construct(
+ private File $file,
+ private IUser $user,
+ private IVersionManager $versionManager,
+ ) {
+ }
+
+ public function createFile($name, $data = null) {
+ throw new Forbidden();
+ }
+
+ public function createDirectory($name) {
+ throw new Forbidden();
+ }
+
+ public function getChild($name) {
+ /** @var VersionFile[] $versions */
+ $versions = $this->getChildren();
+
+ foreach ($versions as $version) {
+ if ($version->getName() === $name) {
+ return $version;
+ }
+ }
+
+ throw new NotFound();
+ }
+
+ public function getChildren(): array {
+ $versions = $this->versionManager->getVersionsForFile($this->user, $this->file);
+
+ return array_map(function (IVersion $version) {
+ return new VersionFile($version, $this->versionManager);
+ }, $versions);
+ }
+
+ public function childExists($name): bool {
+ try {
+ $this->getChild($name);
+ return true;
+ } catch (NotFound $e) {
+ return false;
+ }
+ }
+
+ public function delete() {
+ throw new Forbidden();
+ }
+
+ public function getName(): string {
+ return (string)$this->file->getId();
+ }
+
+ public function setName($name) {
+ throw new Forbidden();
+ }
+
+ public function getLastModified(): int {
+ return 0;
+ }
+}
diff --git a/apps/files_versions/lib/Sabre/VersionFile.php b/apps/files_versions/lib/Sabre/VersionFile.php
new file mode 100644
index 00000000000..faa03473648
--- /dev/null
+++ b/apps/files_versions/lib/Sabre/VersionFile.php
@@ -0,0 +1,108 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+namespace OCA\Files_Versions\Sabre;
+
+use OCA\Files_Versions\Versions\IDeletableVersionBackend;
+use OCA\Files_Versions\Versions\IMetadataVersion;
+use OCA\Files_Versions\Versions\IMetadataVersionBackend;
+use OCA\Files_Versions\Versions\INameableVersion;
+use OCA\Files_Versions\Versions\INameableVersionBackend;
+use OCA\Files_Versions\Versions\IVersion;
+use OCA\Files_Versions\Versions\IVersionManager;
+use OCP\Files\NotFoundException;
+use Sabre\DAV\Exception\Forbidden;
+use Sabre\DAV\Exception\NotFound;
+use Sabre\DAV\IFile;
+
+class VersionFile implements IFile {
+ public function __construct(
+ private IVersion $version,
+ private IVersionManager $versionManager,
+ ) {
+ }
+
+ public function put($data) {
+ throw new Forbidden();
+ }
+
+ public function get() {
+ try {
+ return $this->versionManager->read($this->version);
+ } catch (NotFoundException $e) {
+ throw new NotFound();
+ }
+ }
+
+ public function getContentType(): string {
+ return $this->version->getMimeType();
+ }
+
+ public function getETag(): string {
+ return (string)$this->version->getRevisionId();
+ }
+
+ /**
+ * @psalm-suppress ImplementedReturnTypeMismatch \Sabre\DAV\IFile::getSize signature does not support 32bit
+ * @return int|float
+ */
+ public function getSize(): int|float {
+ return $this->version->getSize();
+ }
+
+ public function delete() {
+ if ($this->versionManager instanceof IDeletableVersionBackend) {
+ $this->versionManager->deleteVersion($this->version);
+ } else {
+ throw new Forbidden();
+ }
+ }
+
+ public function getName(): string {
+ return (string)$this->version->getRevisionId();
+ }
+
+ public function setName($name) {
+ throw new Forbidden();
+ }
+
+ public function setMetadataValue(string $key, string $value): bool {
+ $backend = $this->version->getBackend();
+
+ if ($backend instanceof IMetadataVersionBackend) {
+ $backend->setMetadataValue($this->version->getSourceFile(), $this->version->getTimestamp(), $key, $value);
+ return true;
+ } elseif ($key === 'label' && $backend instanceof INameableVersionBackend) {
+ $backend->setVersionLabel($this->version, $value);
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ public function getMetadataValue(string $key): ?string {
+ if ($this->version instanceof IMetadataVersion) {
+ return $this->version->getMetadataValue($key);
+ } elseif ($key === 'label' && $this->version instanceof INameableVersion) {
+ return $this->version->getLabel();
+ }
+ return null;
+ }
+
+ public function getLastModified(): int {
+ return $this->version->getTimestamp();
+ }
+
+ public function rollBack() {
+ $this->versionManager->rollback($this->version);
+ }
+
+ public function getVersion(): IVersion {
+ return $this->version;
+ }
+}
diff --git a/apps/files_versions/lib/Sabre/VersionHome.php b/apps/files_versions/lib/Sabre/VersionHome.php
new file mode 100644
index 00000000000..07ac491f2a1
--- /dev/null
+++ b/apps/files_versions/lib/Sabre/VersionHome.php
@@ -0,0 +1,82 @@
+<?php
+
+/**
+ * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+namespace OCA\Files_Versions\Sabre;
+
+use OC\User\NoUserException;
+use OCA\Files_Versions\Versions\IVersionManager;
+use OCP\Files\IRootFolder;
+use OCP\IUserManager;
+use Sabre\DAV\Exception\Forbidden;
+use Sabre\DAV\ICollection;
+
+class VersionHome implements ICollection {
+
+ public function __construct(
+ private array $principalInfo,
+ private IRootFolder $rootFolder,
+ private IUserManager $userManager,
+ private IVersionManager $versionManager,
+ ) {
+ }
+
+ private function getUser() {
+ [, $name] = \Sabre\Uri\split($this->principalInfo['uri']);
+ $user = $this->userManager->get($name);
+ if (!$user) {
+ throw new NoUserException();
+ }
+ return $user;
+ }
+
+ public function delete() {
+ throw new Forbidden();
+ }
+
+ public function getName(): string {
+ return $this->getUser()->getUID();
+ }
+
+ public function setName($name) {
+ throw new Forbidden();
+ }
+
+ public function createFile($name, $data = null) {
+ throw new Forbidden();
+ }
+
+ public function createDirectory($name) {
+ throw new Forbidden();
+ }
+
+ public function getChild($name) {
+ $user = $this->getUser();
+
+ if ($name === 'versions') {
+ return new VersionRoot($user, $this->rootFolder, $this->versionManager);
+ }
+ if ($name === 'restore') {
+ return new RestoreFolder();
+ }
+ }
+
+ public function getChildren() {
+ $user = $this->getUser();
+
+ return [
+ new VersionRoot($user, $this->rootFolder, $this->versionManager),
+ new RestoreFolder(),
+ ];
+ }
+
+ public function childExists($name) {
+ return $name === 'versions' || $name === 'restore';
+ }
+
+ public function getLastModified() {
+ return 0;
+ }
+}
diff --git a/apps/files_versions/lib/Sabre/VersionRoot.php b/apps/files_versions/lib/Sabre/VersionRoot.php
new file mode 100644
index 00000000000..7f7014fbee3
--- /dev/null
+++ b/apps/files_versions/lib/Sabre/VersionRoot.php
@@ -0,0 +1,81 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+namespace OCA\Files_Versions\Sabre;
+
+use OCA\Files_Versions\Versions\IVersionManager;
+use OCP\Files\File;
+use OCP\Files\IRootFolder;
+use OCP\IUser;
+use Sabre\DAV\Exception\Forbidden;
+use Sabre\DAV\Exception\NotFound;
+use Sabre\DAV\ICollection;
+
+class VersionRoot implements ICollection {
+
+ public function __construct(
+ private IUser $user,
+ private IRootFolder $rootFolder,
+ private IVersionManager $versionManager,
+ ) {
+ }
+
+ public function delete() {
+ throw new Forbidden();
+ }
+
+ public function getName(): string {
+ return 'versions';
+ }
+
+ public function setName($name) {
+ throw new Forbidden();
+ }
+
+ public function createFile($name, $data = null) {
+ throw new Forbidden();
+ }
+
+ public function createDirectory($name) {
+ throw new Forbidden();
+ }
+
+ public function getChild($name) {
+ $userFolder = $this->rootFolder->getUserFolder($this->user->getUID());
+
+ $fileId = (int)$name;
+ $node = $userFolder->getFirstNodeById($fileId);
+
+ if (!$node) {
+ throw new NotFound();
+ }
+
+ if (!$node instanceof File) {
+ throw new NotFound();
+ }
+
+ return new VersionCollection($node, $this->user, $this->versionManager);
+ }
+
+ public function getChildren(): array {
+ return [];
+ }
+
+ public function childExists($name): bool {
+ try {
+ $this->getChild($name);
+ return true;
+ } catch (NotFound $e) {
+ return false;
+ }
+ }
+
+ public function getLastModified(): int {
+ return 0;
+ }
+}