ChunkingPlugin.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017, ownCloud GmbH
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\DAV\Upload;
  26. use OCA\DAV\Connector\Sabre\Directory;
  27. use Sabre\DAV\Exception\BadRequest;
  28. use Sabre\DAV\Exception\NotFound;
  29. use Sabre\DAV\INode;
  30. use Sabre\DAV\Server;
  31. use Sabre\DAV\ServerPlugin;
  32. class ChunkingPlugin extends ServerPlugin {
  33. /** @var Server */
  34. private $server;
  35. /** @var FutureFile */
  36. private $sourceNode;
  37. /**
  38. * @inheritdoc
  39. */
  40. public function initialize(Server $server) {
  41. $server->on('beforeMove', [$this, 'beforeMove']);
  42. $this->server = $server;
  43. }
  44. /**
  45. * @param string $sourcePath source path
  46. * @param string $destination destination path
  47. * @return bool|void
  48. * @throws BadRequest
  49. * @throws NotFound
  50. */
  51. public function beforeMove($sourcePath, $destination) {
  52. $this->sourceNode = $this->server->tree->getNodeForPath($sourcePath);
  53. if (!$this->sourceNode instanceof FutureFile) {
  54. // skip handling as the source is not a chunked FutureFile
  55. return;
  56. }
  57. try {
  58. /** @var INode $destinationNode */
  59. $destinationNode = $this->server->tree->getNodeForPath($destination);
  60. if ($destinationNode instanceof Directory) {
  61. throw new BadRequest("The given destination $destination is a directory.");
  62. }
  63. } catch (NotFound $e) {
  64. // If the destination does not exist yet it's not a directory either ;)
  65. }
  66. $this->verifySize();
  67. return $this->performMove($sourcePath, $destination);
  68. }
  69. /**
  70. * Move handler for future file.
  71. *
  72. * This overrides the default move behavior to prevent Sabre
  73. * to delete the target file before moving. Because deleting would
  74. * lose the file id and metadata.
  75. *
  76. * @param string $path source path
  77. * @param string $destination destination path
  78. * @return bool|void false to stop handling, void to skip this handler
  79. */
  80. public function performMove($path, $destination) {
  81. if (!$this->server->tree->nodeExists($destination)) {
  82. // skip and let the default handler do its work
  83. return;
  84. }
  85. // do a move manually, skipping Sabre's default "delete" for existing nodes
  86. $this->server->tree->move($path, $destination);
  87. // trigger all default events (copied from CorePlugin::move)
  88. $this->server->emit('afterMove', [$path, $destination]);
  89. $this->server->emit('afterUnbind', [$path]);
  90. $this->server->emit('afterBind', [$destination]);
  91. $response = $this->server->httpResponse;
  92. $response->setHeader('Content-Length', '0');
  93. $response->setStatus(204);
  94. return false;
  95. }
  96. /**
  97. * @throws BadRequest
  98. */
  99. private function verifySize() {
  100. $expectedSize = $this->server->httpRequest->getHeader('OC-Total-Length');
  101. if ($expectedSize === null) {
  102. return;
  103. }
  104. $actualSize = $this->sourceNode->getSize();
  105. // casted to string because cast to float cause equality for non equal numbers
  106. // and integer has the problem of limited size on 32 bit systems
  107. if ((string)$expectedSize !== (string)$actualSize) {
  108. throw new BadRequest("Chunks on server do not sum up to $expectedSize but to $actualSize bytes");
  109. }
  110. }
  111. }