Skip to content

Commit 47cc5a9

Browse files
authored
Merge pull request #29510 from nextcloud/backport/27378/master
2 parents 5140586 + ff666e6 commit 47cc5a9

7 files changed

Lines changed: 100 additions & 4 deletions

File tree

apps/dav/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@
145145
'OCA\\DAV\\Connector\\Sabre\\BlockLegacyClientPlugin' => $baseDir . '/../lib/Connector/Sabre/BlockLegacyClientPlugin.php',
146146
'OCA\\DAV\\Connector\\Sabre\\CachingTree' => $baseDir . '/../lib/Connector/Sabre/CachingTree.php',
147147
'OCA\\DAV\\Connector\\Sabre\\ChecksumList' => $baseDir . '/../lib/Connector/Sabre/ChecksumList.php',
148+
'OCA\\DAV\\Connector\\Sabre\\ChecksumUpdatePlugin' => $baseDir . '/../lib/Connector/Sabre/ChecksumUpdatePlugin.php',
148149
'OCA\\DAV\\Connector\\Sabre\\CommentPropertiesPlugin' => $baseDir . '/../lib/Connector/Sabre/CommentPropertiesPlugin.php',
149150
'OCA\\DAV\\Connector\\Sabre\\CopyEtagHeaderPlugin' => $baseDir . '/../lib/Connector/Sabre/CopyEtagHeaderPlugin.php',
150151
'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
@@ -160,6 +160,7 @@ class ComposerStaticInitDAV
160160
'OCA\\DAV\\Connector\\Sabre\\BlockLegacyClientPlugin' => __DIR__ . '/..' . '/../lib/Connector/Sabre/BlockLegacyClientPlugin.php',
161161
'OCA\\DAV\\Connector\\Sabre\\CachingTree' => __DIR__ . '/..' . '/../lib/Connector/Sabre/CachingTree.php',
162162
'OCA\\DAV\\Connector\\Sabre\\ChecksumList' => __DIR__ . '/..' . '/../lib/Connector/Sabre/ChecksumList.php',
163+
'OCA\\DAV\\Connector\\Sabre\\ChecksumUpdatePlugin' => __DIR__ . '/..' . '/../lib/Connector/Sabre/ChecksumUpdatePlugin.php',
163164
'OCA\\DAV\\Connector\\Sabre\\CommentPropertiesPlugin' => __DIR__ . '/..' . '/../lib/Connector/Sabre/CommentPropertiesPlugin.php',
164165
'OCA\\DAV\\Connector\\Sabre\\CopyEtagHeaderPlugin' => __DIR__ . '/..' . '/../lib/Connector/Sabre/CopyEtagHeaderPlugin.php',
165166
'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 <[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;
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
@@ -388,11 +388,9 @@ public function put($data) {
388388

389389
if (isset($this->request->server['HTTP_OC_CHECKSUM'])) {
390390
$checksum = trim($this->request->server['HTTP_OC_CHECKSUM']);
391-
$this->fileView->putFileInfo($this->path, ['checksum' => $checksum]);
392-
$this->refreshInfo();
391+
$this->setChecksum($checksum);
393392
} elseif ($this->getChecksum() !== null && $this->getChecksum() !== '') {
394-
$this->fileView->putFileInfo($this->path, ['checksum' => '']);
395-
$this->refreshInfo();
393+
$this->setChecksum('');
396394
}
397395
} catch (StorageNotAvailableException $e) {
398396
throw new ServiceUnavailable($this->l10n->t('Failed to check file size: %1$s', [$e->getMessage()]), 0, $e);
@@ -741,9 +739,18 @@ public function getChecksum() {
741739
return $this->info->getChecksum();
742740
}
743741

742+
public function setChecksum(string $checksum) {
743+
$this->fileView->putFileInfo($this->path, ['checksum' => $checksum]);
744+
$this->refreshInfo();
745+
}
746+
744747
protected function header($string) {
745748
if (!\OC::$CLI) {
746749
\header($string);
747750
}
748751
}
752+
753+
public function hash(string $type) {
754+
return $this->fileView->hash($type, $this->path);
755+
}
749756
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ public function createServer($baseUri,
180180
)
181181
);
182182
$server->addPlugin(new \OCA\DAV\Connector\Sabre\QuotaPlugin($view, true));
183+
$server->addPlugin(new \OCA\DAV\Connector\Sabre\ChecksumUpdatePlugin());
183184

184185
if ($this->userSession->isLoggedIn()) {
185186
$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
@@ -52,6 +52,7 @@
5252
use OCA\DAV\Connector\Sabre\BearerAuth;
5353
use OCA\DAV\Connector\Sabre\BlockLegacyClientPlugin;
5454
use OCA\DAV\Connector\Sabre\CachingTree;
55+
use OCA\DAV\Connector\Sabre\ChecksumUpdatePlugin;
5556
use OCA\DAV\Connector\Sabre\CommentPropertiesPlugin;
5657
use OCA\DAV\Connector\Sabre\CopyEtagHeaderPlugin;
5758
use OCA\DAV\Connector\Sabre\DavAclPlugin;
@@ -253,6 +254,7 @@ public function __construct(IRequest $request, string $baseUri) {
253254
!\OC::$server->getConfig()->getSystemValue('debug', false)
254255
)
255256
);
257+
$this->server->addPlugin(new ChecksumUpdatePlugin());
256258

257259
$this->server->addPlugin(
258260
new \Sabre\DAV\PropertyStorage\Plugin(

lib/private/Files/View.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,6 +1099,7 @@ 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);

0 commit comments

Comments
 (0)