Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use proper caching
Signed-off-by: Roeland Jago Douma <[email protected]>
  • Loading branch information
rullzer committed Jul 30, 2019
commit 06a9ade582c7230dbf1f34344ec33755dd929dcb
79 changes: 54 additions & 25 deletions apps/dav/lib/Connector/Sabre/SharesPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ class SharesPlugin extends \Sabre\DAV\ServerPlugin {
*/
private $userFolder;

/** @var IShare[] */
private $cachedShares;

/**
* @var IShare[]
*/
Expand Down Expand Up @@ -169,6 +172,41 @@ private function getSharesTypesInFolder(\OCP\Files\Folder $node) {
return $shareTypesByFileId;
}

private function getShare(\OCP\Files\Node $node): array {
$result = [];
$requestedShareTypes = [
\OCP\Share::SHARE_TYPE_USER,
\OCP\Share::SHARE_TYPE_GROUP,
\OCP\Share::SHARE_TYPE_LINK,
\OCP\Share::SHARE_TYPE_REMOTE,
\OCP\Share::SHARE_TYPE_EMAIL,
\OCP\Share::SHARE_TYPE_ROOM,
\OCP\Share::SHARE_TYPE_CIRCLE,
];
foreach ($requestedShareTypes as $requestedShareType) {
// one of each type is enough to find out about the types
$shares = $this->shareManager->getSharesBy(
$this->userId,
$requestedShareType,
$node,
false,
1
);
foreach ($shares as $share) {
$result[] = $share;
}
}
return $result;
}

private function getSharesFolder(\OCP\Files\Folder $node): array {
return $this->shareManager->getSharesInFolder(
$this->userId,
$node,
true
);
}

/**
* Adds shares to propfind response
*
Expand All @@ -186,7 +224,10 @@ public function handleGetProperties(
// need prefetch ?
if ($sabreNode instanceof \OCA\DAV\Connector\Sabre\Directory
&& $propFind->getDepth() !== 0
&& !is_null($propFind->getStatus(self::SHARETYPES_PROPERTYNAME))
&& (
!is_null($propFind->getStatus(self::SHARETYPES_PROPERTYNAME)) ||
!is_null($propFind->getStatus(self::SHAREES_PROPERTYNAME))
)
) {
$folderNode = $this->userFolder->get($sabreNode->getPath());

Expand All @@ -196,6 +237,11 @@ public function handleGetProperties(
foreach ($childShares as $id => $shares) {
$this->cachedShareTypes[$id] = $shares;
}

$childShares = $this->getSharesFolder($folderNode);
foreach ($childShares as $id => $shares) {
$this->cachedShares[$id] = $shares;
}
}

$propFind->handle(self::SHARETYPES_PROPERTYNAME, function () use ($sabreNode) {
Expand All @@ -219,21 +265,8 @@ public function handleGetProperties(
});

$propFind->handle(self::SHAREES_PROPERTYNAME, function() use ($sabreNode) {
$user = $this->server->httpRequest->getRawServerValue('PHP_AUTH_USER');

if ($user == null) {
return [];
}

if ($sabreNode->getPath() === "/") {
return [];
}

$userFolder = \OC::$server->getRootFolder()->getUserFolder($user);
$path = $userFolder->get($sabreNode->getPath());

if (isset($this->cachedShareTypes[$sabreNode->getId()])) {
$shareTypes = $this->cachedShareTypes[$sabreNode->getId()];
if (isset($this->cachedShares[$sabreNode->getId()])) {
$shares = $this->cachedShares[$sabreNode->getId()];
} else {
list($parentPath,) = \Sabre\Uri\split($sabreNode->getPath());
if ($parentPath === '') {
Expand All @@ -242,21 +275,17 @@ public function handleGetProperties(
// if we already cached the folder this file is in we know there are no shares for this file
if (array_search($parentPath, $this->cachedFolders) === false) {
$node = $this->userFolder->get($sabreNode->getPath());
$shareTypes = $this->getShareTypes($node);
$shares = $this->getShare($node);
} else {
return [];
}
}

foreach ($shareTypes as $shareType) {
$shares = $this->shareManager->getSharesBy($user, $shareType, $path, false, -1, 0);

foreach ($shares as $share) {
if ($share->getSharedBy() === $user) {
$sharees[] = $share->getSharedWith();
}
}
$sharees = [];
foreach ($shares as $share) {
$sharees[] = $share->getSharedWith();
}

return implode(', ', $sharees);
});
}
Expand Down