ChunkingPlugin.php 3.1 KB

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