ObjectTree.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV\Connector\Sabre;
  8. use OC\Files\FileInfo;
  9. use OC\Files\Storage\FailedStorage;
  10. use OCA\DAV\Connector\Sabre\Exception\FileLocked;
  11. use OCA\DAV\Connector\Sabre\Exception\Forbidden;
  12. use OCA\DAV\Connector\Sabre\Exception\InvalidPath;
  13. use OCP\Files\ForbiddenException;
  14. use OCP\Files\StorageInvalidException;
  15. use OCP\Files\StorageNotAvailableException;
  16. use OCP\Lock\LockedException;
  17. class ObjectTree extends CachingTree {
  18. /**
  19. * @var \OC\Files\View
  20. */
  21. protected $fileView;
  22. /**
  23. * @var \OCP\Files\Mount\IMountManager
  24. */
  25. protected $mountManager;
  26. /**
  27. * Creates the object
  28. */
  29. public function __construct() {
  30. }
  31. /**
  32. * @param \Sabre\DAV\INode $rootNode
  33. * @param \OC\Files\View $view
  34. * @param \OCP\Files\Mount\IMountManager $mountManager
  35. */
  36. public function init(\Sabre\DAV\INode $rootNode, \OC\Files\View $view, \OCP\Files\Mount\IMountManager $mountManager) {
  37. $this->rootNode = $rootNode;
  38. $this->fileView = $view;
  39. $this->mountManager = $mountManager;
  40. }
  41. /**
  42. * Returns the INode object for the requested path
  43. *
  44. * @param string $path
  45. * @return \Sabre\DAV\INode
  46. * @throws InvalidPath
  47. * @throws \Sabre\DAV\Exception\Locked
  48. * @throws \Sabre\DAV\Exception\NotFound
  49. * @throws \Sabre\DAV\Exception\ServiceUnavailable
  50. */
  51. public function getNodeForPath($path) {
  52. if (!$this->fileView) {
  53. throw new \Sabre\DAV\Exception\ServiceUnavailable('filesystem not setup');
  54. }
  55. $path = trim($path, '/');
  56. if (isset($this->cache[$path])) {
  57. return $this->cache[$path];
  58. }
  59. if ($path) {
  60. try {
  61. $this->fileView->verifyPath($path, basename($path));
  62. } catch (\OCP\Files\InvalidPathException $ex) {
  63. throw new InvalidPath($ex->getMessage());
  64. }
  65. }
  66. // Is it the root node?
  67. if (!strlen($path)) {
  68. return $this->rootNode;
  69. }
  70. if (pathinfo($path, PATHINFO_EXTENSION) === 'part') {
  71. // read from storage
  72. $absPath = $this->fileView->getAbsolutePath($path);
  73. $mount = $this->fileView->getMount($path);
  74. $storage = $mount->getStorage();
  75. $internalPath = $mount->getInternalPath($absPath);
  76. if ($storage && $storage->file_exists($internalPath)) {
  77. /**
  78. * @var \OC\Files\Storage\Storage $storage
  79. */
  80. // get data directly
  81. $data = $storage->getMetaData($internalPath);
  82. $info = new FileInfo($absPath, $storage, $internalPath, $data, $mount);
  83. } else {
  84. $info = null;
  85. }
  86. } else {
  87. // read from cache
  88. try {
  89. $info = $this->fileView->getFileInfo($path);
  90. if ($info instanceof \OCP\Files\FileInfo && $info->getStorage()->instanceOfStorage(FailedStorage::class)) {
  91. throw new StorageNotAvailableException();
  92. }
  93. } catch (StorageNotAvailableException $e) {
  94. throw new \Sabre\DAV\Exception\ServiceUnavailable('Storage is temporarily not available', 0, $e);
  95. } catch (StorageInvalidException $e) {
  96. throw new \Sabre\DAV\Exception\NotFound('Storage ' . $path . ' is invalid');
  97. } catch (LockedException $e) {
  98. throw new \Sabre\DAV\Exception\Locked();
  99. } catch (ForbiddenException $e) {
  100. throw new \Sabre\DAV\Exception\Forbidden();
  101. }
  102. }
  103. if (!$info) {
  104. throw new \Sabre\DAV\Exception\NotFound('File with name ' . $path . ' could not be located');
  105. }
  106. if ($info->getType() === 'dir') {
  107. $node = new \OCA\DAV\Connector\Sabre\Directory($this->fileView, $info, $this);
  108. } else {
  109. $node = new \OCA\DAV\Connector\Sabre\File($this->fileView, $info);
  110. }
  111. $this->cache[$path] = $node;
  112. return $node;
  113. }
  114. /**
  115. * Copies a file or directory.
  116. *
  117. * This method must work recursively and delete the destination
  118. * if it exists
  119. *
  120. * @param string $sourcePath
  121. * @param string $destinationPath
  122. * @throws FileLocked
  123. * @throws Forbidden
  124. * @throws InvalidPath
  125. * @throws \Exception
  126. * @throws \Sabre\DAV\Exception\Forbidden
  127. * @throws \Sabre\DAV\Exception\Locked
  128. * @throws \Sabre\DAV\Exception\NotFound
  129. * @throws \Sabre\DAV\Exception\ServiceUnavailable
  130. * @return void
  131. */
  132. public function copy($sourcePath, $destinationPath) {
  133. if (!$this->fileView) {
  134. throw new \Sabre\DAV\Exception\ServiceUnavailable('filesystem not setup');
  135. }
  136. $info = $this->fileView->getFileInfo(dirname($destinationPath));
  137. if ($this->fileView->file_exists($destinationPath)) {
  138. $destinationPermission = $info && $info->isUpdateable();
  139. } else {
  140. $destinationPermission = $info && $info->isCreatable();
  141. }
  142. if (!$destinationPermission) {
  143. throw new Forbidden('No permissions to copy object.');
  144. }
  145. // this will trigger existence check
  146. $this->getNodeForPath($sourcePath);
  147. [$destinationDir, $destinationName] = \Sabre\Uri\split($destinationPath);
  148. try {
  149. $this->fileView->verifyPath($destinationDir, $destinationName);
  150. } catch (\OCP\Files\InvalidPathException $ex) {
  151. throw new InvalidPath($ex->getMessage());
  152. }
  153. try {
  154. $this->fileView->copy($sourcePath, $destinationPath);
  155. } catch (StorageNotAvailableException $e) {
  156. throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage());
  157. } catch (ForbiddenException $ex) {
  158. throw new Forbidden($ex->getMessage(), $ex->getRetry());
  159. } catch (LockedException $e) {
  160. throw new FileLocked($e->getMessage(), $e->getCode(), $e);
  161. }
  162. [$destinationDir,] = \Sabre\Uri\split($destinationPath);
  163. $this->markDirty($destinationDir);
  164. }
  165. }