Skip to content

Commit e822be6

Browse files
committed
Add dav plugin to trigger recalculating of checksums
Signed-off-by: Robin Appelman <robin@icewind.nl>
1 parent dfbac05 commit e822be6

File tree

9 files changed

+106
-7
lines changed

9 files changed

+106
-7
lines changed

apps/dav/composer/composer/ClassLoader.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ public function unregister()
338338
* Loads the given class or interface.
339339
*
340340
* @param string $class The name of the class
341-
* @return bool|null True if loaded, null otherwise
341+
* @return true|null True if loaded, null otherwise
342342
*/
343343
public function loadClass($class)
344344
{
@@ -347,6 +347,8 @@ public function loadClass($class)
347347

348348
return true;
349349
}
350+
351+
return null;
350352
}
351353

352354
/**

apps/dav/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@
140140
'OCA\\DAV\\Connector\\Sabre\\BlockLegacyClientPlugin' => $baseDir . '/../lib/Connector/Sabre/BlockLegacyClientPlugin.php',
141141
'OCA\\DAV\\Connector\\Sabre\\CachingTree' => $baseDir . '/../lib/Connector/Sabre/CachingTree.php',
142142
'OCA\\DAV\\Connector\\Sabre\\ChecksumList' => $baseDir . '/../lib/Connector/Sabre/ChecksumList.php',
143+
'OCA\\DAV\\Connector\\Sabre\\ChecksumUpdatePlugin' => $baseDir . '/../lib/Connector/Sabre/ChecksumUpdatePlugin.php',
143144
'OCA\\DAV\\Connector\\Sabre\\CommentPropertiesPlugin' => $baseDir . '/../lib/Connector/Sabre/CommentPropertiesPlugin.php',
144145
'OCA\\DAV\\Connector\\Sabre\\CopyEtagHeaderPlugin' => $baseDir . '/../lib/Connector/Sabre/CopyEtagHeaderPlugin.php',
145146
'OCA\\DAV\\Connector\\Sabre\\DavAclPlugin' => $baseDir . '/../lib/Connector/Sabre/DavAclPlugin.php',

apps/dav/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ class ComposerStaticInitDAV
155155
'OCA\\DAV\\Connector\\Sabre\\BlockLegacyClientPlugin' => __DIR__ . '/..' . '/../lib/Connector/Sabre/BlockLegacyClientPlugin.php',
156156
'OCA\\DAV\\Connector\\Sabre\\CachingTree' => __DIR__ . '/..' . '/../lib/Connector/Sabre/CachingTree.php',
157157
'OCA\\DAV\\Connector\\Sabre\\ChecksumList' => __DIR__ . '/..' . '/../lib/Connector/Sabre/ChecksumList.php',
158+
'OCA\\DAV\\Connector\\Sabre\\ChecksumUpdatePlugin' => __DIR__ . '/..' . '/../lib/Connector/Sabre/ChecksumUpdatePlugin.php',
158159
'OCA\\DAV\\Connector\\Sabre\\CommentPropertiesPlugin' => __DIR__ . '/..' . '/../lib/Connector/Sabre/CommentPropertiesPlugin.php',
159160
'OCA\\DAV\\Connector\\Sabre\\CopyEtagHeaderPlugin' => __DIR__ . '/..' . '/../lib/Connector/Sabre/CopyEtagHeaderPlugin.php',
160161
'OCA\\DAV\\Connector\\Sabre\\DavAclPlugin' => __DIR__ . '/..' . '/../lib/Connector/Sabre/DavAclPlugin.php',
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
}

apps/dav/lib/Connector/Sabre/File.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -343,11 +343,9 @@ public function put($data) {
343343

344344
if (isset($this->request->server['HTTP_OC_CHECKSUM'])) {
345345
$checksum = trim($this->request->server['HTTP_OC_CHECKSUM']);
346-
$this->fileView->putFileInfo($this->path, ['checksum' => $checksum]);
347-
$this->refreshInfo();
346+
$this->setChecksum($checksum);
348347
} elseif ($this->getChecksum() !== null && $this->getChecksum() !== '') {
349-
$this->fileView->putFileInfo($this->path, ['checksum' => '']);
350-
$this->refreshInfo();
348+
$this->setChecksum('');
351349
}
352350
} catch (StorageNotAvailableException $e) {
353351
throw new ServiceUnavailable("Failed to check file size: " . $e->getMessage(), 0, $e);
@@ -688,9 +686,18 @@ public function getChecksum() {
688686
return $this->info->getChecksum();
689687
}
690688

689+
public function setChecksum(string $checksum) {
690+
$this->fileView->putFileInfo($this->path, ['checksum' => $checksum]);
691+
$this->refreshInfo();
692+
}
693+
691694
protected function header($string) {
692695
if (!\OC::$CLI) {
693696
\header($string);
694697
}
695698
}
699+
700+
public function hash(string $type) {
701+
return $this->fileView->hash($type, $this->path);
702+
}
696703
}

apps/dav/lib/Connector/Sabre/Node.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
use OC\Files\View;
4040
use OCA\DAV\Connector\Sabre\Exception\InvalidPath;
4141
use OCP\Files\FileInfo;
42+
use OCP\Files\IRootFolder;
4243
use OCP\Files\StorageNotAvailableException;
4344
use OCP\Share\IShare;
4445
use OCP\Share\Exceptions\ShareNotFound;

apps/dav/lib/Connector/Sabre/ServerFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ public function createServer($baseUri,
176176
)
177177
);
178178
$server->addPlugin(new \OCA\DAV\Connector\Sabre\QuotaPlugin($view, true));
179+
$server->addPlugin(new \OCA\DAV\Connector\Sabre\ChecksumUpdatePlugin());
179180

180181
if ($this->userSession->isLoggedIn()) {
181182
$server->addPlugin(new \OCA\DAV\Connector\Sabre\TagsPlugin($objectTree, $this->tagManager));

apps/dav/lib/Server.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
use OCA\DAV\Connector\Sabre\BearerAuth;
4747
use OCA\DAV\Connector\Sabre\BlockLegacyClientPlugin;
4848
use OCA\DAV\Connector\Sabre\CachingTree;
49+
use OCA\DAV\Connector\Sabre\ChecksumUpdatePlugin;
4950
use OCA\DAV\Connector\Sabre\CommentPropertiesPlugin;
5051
use OCA\DAV\Connector\Sabre\CopyEtagHeaderPlugin;
5152
use OCA\DAV\Connector\Sabre\DavAclPlugin;
@@ -242,6 +243,7 @@ public function __construct(IRequest $request, $baseUri) {
242243
!\OC::$server->getConfig()->getSystemValue('debug', false)
243244
)
244245
);
246+
$this->server->addPlugin(new ChecksumUpdatePlugin());
245247

246248
$this->server->addPlugin(
247249
new \Sabre\DAV\PropertyStorage\Plugin(

lib/private/Files/View.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,7 +1082,7 @@ public function getMimeType($path) {
10821082
* @param string $type
10831083
* @param string $path
10841084
* @param bool $raw
1085-
* @return bool|null|string
1085+
* @return bool|string
10861086
*/
10871087
public function hash($type, $path, $raw = false) {
10881088
$postFix = (substr($path, -1) === '/') ? '/' : '';
@@ -1099,12 +1099,13 @@ public function hash($type, $path, $raw = false) {
10991099
[Filesystem::signal_param_path => $this->getHookPath($path)]
11001100
);
11011101
}
1102+
/** @var Storage|null $storage */
11021103
[$storage, $internalPath] = Filesystem::resolvePath($absolutePath . $postFix);
11031104
if ($storage) {
11041105
return $storage->hash($type, $internalPath, $raw);
11051106
}
11061107
}
1107-
return null;
1108+
return false;
11081109
}
11091110

11101111
/**

0 commit comments

Comments
 (0)