Skip to content

Commit f282151

Browse files
authored
Merge pull request #1153 from nextcloud/bugfix/noid/share-error
Avoid throwing on other share types than user
2 parents a6aaf32 + 1b9e787 commit f282151

File tree

2 files changed

+33
-16
lines changed

2 files changed

+33
-16
lines changed

lib/AppInfo/Application.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ public function register(IRegistrationContext $context): void {
3737
);
3838
} else {
3939
// FIXME: Remove once Nextcloud 28 is the minimum supported version
40-
\OCP\Server::get(IEventDispatcher::class)->addListener('OCP\Share::preShare', function (GenericEvent $event) {
40+
\OCP\Server::get(IEventDispatcher::class)->addListener('OCP\Share::preShare', function ($event) {
41+
if (!$event instanceof GenericEvent) {
42+
return;
43+
}
44+
4145
/** @var IShare $share */
4246
$share = $event->getSubject();
4347

lib/AppInfo/BeforeShareCreatedListener.php

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@
1111
use OCP\Files\File;
1212
use OCP\Share\Events\BeforeShareCreatedEvent;
1313
use OCP\Share\IShare;
14+
use Psr\Log\LoggerInterface;
1415

1516
class BeforeShareCreatedListener implements IEventListener {
1617
private SettingsService $settings;
1718
private NoteUtil $noteUtil;
1819

19-
public function __construct(SettingsService $settings, NoteUtil $noteUtil) {
20+
public function __construct(SettingsService $settings, NoteUtil $noteUtil, LoggerInterface $logger) {
2021
$this->settings = $settings;
2122
$this->noteUtil = $noteUtil;
23+
$this->logger = $logger;
2224
}
2325

2426
public function handle(Event $event): void {
@@ -31,22 +33,33 @@ public function handle(Event $event): void {
3133

3234
public function overwriteShareTarget(IShare $share): void {
3335
$itemType = $share->getNode() instanceof File ? 'file' : 'folder';
34-
$fileSourcePath = $share->getNode()->getPath();
35-
$itemTarget = $share->getTarget();
36-
$uidOwner = $share->getSharedBy();
37-
$ownerPath = $this->noteUtil->getRoot()->getUserFolder($uidOwner)->getPath();
38-
$ownerNotesPath = $ownerPath . '/' . $this->settings->get($uidOwner, 'notesPath');
39-
40-
$receiver = $share->getSharedWith();
41-
$receiverPath = $this->noteUtil->getRoot()->getUserFolder($receiver)->getPath();
42-
$receiverNotesInternalPath = $this->settings->get($receiver, 'notesPath');
43-
$receiverNotesPath = $receiverPath . '/' . $receiverNotesInternalPath;
44-
$this->noteUtil->getOrCreateFolder($receiverNotesPath);
45-
46-
if ($itemType !== 'file' || strpos($fileSourcePath, $ownerNotesPath) !== 0) {
36+
37+
if ($share->getShareType() !== IShare::TYPE_USER) {
4738
return;
4839
}
4940

50-
$share->setTarget('/' . $receiverNotesInternalPath . $itemTarget);
41+
try {
42+
$fileSourcePath = $share->getNode()->getPath();
43+
$itemTarget = $share->getTarget();
44+
$uidOwner = $share->getSharedBy();
45+
$ownerPath = $this->noteUtil->getRoot()->getUserFolder($uidOwner)->getPath();
46+
$ownerNotesPath = $ownerPath . '/' . $this->settings->get($uidOwner, 'notesPath');
47+
48+
$receiver = $share->getSharedWith();
49+
$receiverPath = $this->noteUtil->getRoot()->getUserFolder($receiver)->getPath();
50+
$receiverNotesInternalPath = $this->settings->get($receiver, 'notesPath');
51+
$receiverNotesPath = $receiverPath . '/' . $receiverNotesInternalPath;
52+
$this->noteUtil->getOrCreateFolder($receiverNotesPath);
53+
54+
if ($itemType !== 'file' || strpos($fileSourcePath, $ownerNotesPath) !== 0) {
55+
return;
56+
}
57+
58+
$share->setTarget('/' . $receiverNotesInternalPath . $itemTarget);
59+
} catch (\Throwable $e) {
60+
$this->logger->error('Failed to overwrite share target for notes', [
61+
'exception' => $e,
62+
]);
63+
}
5164
}
5265
}

0 commit comments

Comments
 (0)