diff options
author | Robin Appelman <robin@icewind.nl> | 2021-06-04 17:22:39 +0300 |
---|---|---|
committer | Robin Appelman <robin@icewind.nl> | 2021-08-23 16:38:58 +0300 |
commit | fd5e11a81abcc46605dac2d26f7374d34e08a152 (patch) | |
tree | 1b680e87671856d7c193096f9bb5bb39234768cb /apps/dav/lib/Connector/Sabre | |
parent | a8547cc0eadb4e92e1730b98424a413f22f76b50 (diff) |
Add dav plugin to trigger recalculating of checksums
Signed-off-by: Robin Appelman <robin@icewind.nl>
Diffstat (limited to 'apps/dav/lib/Connector/Sabre')
-rw-r--r-- | apps/dav/lib/Connector/Sabre/ChecksumUpdatePlugin.php | 83 | ||||
-rw-r--r-- | apps/dav/lib/Connector/Sabre/File.php | 15 | ||||
-rw-r--r-- | apps/dav/lib/Connector/Sabre/ServerFactory.php | 1 |
3 files changed, 95 insertions, 4 deletions
diff --git a/apps/dav/lib/Connector/Sabre/ChecksumUpdatePlugin.php b/apps/dav/lib/Connector/Sabre/ChecksumUpdatePlugin.php new file mode 100644 index 00000000000..3247259357f --- /dev/null +++ b/apps/dav/lib/Connector/Sabre/ChecksumUpdatePlugin.php @@ -0,0 +1,83 @@ +<?php + +declare(strict_types=1); +/** + * @copyright Copyright (c) 2021 Robin Appelman <robin@icewind.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\DAV\Connector\Sabre; + +use Sabre\DAV\ServerPlugin; +use Sabre\HTTP\RequestInterface; +use Sabre\HTTP\ResponseInterface; + +class ChecksumUpdatePlugin extends ServerPlugin { + /** + * @var \Sabre\DAV\Server + */ + protected $server; + + public function initialize(\Sabre\DAV\Server $server) { + $this->server = $server; + $server->on('method:PATCH', [$this, 'httpPatch']); + } + + public function getPluginName(): string { + return 'checksumupdate'; + } + + public function getHTTPMethods($path): array { + $tree = $this->server->tree; + + if ($tree->nodeExists($path)) { + $node = $tree->getNodeForPath($path); + if ($node instanceof File) { + return ['PATCH']; + } + } + + return []; + } + + public function getFeatures(): array { + return ['nextcloud-checksum-update']; + } + + public function httpPatch(RequestInterface $request, ResponseInterface $response) { + $path = $request->getPath(); + + $node = $this->server->tree->getNodeForPath($path); + if ($node instanceof File) { + $type = strtolower( + (string)$request->getHeader('X-Recalculate-Hash') + ); + + $hash = $node->hash($type); + if ($hash) { + $checksum = strtoupper($type) . ':' . $hash; + $node->setChecksum($checksum); + $response->addHeader('OC-Checksum', $checksum); + $response->setHeader('Content-Length', '0'); + $response->setStatus(204); + + return false; + } + } + } +} diff --git a/apps/dav/lib/Connector/Sabre/File.php b/apps/dav/lib/Connector/Sabre/File.php index 5ff5f831eb5..6bb30591d95 100644 --- a/apps/dav/lib/Connector/Sabre/File.php +++ b/apps/dav/lib/Connector/Sabre/File.php @@ -343,11 +343,9 @@ class File extends Node implements IFile { if (isset($this->request->server['HTTP_OC_CHECKSUM'])) { $checksum = trim($this->request->server['HTTP_OC_CHECKSUM']); - $this->fileView->putFileInfo($this->path, ['checksum' => $checksum]); - $this->refreshInfo(); + $this->setChecksum($checksum); } elseif ($this->getChecksum() !== null && $this->getChecksum() !== '') { - $this->fileView->putFileInfo($this->path, ['checksum' => '']); - $this->refreshInfo(); + $this->setChecksum(''); } } catch (StorageNotAvailableException $e) { throw new ServiceUnavailable("Failed to check file size: " . $e->getMessage(), 0, $e); @@ -688,9 +686,18 @@ class File extends Node implements IFile { return $this->info->getChecksum(); } + public function setChecksum(string $checksum) { + $this->fileView->putFileInfo($this->path, ['checksum' => $checksum]); + $this->refreshInfo(); + } + protected function header($string) { if (!\OC::$CLI) { \header($string); } } + + public function hash(string $type) { + return $this->fileView->hash($type, $this->path); + } } diff --git a/apps/dav/lib/Connector/Sabre/ServerFactory.php b/apps/dav/lib/Connector/Sabre/ServerFactory.php index 7be24014881..e74fbbf47cf 100644 --- a/apps/dav/lib/Connector/Sabre/ServerFactory.php +++ b/apps/dav/lib/Connector/Sabre/ServerFactory.php @@ -177,6 +177,7 @@ class ServerFactory { ) ); $server->addPlugin(new \OCA\DAV\Connector\Sabre\QuotaPlugin($view, true)); + $server->addPlugin(new \OCA\DAV\Connector\Sabre\ChecksumUpdatePlugin()); if ($this->userSession->isLoggedIn()) { $server->addPlugin(new \OCA\DAV\Connector\Sabre\TagsPlugin($objectTree, $this->tagManager)); |