aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files_versions/lib
diff options
context:
space:
mode:
authorRoeland Jago Douma <roeland@famdouma.nl>2018-04-30 20:39:45 +0200
committerRoeland Jago Douma <roeland@famdouma.nl>2018-05-01 08:38:59 +0200
commitd40afac1b9e9fe4bb7b64d2c5014a9ba6edb8957 (patch)
tree80305ebb6bbfa93f697c262259aff615e821874d /apps/files_versions/lib
parentc8cb42fd2671c8b6ed3257e62fa9f6a24474acb8 (diff)
downloadnextcloud-server-d40afac1b9e9fe4bb7b64d2c5014a9ba6edb8957.tar.gz
nextcloud-server-d40afac1b9e9fe4bb7b64d2c5014a9ba6edb8957.zip
Add version collection to DAV
listed as versions/<username>/versions/<file id>/<timastamp> Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
Diffstat (limited to 'apps/files_versions/lib')
-rw-r--r--apps/files_versions/lib/AppInfo/Application.php13
-rw-r--r--apps/files_versions/lib/Sabre/RootCollection.php65
-rw-r--r--apps/files_versions/lib/Sabre/VersionCollection.php94
-rw-r--r--apps/files_versions/lib/Sabre/VersionFile.php73
-rw-r--r--apps/files_versions/lib/Sabre/VersionHome.php86
-rw-r--r--apps/files_versions/lib/Sabre/VersionRoot.php100
6 files changed, 431 insertions, 0 deletions
diff --git a/apps/files_versions/lib/AppInfo/Application.php b/apps/files_versions/lib/AppInfo/Application.php
index e09299c8bc8..4431fbf51bf 100644
--- a/apps/files_versions/lib/AppInfo/Application.php
+++ b/apps/files_versions/lib/AppInfo/Application.php
@@ -23,6 +23,7 @@
namespace OCA\Files_Versions\AppInfo;
+use OCA\DAV\Connector\Sabre\Principal;
use OCP\AppFramework\App;
use OCA\Files_Versions\Expiration;
use OCP\AppFramework\Utility\ITimeFactory;
@@ -48,5 +49,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_versions/lib/Sabre/RootCollection.php b/apps/files_versions/lib/Sabre/RootCollection.php
new file mode 100644
index 00000000000..397f1c55891
--- /dev/null
+++ b/apps/files_versions/lib/Sabre/RootCollection.php
@@ -0,0 +1,65 @@
+<?php
+/**
+ * @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_Versions\Sabre;
+
+use OCP\Files\IRootFolder;
+use Sabre\DAV\INode;
+use Sabre\DAVACL\AbstractPrincipalCollection;
+use Sabre\DAVACL\PrincipalBackend;
+
+class RootCollection extends AbstractPrincipalCollection {
+
+ /** @var IRootFolder */
+ private $rootFolder;
+
+ public function __construct(PrincipalBackend\BackendInterface $principalBackend,
+ IRootFolder $rootFolder) {
+ parent::__construct($principalBackend, 'principals/users');
+
+ $this->rootFolder = $rootFolder;
+ }
+
+ /**
+ * 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) {
+ 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 VersionHome($principalInfo, $this->rootFolder);
+ }
+
+ 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..a9c83fe475a
--- /dev/null
+++ b/apps/files_versions/lib/Sabre/VersionCollection.php
@@ -0,0 +1,94 @@
+<?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_Versions\Sabre;
+
+use OCA\Files_Versions\Storage;
+use OCP\Files\File;
+use OCP\Files\Folder;
+use Sabre\DAV\Exception\Forbidden;
+use Sabre\DAV\Exception\NotFound;
+use Sabre\DAV\ICollection;
+
+class VersionCollection implements ICollection {
+ /** @var Folder */
+ private $userFolder;
+
+ /** @var File */
+ private $file;
+
+ /** @var string */
+ private $userId;
+
+ public function __construct(Folder $userFolder, File $file, string $userId) {
+ $this->userFolder = $userFolder;
+ $this->file = $file;
+ $this->userId = $userId;
+ }
+
+ public function createFile($name, $data = null) {
+ throw new Forbidden();
+ }
+
+ public function createDirectory($name) {
+ throw new Forbidden();
+ }
+
+ public function getChild($name) {
+
+ throw new NotFound();
+ }
+
+ public function getChildren(): array {
+ $versions = Storage::getVersions($this->userId, $this->userFolder->getRelativePath($this->file->getPath()));
+
+ return array_map(function (array $data) {
+ return new VersionFile($data);
+ }, $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..52bc9b4898b
--- /dev/null
+++ b/apps/files_versions/lib/Sabre/VersionFile.php
@@ -0,0 +1,73 @@
+<?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_Versions\Sabre;
+
+use OCP\Files\FileInfo;
+use Sabre\DAV\Exception\Forbidden;
+use Sabre\DAV\IFile;
+
+class VersionFile implements IFile {
+ /** @var array */
+ private $data;
+
+ public function __construct(array $data) {
+ $this->data = $data;
+ }
+
+ public function put($data) {
+ throw new Forbidden();
+ }
+
+ public function get() {
+ throw new Forbidden();
+ }
+
+ public function getContentType(): string {
+ return $this->data['mimetype'];
+ }
+
+ public function getETag(): string {
+ return $this->data['version'];
+ }
+
+ public function getSize(): int {
+ return $this->data['size'];
+ }
+
+ public function delete() {
+ throw new Forbidden();
+ }
+
+ public function getName(): string {
+ return $this->data['version'];
+ }
+
+ public function setName($name) {
+ throw new Forbidden();
+ }
+
+ public function getLastModified(): int {
+ return (int)$this->data['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..8ad9345f25e
--- /dev/null
+++ b/apps/files_versions/lib/Sabre/VersionHome.php
@@ -0,0 +1,86 @@
+<?php
+/**
+ * @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_Versions\Sabre;
+
+use OCP\Files\IRootFolder;
+use Sabre\DAV\Exception\Forbidden;
+use Sabre\DAV\ICollection;
+
+class VersionHome implements ICollection {
+
+ /** @var array */
+ private $principalInfo;
+
+ /** @var IRootFolder */
+ private $rootFolder;
+
+ public function __construct(array $principalInfo, IRootFolder $rootFolder) {
+ $this->principalInfo = $principalInfo;
+ $this->rootFolder = $rootFolder;
+ }
+
+ 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();
+ }
+
+ public function createFile($name, $data = null) {
+ throw new Forbidden();
+ }
+
+ public function createDirectory($name) {
+ throw new Forbidden();
+ }
+
+ public function getChild($name) {
+ list(,$userId) = \Sabre\Uri\split($this->principalInfo['uri']);
+
+ if ($name === 'versions') {
+ return new VersionRoot($userId, $this->rootFolder);
+ }
+ }
+
+ public function getChildren() {
+ list(,$userId) = \Sabre\Uri\split($this->principalInfo['uri']);
+
+ return [
+ new VersionRoot($userId, $this->rootFolder),
+ ];
+ }
+
+ 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..743b1c6ef1b
--- /dev/null
+++ b/apps/files_versions/lib/Sabre/VersionRoot.php
@@ -0,0 +1,100 @@
+<?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_Versions\Sabre;
+
+use OCP\Files\File;
+use OCP\Files\IRootFolder;
+use Sabre\DAV\Exception\Forbidden;
+use Sabre\DAV\Exception\NotFound;
+use Sabre\DAV\ICollection;
+
+class VersionRoot implements ICollection {
+
+ /** @var string */
+ private $userId;
+
+ /** @var IRootFolder */
+ private $rootFolder;
+
+ public function __construct(string $userId, IRootFolder $rootFolder) {
+ $this->userId = $userId;
+ $this->rootFolder = $rootFolder;
+ }
+
+ 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->userId);
+
+ $fileId = (int)$name;
+ $nodes = $userFolder->getById($fileId);
+
+ if ($nodes === []) {
+ throw new NotFound();
+ }
+
+ $node = array_pop($nodes);
+
+ if (!$node instanceof File) {
+ throw new NotFound();
+ }
+
+ return new VersionCollection($userFolder, $node, $this->userId);
+ }
+
+ 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;
+ }
+}