Skip to content

Commit

Permalink
fix duplicate name on new share
Browse files Browse the repository at this point in the history
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
  • Loading branch information
ArtificialOwl committed Apr 15, 2020
1 parent fe742d7 commit 013c0fe
Showing 1 changed file with 44 additions and 2 deletions.
46 changes: 44 additions & 2 deletions lib/GlobalScale/GSMount/MountProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@
namespace OCA\Circles\GlobalScale\GSMount;


use daita\MySmallPhpTools\Traits\TArrayTools;
use Exception;
use OC;
use OCA\Circles\Db\GSSharesRequest;
use OCA\Circles\Model\GlobalScale\GSShare;
use OCP\AppFramework\QueryException;
use OCA\Circles\Model\GlobalScale\GSShareMountpoint;
use OCP\Federation\ICloudIdManager;
use OCP\Files\Config\IMountProvider;
use OCP\Files\Mount\IMountPoint;
use OCP\Files\NotFoundException;
use OCP\Files\Storage\IStorageFactory;
use OCP\IUser;

Expand All @@ -50,6 +52,9 @@
class MountProvider implements IMountProvider {


use TArrayTools;


const STORAGE = '\OCA\Files_Sharing\External\Storage';


Expand Down Expand Up @@ -84,7 +89,6 @@ public function __construct(
* @param IStorageFactory $loader
*
* @return IMountPoint[]
* @throws QueryException
*/
public function getMountsForUser(IUser $user, IStorageFactory $loader): array {
$shares = $this->gsSharesRequest->getForUser($user->getUID());
Expand All @@ -93,6 +97,7 @@ public function getMountsForUser(IUser $user, IStorageFactory $loader): array {
foreach ($shares as $share) {
try {
if ($share->getMountPoint() !== '-') {
$this->fixDuplicateFile($user->getUID(), $share);
$mounts[] = $this->generateMount($share, $user->getUID(), $loader);
}
} catch (Exception $e) {
Expand Down Expand Up @@ -127,5 +132,42 @@ public function generateMount(
);
}


/**
* @param string $userId
* @param GSShare $share
*/
private function fixDuplicateFile(string $userId, GSShare $share) {
$fs = \OC::$server->getRootFolder()
->getUserFolder($userId);

try {
$fs->get($share->getMountPoint());
} catch (NotFoundException $e) {
return;
}

$info = pathinfo($share->getMountPoint());
$filename = $this->get('dirname', $info) . '/' . $this->get('filename', $info);
$extension = $this->get('extension', $info);
$extension = ($extension === '') ? '' : '.' . $extension;

$n = 2;
while (true) {
$path = $filename . " ($n)" . $extension;
try {
$fs->get($path);
} catch (NotFoundException $e) {
$share->setMountPoint($path);
$mountPoint = new GSShareMountpoint($share->getId(), $userId, $path);
$this->gsSharesRequest->updateShareMountPoint($mountPoint);

return;
}

$n++;
}
}

}

0 comments on commit 013c0fe

Please sign in to comment.