|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * @copyright Copyright (c) 2021 Robin Appelman <robin@icewind.nl> |
| 6 | + * |
| 7 | + * @license GNU AGPL version 3 or any later version |
| 8 | + * |
| 9 | + * This program is free software: you can redistribute it and/or modify |
| 10 | + * it under the terms of the GNU Affero General Public License as |
| 11 | + * published by the Free Software Foundation, either version 3 of the |
| 12 | + * License, or (at your option) any later version. |
| 13 | + * |
| 14 | + * This program is distributed in the hope that it will be useful, |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | + * GNU Affero General Public License for more details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU Affero General Public License |
| 20 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | + * |
| 22 | + */ |
| 23 | + |
| 24 | +namespace OCA\DAV\Connector\Sabre; |
| 25 | + |
| 26 | +use Sabre\DAV\ServerPlugin; |
| 27 | +use Sabre\HTTP\RequestInterface; |
| 28 | +use Sabre\HTTP\ResponseInterface; |
| 29 | + |
| 30 | +class ChecksumUpdatePlugin extends ServerPlugin { |
| 31 | + /** |
| 32 | + * @var \Sabre\DAV\Server |
| 33 | + */ |
| 34 | + protected $server; |
| 35 | + |
| 36 | + public function initialize(\Sabre\DAV\Server $server) { |
| 37 | + $this->server = $server; |
| 38 | + $server->on('method:PATCH', [$this, 'httpPatch']); |
| 39 | + } |
| 40 | + |
| 41 | + public function getPluginName(): string { |
| 42 | + return 'checksumupdate'; |
| 43 | + } |
| 44 | + |
| 45 | + public function getHTTPMethods($uri): array { |
| 46 | + $tree = $this->server->tree; |
| 47 | + |
| 48 | + if ($tree->nodeExists($uri)) { |
| 49 | + $node = $tree->getNodeForPath($uri); |
| 50 | + if ($node instanceof File) { |
| 51 | + return ['PATCH']; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + return []; |
| 56 | + } |
| 57 | + |
| 58 | + public function getFeatures(): array { |
| 59 | + return ['nextcloud-checksum-update']; |
| 60 | + } |
| 61 | + |
| 62 | + public function httpPatch(RequestInterface $request, ResponseInterface $response) { |
| 63 | + $path = $request->getPath(); |
| 64 | + |
| 65 | + $node = $this->server->tree->getNodeForPath($path); |
| 66 | + if ($node instanceof File) { |
| 67 | + $type = strtolower( |
| 68 | + (string)$request->getHeader('X-Recalculate-Hash') |
| 69 | + ); |
| 70 | + |
| 71 | + $hash = $node->hash($type); |
| 72 | + if ($hash) { |
| 73 | + $checksum = strtoupper($type) . ':' . $hash; |
| 74 | + $node->setChecksum($checksum); |
| 75 | + $response->addHeader('OC-Checksum', $checksum); |
| 76 | + $response->setHeader('Content-Length', '0'); |
| 77 | + $response->setStatus(204); |
| 78 | + |
| 79 | + return false; |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments