Skip to content

Commit

Permalink
Allows moving a share into another, (filesharing)
Browse files Browse the repository at this point in the history
When a share is moved inside another, will check if both shares have equivalence in sharing types, permissions and sharing targets. If they have, will remove the sharing of the first and move it inside the second.

This changes a policy in nextcloud and I don't know if this will be accepted.
nextcloud#6707

Signed-off-by: Vinicius Cubas Brand <viniciuscb@gmail.com>
  • Loading branch information
viniciuscb committed Oct 3, 2017
1 parent bc9b3c0 commit 5b34e62
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 4 deletions.
65 changes: 62 additions & 3 deletions apps/files_sharing/lib/SharedMount.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 . '"',
Expand Down
10 changes: 9 additions & 1 deletion lib/private/Share20/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -1165,7 +1165,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;
}

/**
Expand Down Expand Up @@ -1570,4 +1576,6 @@ public function shareProviderExists($shareType) {
return true;
}



}
9 changes: 9 additions & 0 deletions lib/public/Share/IManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,4 +341,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);

}

0 comments on commit 5b34e62

Please sign in to comment.