1
0

HookConnector.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  47. class HookConnector {
  48. /** @var IRootFolder */
  49. private $root;
  50. /** @var View */
  51. private $view;
  52. /** @var FileInfo[] */
  53. private $deleteMetaCache = [];
  54. /** @var EventDispatcherInterface */
  55. private $legacyDispatcher;
  56. /** @var IEventDispatcher */
  57. private $dispatcher;
  58. /**
  59. * HookConnector constructor.
  60. *
  61. * @param Root $root
  62. * @param View $view
  63. */
  64. public function __construct(
  65. IRootFolder $root,
  66. View $view,
  67. EventDispatcherInterface $legacyDispatcher,
  68. IEventDispatcher $dispatcher) {
  69. $this->root = $root;
  70. $this->view = $view;
  71. $this->legacyDispatcher = $legacyDispatcher;
  72. $this->dispatcher = $dispatcher;
  73. }
  74. public function viewToNode() {
  75. Util::connectHook('OC_Filesystem', 'write', $this, 'write');
  76. Util::connectHook('OC_Filesystem', 'post_write', $this, 'postWrite');
  77. Util::connectHook('OC_Filesystem', 'create', $this, 'create');
  78. Util::connectHook('OC_Filesystem', 'post_create', $this, 'postCreate');
  79. Util::connectHook('OC_Filesystem', 'delete', $this, 'delete');
  80. Util::connectHook('OC_Filesystem', 'post_delete', $this, 'postDelete');
  81. Util::connectHook('OC_Filesystem', 'rename', $this, 'rename');
  82. Util::connectHook('OC_Filesystem', 'post_rename', $this, 'postRename');
  83. Util::connectHook('OC_Filesystem', 'copy', $this, 'copy');
  84. Util::connectHook('OC_Filesystem', 'post_copy', $this, 'postCopy');
  85. Util::connectHook('OC_Filesystem', 'touch', $this, 'touch');
  86. Util::connectHook('OC_Filesystem', 'post_touch', $this, 'postTouch');
  87. Util::connectHook('OC_Filesystem', 'read', $this, 'read');
  88. }
  89. public function write($arguments) {
  90. $node = $this->getNodeForPath($arguments['path']);
  91. $this->root->emit('\OC\Files', 'preWrite', [$node]);
  92. $this->legacyDispatcher->dispatch('\OCP\Files::preWrite', new GenericEvent($node));
  93. $event = new BeforeNodeWrittenEvent($node);
  94. $this->dispatcher->dispatchTyped($event);
  95. }
  96. public function postWrite($arguments) {
  97. $node = $this->getNodeForPath($arguments['path']);
  98. $this->root->emit('\OC\Files', 'postWrite', [$node]);
  99. $this->legacyDispatcher->dispatch('\OCP\Files::postWrite', new GenericEvent($node));
  100. $event = new NodeWrittenEvent($node);
  101. $this->dispatcher->dispatchTyped($event);
  102. }
  103. public function create($arguments) {
  104. $node = $this->getNodeForPath($arguments['path']);
  105. $this->root->emit('\OC\Files', 'preCreate', [$node]);
  106. $this->legacyDispatcher->dispatch('\OCP\Files::preCreate', new GenericEvent($node));
  107. $event = new BeforeNodeCreatedEvent($node);
  108. $this->dispatcher->dispatchTyped($event);
  109. }
  110. public function postCreate($arguments) {
  111. $node = $this->getNodeForPath($arguments['path']);
  112. $this->root->emit('\OC\Files', 'postCreate', [$node]);
  113. $this->legacyDispatcher->dispatch('\OCP\Files::postCreate', new GenericEvent($node));
  114. $event = new NodeCreatedEvent($node);
  115. $this->dispatcher->dispatchTyped($event);
  116. }
  117. public function delete($arguments) {
  118. $node = $this->getNodeForPath($arguments['path']);
  119. $this->deleteMetaCache[$node->getPath()] = $node->getFileInfo();
  120. $this->root->emit('\OC\Files', 'preDelete', [$node]);
  121. $this->legacyDispatcher->dispatch('\OCP\Files::preDelete', new GenericEvent($node));
  122. $event = new BeforeNodeDeletedEvent($node);
  123. $this->dispatcher->dispatchTyped($event);
  124. }
  125. public function postDelete($arguments) {
  126. $node = $this->getNodeForPath($arguments['path']);
  127. unset($this->deleteMetaCache[$node->getPath()]);
  128. $this->root->emit('\OC\Files', 'postDelete', [$node]);
  129. $this->legacyDispatcher->dispatch('\OCP\Files::postDelete', new GenericEvent($node));
  130. $event = new NodeDeletedEvent($node);
  131. $this->dispatcher->dispatchTyped($event);
  132. }
  133. public function touch($arguments) {
  134. $node = $this->getNodeForPath($arguments['path']);
  135. $this->root->emit('\OC\Files', 'preTouch', [$node]);
  136. $this->legacyDispatcher->dispatch('\OCP\Files::preTouch', new GenericEvent($node));
  137. $event = new BeforeNodeTouchedEvent($node);
  138. $this->dispatcher->dispatchTyped($event);
  139. }
  140. public function postTouch($arguments) {
  141. $node = $this->getNodeForPath($arguments['path']);
  142. $this->root->emit('\OC\Files', 'postTouch', [$node]);
  143. $this->legacyDispatcher->dispatch('\OCP\Files::postTouch', new GenericEvent($node));
  144. $event = new NodeTouchedEvent($node);
  145. $this->dispatcher->dispatchTyped($event);
  146. }
  147. public function rename($arguments) {
  148. $source = $this->getNodeForPath($arguments['oldpath']);
  149. $target = $this->getNodeForPath($arguments['newpath']);
  150. $this->root->emit('\OC\Files', 'preRename', [$source, $target]);
  151. $this->legacyDispatcher->dispatch('\OCP\Files::preRename', new GenericEvent([$source, $target]));
  152. $event = new BeforeNodeRenamedEvent($source, $target);
  153. $this->dispatcher->dispatchTyped($event);
  154. }
  155. public function postRename($arguments) {
  156. $source = $this->getNodeForPath($arguments['oldpath']);
  157. $target = $this->getNodeForPath($arguments['newpath']);
  158. $this->root->emit('\OC\Files', 'postRename', [$source, $target]);
  159. $this->legacyDispatcher->dispatch('\OCP\Files::postRename', new GenericEvent([$source, $target]));
  160. $event = new NodeRenamedEvent($source, $target);
  161. $this->dispatcher->dispatchTyped($event);
  162. }
  163. public function copy($arguments) {
  164. $source = $this->getNodeForPath($arguments['oldpath']);
  165. $target = $this->getNodeForPath($arguments['newpath']);
  166. $this->root->emit('\OC\Files', 'preCopy', [$source, $target]);
  167. $this->legacyDispatcher->dispatch('\OCP\Files::preCopy', new GenericEvent([$source, $target]));
  168. $event = new BeforeNodeCopiedEvent($source, $target);
  169. $this->dispatcher->dispatchTyped($event);
  170. }
  171. public function postCopy($arguments) {
  172. $source = $this->getNodeForPath($arguments['oldpath']);
  173. $target = $this->getNodeForPath($arguments['newpath']);
  174. $this->root->emit('\OC\Files', 'postCopy', [$source, $target]);
  175. $this->legacyDispatcher->dispatch('\OCP\Files::postCopy', new GenericEvent([$source, $target]));
  176. $event = new NodeCopiedEvent($source, $target);
  177. $this->dispatcher->dispatchTyped($event);
  178. }
  179. public function read($arguments) {
  180. $node = $this->getNodeForPath($arguments['path']);
  181. $this->root->emit('\OC\Files', 'read', [$node]);
  182. $this->legacyDispatcher->dispatch('\OCP\Files::read', new GenericEvent([$node]));
  183. $event = new BeforeNodeReadEvent($node);
  184. $this->dispatcher->dispatchTyped($event);
  185. }
  186. private function getNodeForPath(string $path): Node {
  187. $info = Filesystem::getView()->getFileInfo($path);
  188. if (!$info) {
  189. $fullPath = Filesystem::getView()->getAbsolutePath($path);
  190. if (isset($this->deleteMetaCache[$fullPath])) {
  191. $info = $this->deleteMetaCache[$fullPath];
  192. } else {
  193. $info = null;
  194. }
  195. if (Filesystem::is_dir($path)) {
  196. return new NonExistingFolder($this->root, $this->view, $fullPath, $info);
  197. } else {
  198. return new NonExistingFile($this->root, $this->view, $fullPath, $info);
  199. }
  200. }
  201. if ($info->getType() === FileInfo::TYPE_FILE) {
  202. return new File($this->root, $this->view, $info->getPath(), $info);
  203. } else {
  204. return new Folder($this->root, $this->view, $info->getPath(), $info);
  205. }
  206. }
  207. }