changewatcher.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * @author Morris Jobke <hey@morrisjobke.de>
  4. * @author Robin Appelman <icewind@owncloud.com>
  5. *
  6. * @copyright Copyright (c) 2015, ownCloud, Inc.
  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 OCA\Files_Sharing\Propagation;
  23. use OC\Files\Cache\ChangePropagator;
  24. use OC\Files\Filesystem;
  25. use OC\Files\View;
  26. use OCA\Files_Sharing\SharedMount;
  27. /**
  28. * Watch for changes made in a shared mount and propagate the changes to the share owner
  29. */
  30. class ChangeWatcher {
  31. /**
  32. * The user view for the logged in user
  33. *
  34. * @var \OC\Files\View
  35. */
  36. private $baseView;
  37. /**
  38. * @var RecipientPropagator
  39. */
  40. private $recipientPropagator;
  41. /**
  42. * @param \OC\Files\View $baseView the view for the logged in user
  43. * @param RecipientPropagator $recipientPropagator
  44. */
  45. public function __construct(View $baseView, RecipientPropagator $recipientPropagator) {
  46. $this->baseView = $baseView;
  47. $this->recipientPropagator = $recipientPropagator;
  48. }
  49. public function writeHook($params) {
  50. $path = $params['path'];
  51. $fullPath = $this->baseView->getAbsolutePath($path);
  52. $mount = $this->baseView->getMount($path);
  53. if ($mount instanceof SharedMount) {
  54. $this->propagateForOwner($mount->getShare(), $mount->getInternalPath($fullPath), $mount->getOwnerPropagator());
  55. }
  56. $info = $this->baseView->getFileInfo($path);
  57. if ($info) {
  58. // trigger propagation if the subject of the write hook is shared.
  59. // if a parent folder of $path is shared the propagation will be triggered from the change propagator hooks
  60. $this->recipientPropagator->propagateById($info->getId());
  61. }
  62. }
  63. public function renameHook($params) {
  64. $path1 = $params['oldpath'];
  65. $path2 = $params['newpath'];
  66. $fullPath1 = $this->baseView->getAbsolutePath($path1);
  67. $fullPath2 = $this->baseView->getAbsolutePath($path2);
  68. $mount1 = $this->baseView->getMount($path1);
  69. $mount2 = $this->baseView->getMount($path2);
  70. if ($mount1 instanceof SharedMount and $mount1->getInternalPath($fullPath1) !== '') {
  71. $this->propagateForOwner($mount1->getShare(), $mount1->getInternalPath($fullPath1), $mount1->getOwnerPropagator());
  72. }
  73. if ($mount1 !== $mount2 and $mount2 instanceof SharedMount and $mount2->getInternalPath($fullPath2) !== '') {
  74. $this->propagateForOwner($mount2->getShare(), $mount2->getInternalPath($fullPath2), $mount2->getOwnerPropagator());
  75. }
  76. }
  77. /**
  78. * @param array $share
  79. * @param string $internalPath
  80. * @param \OC\Files\Cache\ChangePropagator $propagator
  81. */
  82. private function propagateForOwner($share, $internalPath, ChangePropagator $propagator) {
  83. // note that we have already set up the filesystem for the owner when mounting the share
  84. $view = new View('/' . $share['uid_owner'] . '/files');
  85. $shareRootPath = $view->getPath($share['item_source']);
  86. if (!is_null($shareRootPath)) {
  87. $path = $shareRootPath . '/' . $internalPath;
  88. $path = Filesystem::normalizePath($path);
  89. $propagator->addChange($path);
  90. $propagator->propagateChanges();
  91. }
  92. }
  93. }