refactor: move share unique path generation from view api to node api

Signed-off-by: Robin Appelman <robin@icewind.nl>
This commit is contained in:
Robin Appelman 2025-11-11 21:03:49 +01:00
parent cd1c45d789
commit 4eeb8f1583
No known key found for this signature in database
GPG key ID: 42B69D8A64526EFB
3 changed files with 31 additions and 30 deletions

View file

@ -12,6 +12,7 @@ use OCA\Files_Sharing\Event\ShareMountedEvent;
use OCP\Cache\CappedMemoryCache;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Config\IMountProvider;
use OCP\Files\IRootFolder;
use OCP\Files\Mount\IMountManager;
use OCP\Files\Mount\IMountPoint;
use OCP\Files\Storage\IStorageFactory;
@ -35,6 +36,7 @@ class MountProvider implements IMountProvider {
protected IEventDispatcher $eventDispatcher,
protected ICacheFactory $cacheFactory,
protected IMountManager $mountManager,
protected IRootFolder $rootFolder,
) {
}
@ -64,7 +66,6 @@ class MountProvider implements IMountProvider {
$allMounts = $this->mountManager->getAll();
$mounts = [];
$view = new View('/' . $user->getUID() . '/files');
$ownerViews = [];
$sharingDisabledForUser = $this->shareManager->sharingDisabledForUser($user->getUID());
/** @var CappedMemoryCache<bool> $folderExistCache */
@ -100,14 +101,13 @@ class MountProvider implements IMountProvider {
'superShare' => $parentShare,
// children/component of the superShare
'groupedShares' => $share[1],
'ownerView' => $ownerViews[$owner],
'sharingDisabledForUser' => $sharingDisabledForUser
],
$loader,
$view,
$foldersExistCache,
$this->eventDispatcher,
$user,
$this->rootFolder,
($shareId <= $maxValidatedShare),
);

View file

@ -16,6 +16,8 @@ use OCA\Files_Sharing\Exceptions\BrokenPath;
use OCP\Cache\CappedMemoryCache;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Events\InvalidateMountCacheEvent;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use OCP\Files\Storage\IStorageFactory;
use OCP\IDBConnection;
use OCP\IUser;
@ -44,10 +46,10 @@ class SharedMount extends MountPoint implements MoveableMount, ISharedMountPoint
array $mountpoints,
$arguments,
IStorageFactory $loader,
private View $recipientView,
CappedMemoryCache $folderExistCache,
private IEventDispatcher $eventDispatcher,
private IUser $user,
private IRootFolder $rootFolder,
bool $alreadyVerified,
) {
$this->superShare = $arguments['superShare'];
@ -80,24 +82,26 @@ class SharedMount extends MountPoint implements MoveableMount, ISharedMountPoint
$mountPoint = basename($share->getTarget());
$parent = dirname($share->getTarget());
$event = new VerifyMountPointEvent($share, $this->recipientView, $parent);
$event = new VerifyMountPointEvent($share, $this->user, $parent);
$this->eventDispatcher->dispatchTyped($event);
$parent = $event->getParent();
$userFolder = $this->rootFolder->getUserFolder($this->user->getUID());
$cached = $folderExistCache->get($parent);
if ($cached) {
$parentExists = $cached;
} else {
$parentExists = $this->recipientView->is_dir($parent);
$parentExists = $userFolder->get($parent) instanceof Folder;
$folderExistCache->set($parent, $parentExists);
}
if (!$parentExists) {
$parent = Helper::getShareFolder($this->recipientView, $this->user->getUID());
$parent = Helper::getShareFolder(new View('/' . $this->user->getUID() . '/files'), $this->user->getUID());
}
$newMountPoint = $this->generateUniqueTarget(
Filesystem::normalizePath($parent . '/' . $mountPoint),
$this->recipientView,
$userFolder,
$mountpoints
);
@ -128,22 +132,19 @@ class SharedMount extends MountPoint implements MoveableMount, ISharedMountPoint
/**
* @param string $path
* @param View $view
* @param SharedMount[] $mountpoints
* @return mixed
*/
private function generateUniqueTarget($path, $view, array $mountpoints) {
private function generateUniqueTarget(string $path, Folder $userFolder, array $mountpoints): string {
$pathinfo = pathinfo($path);
$ext = isset($pathinfo['extension']) ? '.' . $pathinfo['extension'] : '';
$name = $pathinfo['filename'];
$dir = $pathinfo['dirname'];
$i = 2;
$absolutePath = $this->recipientView->getAbsolutePath($path) . '/';
while ($view->file_exists($path) || isset($mountpoints[$absolutePath])) {
$absolutePath = $userFolder->getFullPath($path) . '/';
while ($userFolder->nodeExists($path) || isset($mountpoints[$absolutePath])) {
$path = Filesystem::normalizePath($dir . '/' . $name . ' (' . $i . ')' . $ext);
$absolutePath = $this->recipientView->getAbsolutePath($path) . '/';
$absolutePath = $userFolder->getFullPath($path) . '/';
$i++;
}

View file

@ -10,30 +10,22 @@ namespace OCP\Share\Events;
use OC\Files\View;
use OCP\EventDispatcher\Event;
use OCP\IUser;
use OCP\Share\IShare;
/**
* @since 19.0.0
*/
class VerifyMountPointEvent extends Event {
/** @var IShare */
private $share;
/** @var View */
private $view;
/** @var string */
private $parent;
/**
* @since 19.0.0
*/
public function __construct(IShare $share,
View $view,
string $parent) {
public function __construct(
private IShare $share,
private readonly IUser $recipient,
private string $parent
) {
parent::__construct();
$this->share = $share;
$this->view = $view;
$this->parent = $parent;
}
/**
@ -45,9 +37,17 @@ class VerifyMountPointEvent extends Event {
/**
* @since 19.0.0
* @depreacted 33.0.0 use getRecipient instead to get the user folder
*/
public function getView(): View {
return $this->view;
return new View('/' . $this->recipient->getUID() . '/files');
}
/**
* @since 33.0.0
*/
public function getRecipient(): IUser {
return $this->recipient;
}
/**