ChunkingPlugin.php 3.1 KB

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