Skip to content

Commit b1d6386

Browse files
Carl SchwanCarlSchwan
authored andcommitted
refactor(trash): Port deletion code of Trashbin to node based API
Instead of using a mix of View and Node based file system manipulation, use the 'new' node based API everywhere. Replace the hooks used in the admin_audit related to the deletion to new typed event that expose the deleted nodes. And remove old calculateSize to get the size of the deleted folder and instead rely on the Node::getSize information. Signed-off-by: Carl Schwan <carlschwan@kde.org>
1 parent 599500d commit b1d6386

13 files changed

Lines changed: 261 additions & 147 deletions

File tree

apps/admin_audit/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@
2626
'OCA\\AdminAudit\\Listener\\SecurityEventListener' => $baseDir . '/../lib/Listener/SecurityEventListener.php',
2727
'OCA\\AdminAudit\\Listener\\SharingEventListener' => $baseDir . '/../lib/Listener/SharingEventListener.php',
2828
'OCA\\AdminAudit\\Listener\\TagEventListener' => $baseDir . '/../lib/Listener/TagEventListener.php',
29+
'OCA\\AdminAudit\\Listener\\TrashbinEventListener' => $baseDir . '/../lib/Listener/TrashbinEventListener.php',
2930
'OCA\\AdminAudit\\Listener\\UserManagementEventListener' => $baseDir . '/../lib/Listener/UserManagementEventListener.php',
3031
);

apps/admin_audit/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class ComposerStaticInitAdminAudit
4141
'OCA\\AdminAudit\\Listener\\SecurityEventListener' => __DIR__ . '/..' . '/../lib/Listener/SecurityEventListener.php',
4242
'OCA\\AdminAudit\\Listener\\SharingEventListener' => __DIR__ . '/..' . '/../lib/Listener/SharingEventListener.php',
4343
'OCA\\AdminAudit\\Listener\\TagEventListener' => __DIR__ . '/..' . '/../lib/Listener/TagEventListener.php',
44+
'OCA\\AdminAudit\\Listener\\TrashbinEventListener' => __DIR__ . '/..' . '/../lib/Listener/TrashbinEventListener.php',
4445
'OCA\\AdminAudit\\Listener\\UserManagementEventListener' => __DIR__ . '/..' . '/../lib/Listener/UserManagementEventListener.php',
4546
);
4647

apps/admin_audit/lib/AppInfo/Application.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@
2727
use OCA\AdminAudit\Listener\SecurityEventListener;
2828
use OCA\AdminAudit\Listener\SharingEventListener;
2929
use OCA\AdminAudit\Listener\TagEventListener;
30+
use OCA\AdminAudit\Listener\TrashbinEventListener;
3031
use OCA\AdminAudit\Listener\UserManagementEventListener;
32+
use OCA\Files_Trashbin\Events\BeforeNodeDeletedEvent as TrashbinBeforeNodeDeletedEvent;
33+
use OCA\Files_Trashbin\Events\NodeRestoredEvent;
3134
use OCA\Files_Versions\Events\VersionRestoredEvent;
3235
use OCP\App\Events\AppDisableEvent;
3336
use OCP\App\Events\AppEnableEvent;
@@ -130,6 +133,10 @@ public function register(IRegistrationContext $context): void {
130133

131134
// System tag event
132135
$context->registerEventListener(TagCreatedEvent::class, TagEventListener::class);
136+
137+
// Trashbin events
138+
$context->registerEventListener(TrashbinBeforeNodeDeletedEvent::class, TrashbinEventListener::class);
139+
$context->registerEventListener(NodeRestoredEvent::class, TrashbinEventListener::class);
133140
}
134141

135142
#[\Override]
@@ -152,7 +159,6 @@ private function registerLegacyHooks(IAuditLogger $logger, ContainerInterface $s
152159
$eventDispatcher = $serverContainer->get(IEventDispatcher::class);
153160
$this->sharingLegacyHooks($logger);
154161
$this->fileHooks($logger, $eventDispatcher);
155-
$this->trashbinHooks($logger);
156162
$this->versionsHooks($logger);
157163
}
158164

