diff --git a/apps/files_sharing/lib/SharedMount.php b/apps/files_sharing/lib/SharedMount.php index b42682ab2a806..7c155b3f9c2bc 100644 --- a/apps/files_sharing/lib/SharedMount.php +++ b/apps/files_sharing/lib/SharedMount.php @@ -193,9 +193,68 @@ public function moveMount($target) { $result = true; try { - $this->updateFileTarget($relTargetPath, $share); - $this->setMountPoint($target); - $this->storage->setMountPoint($relTargetPath); + // Gets mount for destination target. If it is another share, will delete this share and move the file + $targetMount = Filesystem::getMountManager()->find($target); + if (is_a($targetMount, 'OCA\Files_Sharing\SharedMount')) { + $thisNode = \OC::$server->getRootFolder()->getById($this->getStorageRootId()); + $thisShares = \OC::$server->getShareManager()->getSharesByPath($thisNode[0]); + + $targetNode = \OC::$server->getRootFolder()->getById($targetMount->getStorageRootId()); + $targetShares = \OC::$server->getShareManager()->getSharesByPath($targetNode[0]); + + //Must have exactly the same shares + if (count($thisShares) == count($targetShares)) { + //checks for each share if there is correspondence + $sameShares = true; + foreach ($thisShares as $thisShare) { + foreach ($targetShares as $targetShare) { + if ($targetShare->getSharedWith() == $thisShare->getSharedWith() and + $targetShare->getShareType() == $thisShare->getShareType() and + ($thisShare->getNodeType() === 'file' or + $targetShare->getPermissions() == $thisShare->getPermissions())) { + continue 2; + } + } + $sameShares = false; + } + if ($sameShares) { + //Deletes share + foreach ($thisShares as $thisShare) { + \OC::$server->getShareManager()->deleteShare($thisShare); + } + + //Gets the mount for the original file + $srcPath = $thisNode[0]->getPath(); + $destPath = $targetNode[0]->getPath(). '/' . basename($internalPath1); + $srcMount = Filesystem::getMountManager()->find($srcPath); + $destMount = Filesystem::getMountManager()->find($destPath); + $storage1 = $srcMount->getStorage(); + $storage2 = $destMount->getStorage(); + $internalPath1 = $srcMount->getInternalPath($srcPath); + $internalPath2 = $srcMount->getInternalPath($destPath). '/' . basename($internalPath1); + + //Moves file into target + if ($storage1 === $storage2) { + if ($storage1) { + $result = $storage1->rename($internalPath1, $internalPath2); + } else { + $result = false; + } + // moving a file/folder between storages (from $storage1 to $storage2) + } else { + $result = $storage2->moveFromStorage($storage1, $internalPath1, $internalPath2); + } + } + + } else { + $result = false; + } + + } else { + $this->updateFileTarget($relTargetPath, $share); + $this->setMountPoint($target); + $this->storage->setMountPoint($relTargetPath); + } } catch (\Exception $e) { \OCP\Util::writeLog('file sharing', 'Could not rename mount point for shared folder "' . $this->getMountPoint() . '" to "' . $target . '"', diff --git a/lib/private/Share20/Manager.php b/lib/private/Share20/Manager.php index b2e192b830930..742dbe7445793 100644 --- a/lib/private/Share20/Manager.php +++ b/lib/private/Share20/Manager.php @@ -1140,7 +1140,13 @@ public function getShareById($id, $recipient = null) { * @return Share[] */ public function getSharesByPath(\OCP\Files\Node $path, $page=0, $perPage=50) { - return []; + $providers = $this->factory->getAllProviders(); + + $existingShares = []; + foreach ($providers as $provider) { + $existingShares = array_merge($existingShares, $provider->getSharesByPath($path)); + } + return $existingShares; } /** @@ -1545,4 +1551,6 @@ public function shareProviderExists($shareType) { return true; } + + } diff --git a/lib/public/Share/IManager.php b/lib/public/Share/IManager.php index 764e36b5d7eb8..edd8bb494327a 100644 --- a/lib/public/Share/IManager.php +++ b/lib/public/Share/IManager.php @@ -344,4 +344,13 @@ public function outgoingServer2ServerSharesAllowed(); */ public function shareProviderExists($shareType); + /** + * Get shares for a given path + * + * @param \OCP\Files\Node $path + * @return \OCP\Share\IShare[] + * @since 13.0.0 + */ + public function getSharesByPath(\OCP\Files\Node $path, $page=0, $perPage=50); + }