Skip to content

Commit

Permalink
Merge pull request #44460 from nextcloud/remove-non-shallow-in-folder
Browse files Browse the repository at this point in the history
refactor: remove non-shallow getSharesInFolder
  • Loading branch information
icewind1991 authored May 17, 2024
2 parents 495d397 + ccd5667 commit 54afea4
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 40 deletions.
11 changes: 5 additions & 6 deletions apps/federatedfilesharing/lib/FederatedShareProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,10 @@ public function restore(IShare $share, string $recipient): IShare {


public function getSharesInFolder($userId, Folder $node, $reshares, $shallow = true) {
if (!$shallow) {
throw new \Exception("non-shallow getSharesInFolder is no longer supported");
}

$qb = $this->dbConnection->getQueryBuilder();
$qb->select('*')
->from('share', 's')
Expand Down Expand Up @@ -605,12 +609,7 @@ public function getSharesInFolder($userId, Folder $node, $reshares, $shallow = t

$qb->innerJoin('s', 'filecache', 'f', $qb->expr()->eq('s.file_source', 'f.fileid'));

$qb->andWhere($qb->expr()->eq('f.storage', $qb->createNamedParameter($node->getMountPoint()->getNumericStorageId(), IQueryBuilder::PARAM_INT)));
if ($shallow) {
$qb->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($node->getId(), IQueryBuilder::PARAM_INT)));
} else {
$qb->andWhere($qb->expr()->like('f.path', $qb->createNamedParameter($this->dbConnection->escapeLikeParameter($node->getInternalPath()) . '/%')));
}
$qb->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($node->getId())));

$qb->orderBy('id');

Expand Down
32 changes: 27 additions & 5 deletions apps/files_sharing/lib/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
*/
namespace OCA\Files_Sharing;

use OC\Files\Cache\FileAccess;
use OC\Files\Mount\MountPoint;
use OCP\Constants;
use OCP\Files\Folder;
use OCP\Server;
use OCP\Share\IShare;

class Updater {
Expand Down Expand Up @@ -58,20 +60,40 @@ private static function moveShareInOrOutOfShare($path): void {
if ($userFolder === null) {
return;
}
$user = $userFolder->getOwner();
if (!$user) {
throw new \Exception("user folder has no owner");
}

$src = $userFolder->get($path);

$shareManager = \OC::$server->getShareManager();

// FIXME: should CIRCLES be included here ??
$shares = $shareManager->getSharesBy($userFolder->getOwner()->getUID(), IShare::TYPE_USER, $src, false, -1);
$shares = array_merge($shares, $shareManager->getSharesBy($userFolder->getOwner()->getUID(), IShare::TYPE_GROUP, $src, false, -1));
$shares = array_merge($shares, $shareManager->getSharesBy($userFolder->getOwner()->getUID(), IShare::TYPE_ROOM, $src, false, -1));
$shares = $shareManager->getSharesBy($user->getUID(), IShare::TYPE_USER, $src, false, -1);
$shares = array_merge($shares, $shareManager->getSharesBy($user->getUID(), IShare::TYPE_GROUP, $src, false, -1));
$shares = array_merge($shares, $shareManager->getSharesBy($user->getUID(), IShare::TYPE_ROOM, $src, false, -1));

if ($src instanceof Folder) {
$subShares = $shareManager->getSharesInFolder($userFolder->getOwner()->getUID(), $src, false, false);
$cacheAccess = Server::get(FileAccess::class);

$sourceStorageId = $src->getStorage()->getCache()->getNumericStorageId();
$sourceInternalPath = $src->getInternalPath();
$subShares = array_merge(
$shareManager->getSharesBy($user->getUID(), IShare::TYPE_USER),
$shareManager->getSharesBy($user->getUID(), IShare::TYPE_GROUP),
$shareManager->getSharesBy($user->getUID(), IShare::TYPE_ROOM),
);
$shareSourceIds = array_map(fn (IShare $share) => $share->getNodeId(), $subShares);
$shareSources = $cacheAccess->getByFileIdsInStorage($shareSourceIds, $sourceStorageId);
foreach ($subShares as $subShare) {
$shares = array_merge($shares, array_values($subShare));
$shareCacheEntry = $shareSources[$subShare->getNodeId()] ?? null;
if (
$shareCacheEntry &&
str_starts_with($shareCacheEntry->getPath(), $sourceInternalPath . '/')
) {
$shares[] = $subShare;
}
}
}

Expand Down
7 changes: 1 addition & 6 deletions apps/sharebymail/lib/ShareByMailProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -1069,12 +1069,7 @@ public function getSharesInFolder($userId, Folder $node, $reshares, $shallow = t

$qb->innerJoin('s', 'filecache', 'f', $qb->expr()->eq('s.file_source', 'f.fileid'));

$qb->andWhere($qb->expr()->eq('f.storage', $qb->createNamedParameter($node->getMountPoint()->getNumericStorageId(), IQueryBuilder::PARAM_INT)));
if ($shallow) {
$qb->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($node->getId())));
} else {
$qb->andWhere($qb->expr()->like('f.path', $qb->createNamedParameter($this->dbConnection->escapeLikeParameter($node->getInternalPath()) . '/%')));
}
$qb->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($node->getId())));

