You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ChecksumUpdatePlugin.php 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2021 Robin Appelman <robin@icewind.nl>
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace OCA\DAV\Connector\Sabre;
  23. use Sabre\DAV\Server;
  24. use Sabre\DAV\ServerPlugin;
  25. use Sabre\HTTP\RequestInterface;
  26. use Sabre\HTTP\ResponseInterface;
  27. class ChecksumUpdatePlugin extends ServerPlugin {
  28. protected ?Server $server = null;
  29. public function initialize(Server $server) {
  30. $this->server = $server;
  31. $server->on('method:PATCH', [$this, 'httpPatch']);
  32. }
  33. public function getPluginName(): string {
  34. return 'checksumupdate';
  35. }
  36. /** @return string[] */
  37. public function getHTTPMethods($path): array {
  38. $tree = $this->server->tree;
  39. if ($tree->nodeExists($path)) {
  40. $node = $tree->getNodeForPath($path);
  41. if ($node instanceof File) {
  42. return ['PATCH'];
  43. }
  44. }
  45. return [];
  46. }
  47. /** @return string[] */
  48. public function getFeatures(): array {
  49. return ['nextcloud-checksum-update'];
  50. }
  51. public function httpPatch(RequestInterface $request, ResponseInterface $response) {
  52. $path = $request->getPath();
  53. $node = $this->server->tree->getNodeForPath($path);
  54. if ($node instanceof File) {
  55. $type = strtolower(
  56. (string)$request->getHeader('X-Recalculate-Hash')
  57. );
  58. $hash = $node->hash($type);
  59. if ($hash) {
  60. $checksum = strtoupper($type) . ':' . $hash;
  61. $node->setChecksum($checksum);
  62. $response->addHeader('OC-Checksum', $checksum);
  63. $response->setHeader('Content-Length', '0');
  64. $response->setStatus(204);
  65. return false;
  66. }
  67. }
  68. }
  69. }