ChecksumUpdatePlugin.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\DAV\Connector\Sabre;
  8. use OCP\AppFramework\Http;
  9. use Sabre\DAV\Server;
  10. use Sabre\DAV\ServerPlugin;
  11. use Sabre\HTTP\RequestInterface;
  12. use Sabre\HTTP\ResponseInterface;
  13. class ChecksumUpdatePlugin extends ServerPlugin {
  14. protected ?Server $server = null;
  15. public function initialize(Server $server) {
  16. $this->server = $server;
  17. $server->on('method:PATCH', [$this, 'httpPatch']);
  18. }
  19. public function getPluginName(): string {
  20. return 'checksumupdate';
  21. }
  22. /** @return string[] */
  23. public function getHTTPMethods($path): array {
  24. $tree = $this->server->tree;
  25. if ($tree->nodeExists($path)) {
  26. $node = $tree->getNodeForPath($path);
  27. if ($node instanceof File) {
  28. return ['PATCH'];
  29. }
  30. }
  31. return [];
  32. }
  33. /** @return string[] */
  34. public function getFeatures(): array {
  35. return ['nextcloud-checksum-update'];
  36. }
  37. public function httpPatch(RequestInterface $request, ResponseInterface $response) {
  38. $path = $request->getPath();
  39. $node = $this->server->tree->getNodeForPath($path);
  40. if ($node instanceof File) {
  41. $type = strtolower(
  42. (string)$request->getHeader('X-Recalculate-Hash')
  43. );
  44. $hash = $node->hash($type);
  45. if ($hash) {
  46. $checksum = strtoupper($type) . ':' . $hash;
  47. $node->setChecksum($checksum);
  48. $response->addHeader('OC-Checksum', $checksum);
  49. $response->setHeader('Content-Length', '0');
  50. $response->setStatus(Http::STATUS_NO_CONTENT);
  51. return false;
  52. }
  53. }
  54. }
  55. }