$qb->orderBy('id');

Expand Down
32 changes: 10 additions & 22 deletions lib/private/Share20/DefaultShareProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,10 @@ public function move(\OCP\Share\IShare $share, $recipient) {
}

public function getSharesInFolder($userId, Folder $node, $reshares, $shallow = true) {
if (!$shallow) {
throw new \Exception("non-shallow getSharesInFolder is no longer supported");
}

$qb = $this->dbConn->getQueryBuilder();
$qb->select('s.*',
'f.fileid', 'f.path', 'f.permissions AS f_permissions', 'f.storage', 'f.path_hash',
Expand Down Expand Up @@ -692,28 +696,12 @@ public function getSharesInFolder($userId, Folder $node, $reshares, $shallow = t
}, $childMountNodes);

$qb->innerJoin('s', 'filecache', 'f', $qb->expr()->eq('s.file_source', 'f.fileid'));
$storageFilter = $qb->expr()->eq('f.storage', $qb->createNamedParameter($node->getMountPoint()->getNumericStorageId(), IQueryBuilder::PARAM_INT));
if ($shallow) {
$qb->andWhere(
$qb->expr()->orX(
$qb->expr()->andX(
$storageFilter,
$qb->expr()->eq('f.parent', $qb->createNamedParameter($node->getId())),
),
$qb->expr()->in('f.fileid', $qb->createParameter('chunk'))
)
);
} else {
$qb->andWhere(
$qb->expr()->orX(
$qb->expr()->andX(
$storageFilter,
$qb->expr()->like('f.path', $qb->createNamedParameter($this->dbConn->escapeLikeParameter($node->getInternalPath()) . '/%')),
),
$qb->expr()->in('f.fileid', $qb->createParameter('chunk'))
)
);
}
$qb->andWhere(
$qb->expr()->orX(
$qb->expr()->eq('f.parent', $qb->createNamedParameter($node->getId())),
$qb->expr()->in('f.fileid', $qb->createParameter('chunk'))
)
);

$qb->orderBy('id');

Expand Down
5 changes: 4 additions & 1 deletion lib/private/Share20/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -1276,9 +1276,12 @@ public function moveShare(IShare $share, $recipientId) {

public function getSharesInFolder($userId, Folder $node, $reshares = false, $shallow = true) {
$providers = $this->factory->getAllProviders();
if (!$shallow) {
throw new \Exception("non-shallow getSharesInFolder is no longer supported");
}

return array_reduce($providers, function ($shares, IShareProvider $provider) use ($userId, $node, $reshares, $shallow) {
$newShares = $provider->getSharesInFolder($userId, $node, $reshares, $shallow);
$newShares = $provider->getSharesInFolder($userId, $node, $reshares);
foreach ($newShares as $fid => $data) {
if (!isset($shares[$fid])) {
$shares[$fid] = [];
Expand Down

0 comments on commit 54afea4

Please sign in to comment.