@@ -215,10 +221,4 @@ private function versionsHooks(IAuditLogger $logger): void {
215221
$versionsActions = new Versions($logger);
216222
Util::connectHook('\OCP\Versions', 'delete', $versionsActions, 'delete');
217223
}
218-
219-
private function trashbinHooks(IAuditLogger $logger): void {
220-
$trashActions = new Trashbin($logger);
221-
Util::connectHook('\OCP\Trashbin', 'preDelete', $trashActions, 'delete');
222-
Util::connectHook('\OCA\Files_Trashbin\Trashbin', 'post_restore', $trashActions, 'restore');
223-
}
224224
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
namespace OCA\AdminAudit\Listener;
11+
12+
use OCA\AdminAudit\Actions\Action;
13+
use OCA\Files_Trashbin\Events\BeforeNodeDeletedEvent;
14+
use OCA\Files_Trashbin\Events\NodeRestoredEvent;
15+
use OCP\EventDispatcher\Event;
16+
use OCP\EventDispatcher\IEventListener;
17+
use Override;
18+
19+
/**
20+
* @template-implements IEventListener<BeforeNodeDeletedEvent|NodeRestoredEvent>
21+
*/
22+
class TrashbinEventListener extends Action implements IEventListener {
23+
24+
#[Override]
25+
public function handle(Event $event): void {
26+
if ($event instanceof BeforeNodeDeletedEvent) {
27+
$this->log('File "%s" deleted from trash bin.',
28+
['path' => $event->getSource()->getPath()], ['path']
29+
);
30+
} elseif ($event instanceof NodeRestoredEvent) {
31+
$this->log('File "%s" restored from trash bin.',
32+
['path' => $event->getTarget()->getPath()], ['path']
33+
);
34+
}
35+
}
36+
}

apps/files_trashbin/composer/composer/autoload_classmap.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@
1616
'OCA\\Files_Trashbin\\Command\\RestoreAllFiles' => $baseDir . '/../lib/Command/RestoreAllFiles.php',
1717
'OCA\\Files_Trashbin\\Command\\Size' => $baseDir . '/../lib/Command/Size.php',
1818
'OCA\\Files_Trashbin\\Controller\\PreviewController' => $baseDir . '/../lib/Controller/PreviewController.php',
19+
'OCA\\Files_Trashbin\\Events\\BeforeDeleteAllEvent' => $baseDir . '/../lib/Events/BeforeDeleteAllEvent.php',
20+
'OCA\\Files_Trashbin\\Events\\BeforeNodeDeletedEvent' => $baseDir . '/../lib/Events/BeforeNodeDeletedEvent.php',
1921
'OCA\\Files_Trashbin\\Events\\BeforeNodeRestoredEvent' => $baseDir . '/../lib/Events/BeforeNodeRestoredEvent.php',
22+
'OCA\\Files_Trashbin\\Events\\DeleteAllEvent' => $baseDir . '/../lib/Events/DeleteAllEvent.php',
2023
'OCA\\Files_Trashbin\\Events\\MoveToTrashEvent' => $baseDir . '/../lib/Events/MoveToTrashEvent.php',
24+
'OCA\\Files_Trashbin\\Events\\NodeDeletedEvent' => $baseDir . '/../lib/Events/NodeDeletedEvent.php',
2125
'OCA\\Files_Trashbin\\Events\\NodeRestoredEvent' => $baseDir . '/../lib/Events/NodeRestoredEvent.php',
2226
'OCA\\Files_Trashbin\\Exceptions\\CopyRecursiveException' => $baseDir . '/../lib/Exceptions/CopyRecursiveException.php',
2327
'OCA\\Files_Trashbin\\Expiration' => $baseDir . '/../lib/Expiration.php',

