HookConnector.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC\Files\Node;
  26. use OC\Files\Filesystem;
  27. use OC\Files\View;
  28. use OCP\EventDispatcher\GenericEvent;
  29. use OCP\EventDispatcher\IEventDispatcher;
  30. use OCP\Files\Events\Node\BeforeNodeCopiedEvent;
  31. use OCP\Files\Events\Node\BeforeNodeCreatedEvent;
  32. use OCP\Files\Events\Node\BeforeNodeDeletedEvent;
  33. use OCP\Files\Events\Node\BeforeNodeReadEvent;
  34. use OCP\Files\Events\Node\BeforeNodeRenamedEvent;
  35. use OCP\Files\Events\Node\BeforeNodeTouchedEvent;
  36. use OCP\Files\Events\Node\BeforeNodeWrittenEvent;
  37. use OCP\Files\Events\Node\NodeCopiedEvent;
  38. use OCP\Files\Events\Node\NodeCreatedEvent;
  39. use OCP\Files\Events\Node\NodeDeletedEvent;
  40. use OCP\Files\Events\Node\NodeRenamedEvent;
  41. use OCP\Files\Events\Node\NodeTouchedEvent;
  42. use OCP\Files\Events\Node\NodeWrittenEvent;
  43. use OCP\Files\FileInfo;
  44. use OCP\Files\IRootFolder;
  45. use OCP\Util;
  46. class HookConnector {
  47. /** @var IRootFolder */
  48. private $root;
  49. /** @var View */
  50. private $view;
  51. /** @var FileInfo[] */
  52. private $deleteMetaCache = [];
  53. /** @var IEventDispatcher */
  54. private $dispatcher;
  55. public function __construct(
  56. IRootFolder $root,
  57. View $view,
  58. IEventDispatcher $dispatcher) {
  59. $this->root = $root;
  60. $this->view = $view;
  61. $this->dispatcher = $dispatcher;
  62. }
  63. public function viewToNode() {
  64. Util::connectHook('OC_Filesystem', 'write', $this, 'write');
  65. Util::connectHook('OC_Filesystem', 'post_write', $this, 'postWrite');
  66. Util::connectHook('OC_Filesystem', 'create', $this, 'create');
  67. Util::connectHook('OC_Filesystem', 'post_create', $this, 'postCreate');
  68. Util::connectHook('OC_Filesystem', 'delete', $this, 'delete');
  69. Util::connectHook('OC_Filesystem', 'post_delete', $this, 'postDelete');
  70. Util::connectHook('OC_Filesystem', 'rename', $this, 'rename');
  71. Util::connectHook('OC_Filesystem', 'post_rename', $this, 'postRename');
  72. Util::connectHook('OC_Filesystem', 'copy', $this, 'copy');
  73. Util::connectHook('OC_Filesystem', 'post_copy', $this, 'postCopy');
  74. Util::connectHook('OC_Filesystem', 'touch', $this, 'touch');
  75. Util::connectHook('OC_Filesystem', 'post_touch', $this, 'postTouch');
  76. Util::connectHook('OC_Filesystem', 'read', $this, 'read');
  77. }
  78. public function write($arguments) {
  79. $node = $this->getNodeForPath($arguments['path']);
  80. $this->root->emit('\OC\Files', 'preWrite', [$node]);
  81. $this->dispatcher->dispatch('\OCP\Files::preWrite', new GenericEvent($node));
  82. $event = new BeforeNodeWrittenEvent($node);
  83. $this->dispatcher->dispatchTyped($event);
  84. }
  85. public function postWrite($arguments) {
  86. $node = $this->getNodeForPath($arguments['path']);
  87. $this->root->emit('\OC\Files', 'postWrite', [$node]);
  88. $this->dispatcher->dispatch('\OCP\Files::postWrite', new GenericEvent($node));
  89. $event = new NodeWrittenEvent($node);
  90. $this->dispatcher->dispatchTyped($event);
  91. }
  92. public function create($arguments) {
  93. $node = $this->getNodeForPath($arguments['path']);
  94. $this->root->emit('\OC\Files', 'preCreate', [$node]);
  95. $this->dispatcher->dispatch('\OCP\Files::preCreate', new GenericEvent($node));
  96. $event = new BeforeNodeCreatedEvent($node);
  97. $this->dispatcher->dispatchTyped($event);
  98. }
  99. public function postCreate($arguments) {
  100. $node = $this->getNodeForPath($arguments['path']);
  101. $this->root->emit('\OC\Files', 'postCreate', [$node]);
  102. $this->dispatcher->dispatch('\OCP\Files::postCreate', new GenericEvent($node));
  103. $event = new NodeCreatedEvent($node);
  104. $this->dispatcher->dispatchTyped($event);
  105. }
  106. public function delete($arguments) {
  107. $node = $this->getNodeForPath($arguments['path']);
  108. $this->deleteMetaCache[$node->getPath()] = $node->getFileInfo();
  109. $this->root->emit('\OC\Files', 'preDelete', [$node]);
  110. $this->dispatcher->dispatch('\OCP\Files::preDelete', new GenericEvent($node));
  111. $event = new BeforeNodeDeletedEvent($node);
  112. $this->dispatcher->dispatchTyped($event);
  113. }
  114. public function postDelete($arguments) {
  115. $node = $this->getNodeForPath($arguments['path']);
  116. unset($this->deleteMetaCache[$node->getPath()]);
  117. $this->root->emit('\OC\Files', 'postDelete', [$node]);
  118. $this->dispatcher->dispatch('\OCP\Files::postDelete', new GenericEvent($node));
  119. $event = new NodeDeletedEvent($node);
  120. $this->dispatcher->dispatchTyped($event);
  121. }
  122. public function touch($arguments) {
  123. $node = $this->getNodeForPath($arguments['path']);
  124. $this->root->emit('\OC\Files', 'preTouch', [$node]);
  125. $this->dispatcher->dispatch('\OCP\Files::preTouch', new GenericEvent($node));
  126. $event = new BeforeNodeTouchedEvent($node);
  127. $this->dispatcher->dispatchTyped($event);
  128. }
  129. public function postTouch($arguments) {
  130. $node = $this->getNodeForPath($arguments['path']);
  131. $this->root->emit('\OC\Files', 'postTouch', [$node]);
  132. $this->dispatcher->dispatch('\OCP\Files::postTouch', new GenericEvent($node));
  133. $event = new NodeTouchedEvent($node);
  134. $this->dispatcher->dispatchTyped($event);
  135. }
  136. public function rename($arguments) {
  137. $source = $this->getNodeForPath($arguments['oldpath']);
  138. $target = $this->getNodeForPath($arguments['newpath']);
  139. $this->root->emit('\OC\Files', 'preRename', [$source, $target]);
  140. $this->dispatcher->dispatch('\OCP\Files::preRename', new GenericEvent([$source, $target]));
  141. $event = new BeforeNodeRenamedEvent($source, $target);
  142. $this->dispatcher->dispatchTyped($event);
  143. }
  144. public function postRename($arguments) {
  145. $source = $this->getNodeForPath($arguments['oldpath']);
  146. $target = $this->getNodeForPath($arguments['newpath']);
  147. $this->root->emit('\OC\Files', 'postRename', [$source, $target]);
  148. $this->dispatcher->dispatch('\OCP\Files::postRename', new GenericEvent([$source, $target]));
  149. $event = new NodeRenamedEvent($source, $target);
  150. $this->dispatcher->dispatchTyped($event);
  151. }
  152. public function copy($arguments) {
  153. $source = $this->getNodeForPath($arguments['oldpath']);
  154. $target = $this->getNodeForPath($arguments['newpath']);
  155. $this->root->emit('\OC\Files', 'preCopy', [$source, $target]);
  156. $this->dispatcher->dispatch('\OCP\Files::preCopy', new GenericEvent([$source, $target]));
  157. $event = new BeforeNodeCopiedEvent($source, $target);
  158. $this->dispatcher->dispatchTyped($event);
  159. }
  160. public function postCopy($arguments) {
  161. $source = $this->getNodeForPath($arguments['oldpath']);
  162. $target = $this->getNodeForPath($arguments['newpath']);
  163. $this->root->emit('\OC\Files', 'postCopy', [$source, $target]);
  164. $this->dispatcher->dispatch('\OCP\Files::postCopy', new GenericEvent([$source, $target]));
  165. $event = new NodeCopiedEvent($source, $target);
  166. $this->dispatcher->dispatchTyped($event);
  167. }
  168. public function read($arguments) {
  169. $node = $this->getNodeForPath($arguments['path']);
  170. $this->root->emit('\OC\Files', 'read', [$node]);
  171. $this->dispatcher->dispatch('\OCP\Files::read', new GenericEvent([$node]));
  172. $event = new BeforeNodeReadEvent($node);
  173. $this->dispatcher->dispatchTyped($event);
  174. }
  175. private function getNodeForPath(string $path): Node {
  176. $info = Filesystem::getView()->getFileInfo($path);
  177. if (!$info) {
  178. $fullPath = Filesystem::getView()->getAbsolutePath($path);
  179. if (isset($this->deleteMetaCache[$fullPath])) {
  180. $info = $this->deleteMetaCache[$fullPath];
  181. } else {
  182. $info = null;
  183. }
  184. if (Filesystem::is_dir($path)) {
  185. return new NonExistingFolder($this->root, $this->view, $fullPath, $info);
  186. } else {
  187. return new NonExistingFile($this->root, $this->view, $fullPath, $info);
  188. }
  189. }
  190. if ($info->getType() === FileInfo::TYPE_FILE) {
  191. return new File($this->root, $this->view, $info->getPath(), $info);
  192. } else {
  193. return new Folder($this->root, $this->view, $info->getPath(), $info);
  194. }
  195. }
  196. }