HookConnector.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. Util::connectHook('OC_Filesystem', 'read', $this, 'read');
  64. }
  65. public function write($arguments) {
  66. $node = $this->getNodeForPath($arguments['path']);
  67. $this->root->emit('\OC\Files', 'preWrite', [$node]);
  68. }
  69. public function postWrite($arguments) {
  70. $node = $this->getNodeForPath($arguments['path']);
  71. $this->root->emit('\OC\Files', 'postWrite', [$node]);
  72. }
  73. public function create($arguments) {
  74. $node = $this->getNodeForPath($arguments['path']);
  75. $this->root->emit('\OC\Files', 'preCreate', [$node]);
  76. }
  77. public function postCreate($arguments) {
  78. $node = $this->getNodeForPath($arguments['path']);
  79. $this->root->emit('\OC\Files', 'postCreate', [$node]);
  80. }
  81. public function delete($arguments) {
  82. $node = $this->getNodeForPath($arguments['path']);
  83. $this->deleteMetaCache[$node->getPath()] = $node->getFileInfo();
  84. $this->root->emit('\OC\Files', 'preDelete', [$node]);
  85. }
  86. public function postDelete($arguments) {
  87. $node = $this->getNodeForPath($arguments['path']);
  88. unset($this->deleteMetaCache[$node->getPath()]);
  89. $this->root->emit('\OC\Files', 'postDelete', [$node]);
  90. }
  91. public function touch($arguments) {
  92. $node = $this->getNodeForPath($arguments['path']);
  93. $this->root->emit('\OC\Files', 'preTouch', [$node]);
  94. }
  95. public function postTouch($arguments) {
  96. $node = $this->getNodeForPath($arguments['path']);
  97. $this->root->emit('\OC\Files', 'postTouch', [$node]);
  98. }
  99. public function rename($arguments) {
  100. $source = $this->getNodeForPath($arguments['oldpath']);
  101. $target = $this->getNodeForPath($arguments['newpath']);
  102. $this->root->emit('\OC\Files', 'preRename', [$source, $target]);
  103. }
  104. public function postRename($arguments) {
  105. $source = $this->getNodeForPath($arguments['oldpath']);
  106. $target = $this->getNodeForPath($arguments['newpath']);
  107. $this->root->emit('\OC\Files', 'postRename', [$source, $target]);
  108. }
  109. public function copy($arguments) {
  110. $source = $this->getNodeForPath($arguments['oldpath']);
  111. $target = $this->getNodeForPath($arguments['newpath']);
  112. $this->root->emit('\OC\Files', 'preCopy', [$source, $target]);
  113. }
  114. public function postCopy($arguments) {
  115. $source = $this->getNodeForPath($arguments['oldpath']);
  116. $target = $this->getNodeForPath($arguments['newpath']);
  117. $this->root->emit('\OC\Files', 'postCopy', [$source, $target]);
  118. }
  119. public function read($arguments) {
  120. $node = $this->getNodeForPath($arguments['path']);
  121. $this->root->emit('\OC\Files', 'read', [$node]);
  122. }
  123. private function getNodeForPath($path) {
  124. $info = Filesystem::getView()->getFileInfo($path);
  125. if (!$info) {
  126. $fullPath = Filesystem::getView()->getAbsolutePath($path);
  127. if (isset($this->deleteMetaCache[$fullPath])) {
  128. $info = $this->deleteMetaCache[$fullPath];
  129. } else {
  130. $info = null;
  131. }
  132. if (Filesystem::is_dir($path)) {
  133. return new NonExistingFolder($this->root, $this->view, $fullPath, $info);
  134. } else {
  135. return new NonExistingFile($this->root, $this->view, $fullPath, $info);
  136. }
  137. }
  138. if ($info->getType() === FileInfo::TYPE_FILE) {
  139. return new File($this->root, $this->view, $info->getPath(), $info);
  140. } else {
  141. return new Folder($this->root, $this->view, $info->getPath(), $info);
  142. }
  143. }
  144. }