ChunkingPlugin.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2018-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2017 ownCloud GmbH
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV\Upload;
  8. use OCA\DAV\Connector\Sabre\Directory;
  9. use OCA\DAV\Connector\Sabre\Exception\Forbidden;
  10. use OCP\AppFramework\Http;
  11. use Sabre\DAV\Exception\BadRequest;
  12. use Sabre\DAV\Exception\NotFound;
  13. use Sabre\DAV\INode;
  14. use Sabre\DAV\Server;
  15. use Sabre\DAV\ServerPlugin;
  16. class ChunkingPlugin extends ServerPlugin {
  17. /** @var Server */
  18. private $server;
  19. /** @var FutureFile */
  20. private $sourceNode;
  21. /**
  22. * @inheritdoc
  23. */
  24. public function initialize(Server $server) {
  25. $server->on('beforeMove', [$this, 'beforeMove']);
  26. $this->server = $server;
  27. }
  28. /**
  29. * @param string $sourcePath source path
  30. * @param string $destination destination path
  31. * @return bool|void
  32. * @throws BadRequest
  33. * @throws NotFound
  34. */
  35. public function beforeMove($sourcePath, $destination) {
  36. $this->sourceNode = $this->server->tree->getNodeForPath($sourcePath);
  37. if (!$this->sourceNode instanceof FutureFile) {
  38. // skip handling as the source is not a chunked FutureFile
  39. return;
  40. }
  41. try {
  42. /** @var INode $destinationNode */
  43. $destinationNode = $this->server->tree->getNodeForPath($destination);
  44. if ($destinationNode instanceof Directory) {
  45. throw new BadRequest("The given destination $destination is a directory.");
  46. }
  47. } catch (NotFound $e) {
  48. // If the destination does not exist yet it's not a directory either ;)
  49. }
  50. $this->verifySize();
  51. return $this->performMove($sourcePath, $destination);
  52. }
  53. /**
  54. * Move handler for future file.
  55. *
  56. * This overrides the default move behavior to prevent Sabre
  57. * to delete the target file before moving. Because deleting would
  58. * lose the file id and metadata.
  59. *
  60. * @param string $path source path
  61. * @param string $destination destination path
  62. * @return bool|void false to stop handling, void to skip this handler
  63. */
  64. public function performMove($path, $destination) {
  65. $fileExists = $this->server->tree->nodeExists($destination);
  66. // do a move manually, skipping Sabre's default "delete" for existing nodes
  67. try {
  68. $this->server->tree->move($path, $destination);
  69. } catch (Forbidden $e) {
  70. $sourceNode = $this->server->tree->getNodeForPath($path);
  71. if ($sourceNode instanceof FutureFile) {
  72. $sourceNode->delete();
  73. }
  74. throw $e;
  75. }
  76. // trigger all default events (copied from CorePlugin::move)
  77. $this->server->emit('afterMove', [$path, $destination]);
  78. $this->server->emit('afterUnbind', [$path]);
  79. $this->server->emit('afterBind', [$destination]);
  80. $response = $this->server->httpResponse;
  81. $response->setHeader('Content-Length', '0');
  82. $response->setStatus($fileExists ? Http::STATUS_NO_CONTENT : Http::STATUS_CREATED);
  83. return false;
  84. }
  85. /**
  86. * @throws BadRequest
  87. */
  88. private function verifySize() {
  89. $expectedSize = $this->server->httpRequest->getHeader('OC-Total-Length');
  90. if ($expectedSize === null) {
  91. return;
  92. }
  93. $actualSize = $this->sourceNode->getSize();
  94. // casted to string because cast to float cause equality for non equal numbers
  95. // and integer has the problem of limited size on 32 bit systems
  96. if ((string)$expectedSize !== (string)$actualSize) {
  97. throw new BadRequest("Chunks on server do not sum up to $expectedSize but to $actualSize bytes");
  98. }
  99. }
  100. }