1
0

HookConnector.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 Maxence Lange <maxence@artificial-owl.com>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OC\Files\Node;
  27. use OC\Files\Filesystem;
  28. use OC\Files\View;
  29. use OCP\EventDispatcher\GenericEvent;
  30. use OCP\EventDispatcher\IEventDispatcher;
  31. use OCP\Exceptions\AbortedEventException;
  32. use OCP\Files\Events\Node\BeforeNodeCopiedEvent;
  33. use OCP\Files\Events\Node\BeforeNodeCreatedEvent;
  34. use OCP\Files\Events\Node\BeforeNodeDeletedEvent;
  35. use OCP\Files\Events\Node\BeforeNodeReadEvent;
  36. use OCP\Files\Events\Node\BeforeNodeRenamedEvent;
  37. use OCP\Files\Events\Node\BeforeNodeTouchedEvent;
  38. use OCP\Files\Events\Node\BeforeNodeWrittenEvent;
  39. use OCP\Files\Events\Node\NodeCopiedEvent;
  40. use OCP\Files\Events\Node\NodeCreatedEvent;
  41. use OCP\Files\Events\Node\NodeDeletedEvent;
  42. use OCP\Files\Events\Node\NodeRenamedEvent;
  43. use OCP\Files\Events\Node\NodeTouchedEvent;
  44. use OCP\Files\Events\Node\NodeWrittenEvent;
  45. use OCP\Files\FileInfo;
  46. use OCP\Files\IRootFolder;
  47. use OCP\Util;
  48. use Psr\Log\LoggerInterface;
  49. class HookConnector {
  50. /** @var FileInfo[] */
  51. private array $deleteMetaCache = [];
  52. public function __construct(
  53. private IRootFolder $root,
  54. private View $view,
  55. private IEventDispatcher $dispatcher,
  56. private LoggerInterface $logger
  57. ) {
  58. }
  59. public function viewToNode() {
  60. Util::connectHook('OC_Filesystem', 'write', $this, 'write');
  61. Util::connectHook('OC_Filesystem', 'post_write', $this, 'postWrite');
  62. Util::connectHook('OC_Filesystem', 'create', $this, 'create');
  63. Util::connectHook('OC_Filesystem', 'post_create', $this, 'postCreate');
  64. Util::connectHook('OC_Filesystem', 'delete', $this, 'delete');
  65. Util::connectHook('OC_Filesystem', 'post_delete', $this, 'postDelete');
  66. Util::connectHook('OC_Filesystem', 'rename', $this, 'rename');
  67. Util::connectHook('OC_Filesystem', 'post_rename', $this, 'postRename');
  68. Util::connectHook('OC_Filesystem', 'copy', $this, 'copy');
  69. Util::connectHook('OC_Filesystem', 'post_copy', $this, 'postCopy');
  70. Util::connectHook('OC_Filesystem', 'touch', $this, 'touch');
  71. Util::connectHook('OC_Filesystem', 'post_touch', $this, 'postTouch');
  72. Util::connectHook('OC_Filesystem', 'read', $this, 'read');
  73. }
  74. public function write($arguments) {
  75. $node = $this->getNodeForPath($arguments['path']);
  76. $this->root->emit('\OC\Files', 'preWrite', [$node]);
  77. $this->dispatcher->dispatch('\OCP\Files::preWrite', new GenericEvent($node));
  78. $event = new BeforeNodeWrittenEvent($node);
  79. $this->dispatcher->dispatchTyped($event);
  80. }
  81. public function postWrite($arguments) {
  82. $node = $this->getNodeForPath($arguments['path']);
  83. $this->root->emit('\OC\Files', 'postWrite', [$node]);
  84. $this->dispatcher->dispatch('\OCP\Files::postWrite', new GenericEvent($node));
  85. $event = new NodeWrittenEvent($node);
  86. $this->dispatcher->dispatchTyped($event);
  87. }
  88. public function create($arguments) {
  89. $node = $this->getNodeForPath($arguments['path']);
  90. $this->root->emit('\OC\Files', 'preCreate', [$node]);
  91. $this->dispatcher->dispatch('\OCP\Files::preCreate', new GenericEvent($node));
  92. $event = new BeforeNodeCreatedEvent($node);
  93. $this->dispatcher->dispatchTyped($event);
  94. }
  95. public function postCreate($arguments) {
  96. $node = $this->getNodeForPath($arguments['path']);
  97. $this->root->emit('\OC\Files', 'postCreate', [$node]);
  98. $this->dispatcher->dispatch('\OCP\Files::postCreate', new GenericEvent($node));
  99. $event = new NodeCreatedEvent($node);
  100. $this->dispatcher->dispatchTyped($event);
  101. }
  102. public function delete($arguments) {
  103. $node = $this->getNodeForPath($arguments['path']);
  104. $this->deleteMetaCache[$node->getPath()] = $node->getFileInfo();
  105. $this->root->emit('\OC\Files', 'preDelete', [$node]);
  106. $this->dispatcher->dispatch('\OCP\Files::preDelete', new GenericEvent($node));
  107. $event = new BeforeNodeDeletedEvent($node);
  108. try {
  109. $this->dispatcher->dispatchTyped($event);
  110. } catch (AbortedEventException $e) {
  111. $arguments['run'] = false;
  112. $this->logger->warning('delete process aborted', ['exception' => $e]);
  113. }
  114. }
  115. public function postDelete($arguments) {
  116. $node = $this->getNodeForPath($arguments['path']);
  117. unset($this->deleteMetaCache[$node->getPath()]);
  118. $this->root->emit('\OC\Files', 'postDelete', [$node]);
  119. $this->dispatcher->dispatch('\OCP\Files::postDelete', new GenericEvent($node));
  120. $event = new NodeDeletedEvent($node);
  121. $this->dispatcher->dispatchTyped($event);
  122. }
  123. public function touch($arguments) {
  124. $node = $this->getNodeForPath($arguments['path']);
  125. $this->root->emit('\OC\Files', 'preTouch', [$node]);
  126. $this->dispatcher->dispatch('\OCP\Files::preTouch', new GenericEvent($node));
  127. $event = new BeforeNodeTouchedEvent($node);
  128. $this->dispatcher->dispatchTyped($event);
  129. }
  130. public function postTouch($arguments) {
  131. $node = $this->getNodeForPath($arguments['path']);
  132. $this->root->emit('\OC\Files', 'postTouch', [$node]);
  133. $this->dispatcher->dispatch('\OCP\Files::postTouch', new GenericEvent($node));
  134. $event = new NodeTouchedEvent($node);
  135. $this->dispatcher->dispatchTyped($event);
  136. }
  137. public function rename($arguments) {
  138. $source = $this->getNodeForPath($arguments['oldpath']);
  139. $target = $this->getNodeForPath($arguments['newpath']);
  140. $this->root->emit('\OC\Files', 'preRename', [$source, $target]);
  141. $this->dispatcher->dispatch('\OCP\Files::preRename', new GenericEvent([$source, $target]));
  142. $event = new BeforeNodeRenamedEvent($source, $target);
  143. try {
  144. $this->dispatcher->dispatchTyped($event);
  145. } catch (AbortedEventException $e) {
  146. $arguments['run'] = false;
  147. $this->logger->warning('rename process aborted', ['exception' => $e]);
  148. }
  149. }
  150. public function postRename($arguments) {
  151. $source = $this->getNodeForPath($arguments['oldpath']);
  152. $target = $this->getNodeForPath($arguments['newpath']);
  153. $this->root->emit('\OC\Files', 'postRename', [$source, $target]);
  154. $this->dispatcher->dispatch('\OCP\Files::postRename', new GenericEvent([$source, $target]));
  155. $event = new NodeRenamedEvent($source, $target);
  156. $this->dispatcher->dispatchTyped($event);
  157. }
  158. public function copy($arguments) {
  159. $source = $this->getNodeForPath($arguments['oldpath']);
  160. $target = $this->getNodeForPath($arguments['newpath']);
  161. $this->root->emit('\OC\Files', 'preCopy', [$source, $target]);
  162. $this->dispatcher->dispatch('\OCP\Files::preCopy', new GenericEvent([$source, $target]));
  163. $event = new BeforeNodeCopiedEvent($source, $target);
  164. try {
  165. $this->dispatcher->dispatchTyped($event);
  166. } catch (AbortedEventException $e) {
  167. $arguments['run'] = false;
  168. $this->logger->warning('copy process aborted', ['exception' => $e]);
  169. }
  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->dispatcher->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->dispatcher->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. }