You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
4.9 KiB

10 years ago
10 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Robin Appelman <robin@icewind.nl>
  6. *
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OC\Files\Node;
  23. use OCP\Files\FileInfo;
  24. use OC\Files\Filesystem;
  25. use OC\Files\View;
  26. use OCP\Util;
  27. class HookConnector {
  28. /**
  29. * @var Root
  30. */
  31. private $root;
  32. /**
  33. * @var View
  34. */
  35. private $view;
  36. /**
  37. * @var FileInfo[]
  38. */
  39. private $deleteMetaCache = [];
  40. /**
  41. * HookConnector constructor.
  42. *
  43. * @param Root $root
  44. * @param View $view
  45. */
  46. public function __construct(Root $root, View $view) {
  47. $this->root = $root;
  48. $this->view = $view;
  49. }
  50. public function viewToNode() {
  51. Util::connectHook('OC_Filesystem', 'write', $this, 'write');
  52. Util::connectHook('OC_Filesystem', 'post_write', $this, 'postWrite');
  53. Util::connectHook('OC_Filesystem', 'create', $this, 'create');
  54. Util::connectHook('OC_Filesystem', 'post_create', $this, 'postCreate');
  55. Util::connectHook('OC_Filesystem', 'delete', $this, 'delete');
  56. Util::connectHook('OC_Filesystem', 'post_delete', $this, 'postDelete');
  57. Util::connectHook('OC_Filesystem', 'rename', $this, 'rename');
  58. Util::connectHook('OC_Filesystem', 'post_rename', $this, 'postRename');
  59. Util::connectHook('OC_Filesystem', 'copy', $this, 'copy');
  60. Util::connectHook('OC_Filesystem', 'post_copy', $this, 'postCopy');
  61. Util::connectHook('OC_Filesystem', 'touch', $this, 'touch');
  62. Util::connectHook('OC_Filesystem', 'post_touch', $this, 'postTouch');
  63. }
  64. public function write($arguments) {
  65. $node = $this->getNodeForPath($arguments['path']);
  66. $this->root->emit('\OC\Files', 'preWrite', [$node]);
  67. }
  68. public function postWrite($arguments) {
  69. $node = $this->getNodeForPath($arguments['path']);
  70. $this->root->emit('\OC\Files', 'postWrite', [$node]);
  71. }
  72. public function create($arguments) {
  73. $node = $this->getNodeForPath($arguments['path']);
  74. $this->root->emit('\OC\Files', 'preCreate', [$node]);
  75. }
  76. public function postCreate($arguments) {
  77. $node = $this->getNodeForPath($arguments['path']);
  78. $this->root->emit('\OC\Files', 'postCreate', [$node]);
  79. }
  80. public function delete($arguments) {
  81. $node = $this->getNodeForPath($arguments['path']);
  82. $this->deleteMetaCache[$node->getPath()] = $node->getFileInfo();
  83. $this->root->emit('\OC\Files', 'preDelete', [$node]);
  84. }
  85. public function postDelete($arguments) {
  86. $node = $this->getNodeForPath($arguments['path']);
  87. unset($this->deleteMetaCache[$node->getPath()]);
  88. $this->root->emit('\OC\Files', 'postDelete', [$node]);
  89. }
  90. public function touch($arguments) {
  91. $node = $this->getNodeForPath($arguments['path']);
  92. $this->root->emit('\OC\Files', 'preTouch', [$node]);
  93. }
  94. public function postTouch($arguments) {
  95. $node = $this->getNodeForPath($arguments['path']);
  96. $this->root->emit('\OC\Files', 'postTouch', [$node]);
  97. }
  98. public function rename($arguments) {
  99. $source = $this->getNodeForPath($arguments['oldpath']);
  100. $target = $this->getNodeForPath($arguments['newpath']);
  101. $this->root->emit('\OC\Files', 'preRename', [$source, $target]);
  102. }
  103. public function postRename($arguments) {
  104. $source = $this->getNodeForPath($arguments['oldpath']);
  105. $target = $this->getNodeForPath($arguments['newpath']);
  106. $this->root->emit('\OC\Files', 'postRename', [$source, $target]);
  107. }
  108. public function copy($arguments) {
  109. $source = $this->getNodeForPath($arguments['oldpath']);
  110. $target = $this->getNodeForPath($arguments['newpath']);
  111. $this->root->emit('\OC\Files', 'preCopy', [$source, $target]);
  112. }
  113. public function postCopy($arguments) {
  114. $source = $this->getNodeForPath($arguments['oldpath']);
  115. $target = $this->getNodeForPath($arguments['newpath']);
  116. $this->root->emit('\OC\Files', 'postCopy', [$source, $target]);
  117. }
  118. private function getNodeForPath($path) {
  119. $info = Filesystem::getView()->getFileInfo($path);
  120. if (!$info) {
  121. $fullPath = Filesystem::getView()->getAbsolutePath($path);
  122. if (isset($this->deleteMetaCache[$fullPath])) {
  123. $info = $this->deleteMetaCache[$fullPath];
  124. } else {
  125. $info = null;
  126. }
  127. if (Filesystem::is_dir($path)) {
  128. return new NonExistingFolder($this->root, $this->view, $fullPath, $info);
  129. } else {
  130. return new NonExistingFile($this->root, $this->view, $fullPath, $info);
  131. }
  132. }
  133. if ($info->getType() === FileInfo::TYPE_FILE) {
  134. return new File($this->root, $this->view, $info->getPath(), $info);
  135. } else {
  136. return new Folder($this->root, $this->view, $info->getPath(), $info);
  137. }
  138. }
  139. }