apps/files_trashbin/composer/composer/autoload_static.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,12 @@ class ComposerStaticInitFiles_Trashbin
3131
'OCA\\Files_Trashbin\\Command\\RestoreAllFiles' => __DIR__ . '/..' . '/../lib/Command/RestoreAllFiles.php',
3232
'OCA\\Files_Trashbin\\Command\\Size' => __DIR__ . '/..' . '/../lib/Command/Size.php',
3333
'OCA\\Files_Trashbin\\Controller\\PreviewController' => __DIR__ . '/..' . '/../lib/Controller/PreviewController.php',
34+
'OCA\\Files_Trashbin\\Events\\BeforeDeleteAllEvent' => __DIR__ . '/..' . '/../lib/Events/BeforeDeleteAllEvent.php',
35+
'OCA\\Files_Trashbin\\Events\\BeforeNodeDeletedEvent' => __DIR__ . '/..' . '/../lib/Events/BeforeNodeDeletedEvent.php',
3436
'OCA\\Files_Trashbin\\Events\\BeforeNodeRestoredEvent' => __DIR__ . '/..' . '/../lib/Events/BeforeNodeRestoredEvent.php',
37+
'OCA\\Files_Trashbin\\Events\\DeleteAllEvent' => __DIR__ . '/..' . '/../lib/Events/DeleteAllEvent.php',
3538
'OCA\\Files_Trashbin\\Events\\MoveToTrashEvent' => __DIR__ . '/..' . '/../lib/Events/MoveToTrashEvent.php',
39+
'OCA\\Files_Trashbin\\Events\\NodeDeletedEvent' => __DIR__ . '/..' . '/../lib/Events/NodeDeletedEvent.php',
3640
'OCA\\Files_Trashbin\\Events\\NodeRestoredEvent' => __DIR__ . '/..' . '/../lib/Events/NodeRestoredEvent.php',
3741
'OCA\\Files_Trashbin\\Exceptions\\CopyRecursiveException' => __DIR__ . '/..' . '/../lib/Exceptions/CopyRecursiveException.php',
3842
'OCA\\Files_Trashbin\\Expiration' => __DIR__ . '/..' . '/../lib/Expiration.php',
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
namespace OCA\Files_Trashbin\Events;
11+
12+
use OCP\EventDispatcher\Event;
13+
use OCP\Files\Node;
14+
15+
/**
16+
* Event send before emptying the trash.
17+
* @since 35.0.0
18+
*/
19+
class BeforeDeleteAllEvent extends Event {
20+
21+
/**
22+
* @param Node[] $deletedNodes
23+
*/
24+
public function __construct(
25+
private readonly array $deletedNodes,
26+
) {
27+
parent::__construct();
28+
}
29+
30+
/** @return Node[] */
31+
public function getDeletedNodes(): array {
32+
return $this->deletedNodes;
33+
}
34+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
namespace OCA\Files_Trashbin\Events;
11+
12+
use OCP\EventDispatcher\Event;
13+
use OCP\Files\Node;
14+
15+
/**
16+
* Event send before a node is deleted definitively.
17+
* @since 35.0.0
18+
*/
19+
class BeforeNodeDeletedEvent extends Event {
20+
public function __construct(
21+
private readonly Node $source,
22+
) {
23+
parent::__construct();
24+
}
25+
26+
public function getSource(): Node {
27+
return $this->source;
28+
}
29+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
namespace OCA\Files_Trashbin\Events;
11+
12+
use OCP\EventDispatcher\Event;
13+
use OCP\Files\Node;
14+
15+
/**
16+
* Event send before emptying the trash.
17+
* @since 35.0.0
18+
*/
19+
class DeleteAllEvent extends Event {
20+
21+
/**
22+
* @param Node[] $deletedNodes
23+
*/
24+
public function __construct(
25+
private readonly array $deletedNodes,
26+
) {
27+
parent::__construct();
28+
}
29+
30+
/** @return Node[] */
31+
public function getDeletedNodes(): array {
32+
return $this->deletedNodes;
33+
}
34+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
namespace OCA\Files_Trashbin\Events;
11+
12+
use OCP\EventDispatcher\Event;
13+
use OCP\Files\Node;
14+
15+
/**
16+
* Event send before a node is deleted definitively.
17+
*
18+
* @since 35.0.0
19+
*/
20+
class NodeDeletedEvent extends Event {
21+
public function __construct(
22+
private readonly Node $source,
23+
) {
24+
parent::__construct();
25+
}
26+
27+
public function getSource(): Node {
28+
return $this->source;
29+
}
30+
}

0 commit comments

Comments
 (0)