ChecksumUpdatePlugin.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 Sabre\DAV\Server;
  9. use Sabre\DAV\ServerPlugin;
  10. use Sabre\HTTP\RequestInterface;
  11. use Sabre\HTTP\ResponseInterface;
  12. class ChecksumUpdatePlugin extends ServerPlugin {
  13. protected ?Server $server = null;
  14. public function initialize(Server $server) {
  15. $this->server = $server;
  16. $server->on('method:PATCH', [$this, 'httpPatch']);
  17. }
  18. public function getPluginName(): string {
  19. return 'checksumupdate';
  20. }
  21. /** @return string[] */
  22. public function getHTTPMethods($path): array {
  23. $tree = $this->server->tree;
  24. if ($tree->nodeExists($path)) {
  25. $node = $tree->getNodeForPath($path);
  26. if ($node instanceof File) {
  27. return ['PATCH'];
  28. }
  29. }
  30. return [];
  31. }
  32. /** @return string[] */
  33. public function getFeatures(): array {
  34. return ['nextcloud-checksum-update'];
  35. }
  36. public function httpPatch(RequestInterface $request, ResponseInterface $response) {
  37. $path = $request->getPath();
  38. $node = $this->server->tree->getNodeForPath($path);
  39. if ($node instanceof File) {
  40. $type = strtolower(
  41. (string)$request->getHeader('X-Recalculate-Hash')
  42. );
  43. $hash = $node->hash($type);
  44. if ($hash) {
  45. $checksum = strtoupper($type) . ':' . $hash;
  46. $node->setChecksum($checksum);
  47. $response->addHeader('OC-Checksum', $checksum);
  48. $response->setHeader('Content-Length', '0');
  49. $response->setStatus(204);
  50. return false;
  51. }
  52. }
  53. }
  54. }