summaryrefslogtreecommitdiffstats
path: root/apps
diff options
context:
space:
mode:
authorRoeland Douma <rullzer@users.noreply.github.com>2016-04-14 11:50:27 +0200
committerVincent Petry <pvince81@owncloud.com>2016-06-23 14:27:51 +0200
commitb6192c39d840c46631cb8e3f4ab69aec4b04a479 (patch)
treef3d440d1dd98c19d8611646e148e94906625b49a /apps
parent3aaa33d9d405b826c599b4f1b6b8702ad1b62a77 (diff)
downloadnextcloud-server-b6192c39d840c46631cb8e3f4ab69aec4b04a479.tar.gz
nextcloud-server-b6192c39d840c46631cb8e3f4ab69aec4b04a479.zip
On mount make sure multiple shares with same target map to unique ones (#23937)
Scenario: user0 shares a folder 'foo' with user2 user1 shares a folder 'foo' with user2 user2 logs in Before: show only the 'foo' from user1 After: show both. * Added intergration tests
Diffstat (limited to 'apps')
-rw-r--r--apps/files_sharing/lib/mountprovider.php17
-rw-r--r--apps/files_sharing/lib/sharedmount.php47
2 files changed, 49 insertions, 15 deletions
diff --git a/apps/files_sharing/lib/mountprovider.php b/apps/files_sharing/lib/mountprovider.php
index d1d54a21ab3..b386c6704f9 100644
--- a/apps/files_sharing/lib/mountprovider.php
+++ b/apps/files_sharing/lib/mountprovider.php
@@ -63,24 +63,25 @@ class MountProvider implements IMountProvider {
$shares = array_filter($shares, function ($share) {
return $share['permissions'] > 0;
});
- $shares = array_map(function ($share) use ($user, $storageFactory) {
-
+ $mounts = [];
+ foreach ($shares as $share) {
try {
- return new SharedMount(
+ $mounts[] = new SharedMount(
'\OC\Files\Storage\Shared',
- '/' . $user->getUID() . '/' . $share['file_target'],
- array(
+ $mounts,
+ [
'share' => $share,
'user' => $user->getUID()
- ),
+ ],
$storageFactory
);
} catch (\Exception $e) {
$this->logger->logException($e);
$this->logger->error('Error while trying to create shared mount');
}
- }, $shares);
+ }
+
// array_filter removes the null values from the array
- return array_filter($shares);
+ return array_filter($mounts);
}
}
diff --git a/apps/files_sharing/lib/sharedmount.php b/apps/files_sharing/lib/sharedmount.php
index 3a65794b606..8eaf5469646 100644
--- a/apps/files_sharing/lib/sharedmount.php
+++ b/apps/files_sharing/lib/sharedmount.php
@@ -25,6 +25,7 @@
namespace OCA\Files_Sharing;
+use OC\Files\Filesystem;
use OC\Files\Mount\MountPoint;
use OC\Files\Mount\MoveableMount;
use OC\Files\View;
@@ -50,14 +51,14 @@ class SharedMount extends MountPoint implements MoveableMount {
/**
* @param string $storage
- * @param string $mountpoint
+ * @param SharedMount[] $mountpoints
* @param array|null $arguments
* @param \OCP\Files\Storage\IStorageFactory $loader
*/
- public function __construct($storage, $mountpoint, $arguments = null, $loader = null) {
+ public function __construct($storage, array $mountpoints, $arguments = null, $loader = null) {
$this->user = $arguments['user'];
$this->recipientView = new View('/' . $this->user . '/files');
- $newMountPoint = $this->verifyMountPoint($arguments['share']);
+ $newMountPoint = $this->verifyMountPoint($arguments['share'], $mountpoints);
$absMountPoint = '/' . $this->user . '/files' . $newMountPoint;
$arguments['ownerView'] = new View('/' . $arguments['share']['uid_owner'] . '/files');
parent::__construct($storage, $absMountPoint, $arguments, $loader);
@@ -67,9 +68,10 @@ class SharedMount extends MountPoint implements MoveableMount {
* check if the parent folder exists otherwise move the mount point up
*
* @param array $share
+ * @param SharedMount[] $mountpoints
* @return string
*/
- private function verifyMountPoint(&$share) {
+ private function verifyMountPoint(&$share, array $mountpoints) {
$mountPoint = basename($share['file_target']);
$parent = dirname($share['file_target']);
@@ -78,10 +80,10 @@ class SharedMount extends MountPoint implements MoveableMount {
$parent = Helper::getShareFolder($this->recipientView);
}
- $newMountPoint = \OCA\Files_Sharing\Helper::generateUniqueTarget(
+ $newMountPoint = $this->generateUniqueTarget(
\OC\Files\Filesystem::normalizePath($parent . '/' . $mountPoint),
- [],
- $this->recipientView
+ $this->recipientView,
+ $mountpoints
);
if ($newMountPoint !== $share['file_target']) {
@@ -94,6 +96,37 @@ class SharedMount extends MountPoint implements MoveableMount {
}
/**
+ * @param string $path
+ * @param View $view
+ * @param SharedMount[] $mountpoints
+ * @return mixed
+ */
+ private function generateUniqueTarget($path, $view, array $mountpoints) {
+ $pathinfo = pathinfo($path);
+ $ext = (isset($pathinfo['extension'])) ? '.'.$pathinfo['extension'] : '';
+ $name = $pathinfo['filename'];
+ $dir = $pathinfo['dirname'];
+
+ // Helper function to find existing mount points
+ $mountpointExists = function($path) use ($mountpoints) {
+ foreach ($mountpoints as $mountpoint) {
+ if ($mountpoint->getShare()['file_target'] === $path) {
+ return true;
+ }
+ }
+ return false;
+ };
+
+ $i = 2;
+ while ($view->file_exists($path) || $mountpointExists($path)) {
+ $path = Filesystem::normalizePath($dir . '/' . $name . ' ('.$i.')' . $ext);
+ $i++;
+ }
+
+ return $path;
+ }
+
+ /**
* update fileTarget in the database if the mount point changed
*
* @param string $newPath