Browse Source
Merge pull request #9353 from nextcloud/feature/4724/versions_dav
Merge pull request #9353 from nextcloud/feature/4724/versions_dav
Add versions DAV endpointpull/9395/head
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 570 additions and 1 deletions
-
9apps/files_versions/appinfo/info.xml
-
6apps/files_versions/composer/composer/autoload_classmap.php
-
6apps/files_versions/composer/composer/autoload_static.php
-
13apps/files_versions/lib/AppInfo/Application.php
-
86apps/files_versions/lib/Sabre/RestoreFolder.php
-
65apps/files_versions/lib/Sabre/RootCollection.php
-
102apps/files_versions/lib/Sabre/VersionCollection.php
-
94apps/files_versions/lib/Sabre/VersionFile.php
-
90apps/files_versions/lib/Sabre/VersionHome.php
-
100apps/files_versions/lib/Sabre/VersionRoot.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_Versions\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 VersionFile)) { |
|||
return false; |
|||
} |
|||
|
|||
return $sourceNode->rollBack(); |
|||
} |
|||
|
|||
} |
|||
@ -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'; |
|||
} |
|||
|
|||
} |
|||
@ -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_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) { |
|||
/** @var VersionFile[] $versions */ |
|||
$versions = $this->getChildren(); |
|||
|
|||
foreach ($versions as $version) { |
|||
if ($version->getName() === $name) { |
|||
return $version; |
|||
} |
|||
} |
|||
|
|||
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, $this->userFolder->getParent()); |
|||
}, $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; |
|||
} |
|||
} |
|||
@ -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 OCP\Files\NotFoundException; |
|||
use Sabre\DAV\Exception\Forbidden; |
|||
use Sabre\DAV\Exception\NotFound; |
|||
use Sabre\DAV\IFile; |
|||
|
|||
class VersionFile implements IFile { |
|||
/** @var array */ |
|||
private $data; |
|||
|
|||
/** @var Folder */ |
|||
private $userRoot; |
|||
|
|||
public function __construct(array $data, Folder $userRoot) { |
|||
$this->data = $data; |
|||
$this->userRoot = $userRoot; |
|||
} |
|||
|
|||
public function put($data) { |
|||
throw new Forbidden(); |
|||
} |
|||
|
|||
public function get() { |
|||
try { |
|||
/** @var Folder $versions */ |
|||
$versions = $this->userRoot->get('files_versions'); |
|||
/** @var File $version */ |
|||
$version = $versions->get($this->data['path'].'.v'.$this->data['version']); |
|||
} catch (NotFoundException $e) { |
|||
throw new NotFound(); |
|||
} |
|||
|
|||
return $version->fopen('rb'); |
|||
} |
|||
|
|||
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']; |
|||
} |
|||
|
|||
public function rollBack(): bool { |
|||
return Storage::rollback($this->data['path'], $this->data['version']); |
|||
} |
|||
} |
|||
@ -0,0 +1,90 @@ |
|||
<?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); |
|||
} |
|||
if ($name === 'restore') { |
|||
return new RestoreFolder($userId); |
|||
} |
|||
} |
|||
|
|||
public function getChildren() { |
|||
list(,$userId) = \Sabre\Uri\split($this->principalInfo['uri']); |
|||
|
|||
return [ |
|||
new VersionRoot($userId, $this->rootFolder), |
|||
new RestoreFolder($userId), |
|||
]; |
|||
} |
|||
|
|||
public function childExists($name) { |
|||
return $name === 'versions' || $name === 'restore'; |
|||
} |
|||
|
|||
public function getLastModified() { |
|||
return 0; |
|||
} |
|||
} |
|||
@ -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; |
|||
} |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue