Passed
Push — master ( 514058...47cc5a )
by John
16:30 queued 03:00
created

ChecksumUpdatePlugin   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 50
rs 10
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getFeatures() 0 2 1
A getPluginName() 0 2 1
A initialize() 0 3 1
A getHTTPMethods() 0 11 3
A httpPatch() 0 18 3
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * @copyright Copyright (c) 2021 Robin Appelman <[email protected]>
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($path): array {
46
		$tree = $this->server->tree;
47
48
		if ($tree->nodeExists($path)) {
49
			$node = $tree->getNodeForPath($path);
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;
0 ignored issues
show
Bug introduced by
Are you sure $hash of type string|true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

73
				$checksum = strtoupper($type) . ':' . /** @scrutinizer ignore-type */ $hash;
Loading history...
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
}
84