Skip to content

Commit f5294e5

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 <[email protected]>
1 parent 2c0f312 commit f5294e5

File tree

14 files changed

+257
-152
lines changed

14 files changed

+257
-152
lines changed

apps/admin_audit/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
'OCA\\AdminAudit\\Actions\\Files' => $baseDir . '/../lib/Actions/Files.php',
1212
'OCA\\AdminAudit\\Actions\\Sharing' => $baseDir . '/../lib/Actions/Sharing.php',
1313
'OCA\\AdminAudit\\Actions\\TagManagement' => $baseDir . '/../lib/Actions/TagManagement.php',
14-
'OCA\\AdminAudit\\Actions\\Trashbin' => $baseDir . '/../lib/Actions/Trashbin.php',
1514
'OCA\\AdminAudit\\Actions\\Versions' => $baseDir . '/../lib/Actions/Versions.php',
1615
'OCA\\AdminAudit\\AppInfo\\Application' => $baseDir . '/../lib/AppInfo/Application.php',
1716
'OCA\\AdminAudit\\AuditLogger' => $baseDir . '/../lib/AuditLogger.php',
@@ -25,5 +24,6 @@
2524
'OCA\\AdminAudit\\Listener\\GroupManagementEventListener' => $baseDir . '/../lib/Listener/GroupManagementEventListener.php',
2625
'OCA\\AdminAudit\\Listener\\SecurityEventListener' => $baseDir . '/../lib/Listener/SecurityEventListener.php',
2726
'OCA\\AdminAudit\\Listener\\SharingEventListener' => $baseDir . '/../lib/Listener/SharingEventListener.php',
27+
'OCA\\AdminAudit\\Listener\\TrashbinEventListener' => $baseDir . '/../lib/Listener/TrashbinEventListener.php',
2828
'OCA\\AdminAudit\\Listener\\UserManagementEventListener' => $baseDir . '/../lib/Listener/UserManagementEventListener.php',
2929
);

apps/admin_audit/composer/composer/autoload_static.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ class ComposerStaticInitAdminAudit
2626
'OCA\\AdminAudit\\Actions\\Files' => __DIR__ . '/..' . '/../lib/Actions/Files.php',
2727
'OCA\\AdminAudit\\Actions\\Sharing' => __DIR__ . '/..' . '/../lib/Actions/Sharing.php',
2828
'OCA\\AdminAudit\\Actions\\TagManagement' => __DIR__ . '/..' . '/../lib/Actions/TagManagement.php',
29-
'OCA\\AdminAudit\\Actions\\Trashbin' => __DIR__ . '/..' . '/../lib/Actions/Trashbin.php',
3029
'OCA\\AdminAudit\\Actions\\Versions' => __DIR__ . '/..' . '/../lib/Actions/Versions.php',
3130
'OCA\\AdminAudit\\AppInfo\\Application' => __DIR__ . '/..' . '/../lib/AppInfo/Application.php',
3231
'OCA\\AdminAudit\\AuditLogger' => __DIR__ . '/..' . '/../lib/AuditLogger.php',
@@ -40,6 +39,7 @@ class ComposerStaticInitAdminAudit
4039
'OCA\\AdminAudit\\Listener\\GroupManagementEventListener' => __DIR__ . '/..' . '/../lib/Listener/GroupManagementEventListener.php',
4140
'OCA\\AdminAudit\\Listener\\SecurityEventListener' => __DIR__ . '/..' . '/../lib/Listener/SecurityEventListener.php',
4241
'OCA\\AdminAudit\\Listener\\SharingEventListener' => __DIR__ . '/..' . '/../lib/Listener/SharingEventListener.php',
42+
'OCA\\AdminAudit\\Listener\\TrashbinEventListener' => __DIR__ . '/..' . '/../lib/Listener/TrashbinEventListener.php',
4343
'OCA\\AdminAudit\\Listener\\UserManagementEventListener' => __DIR__ . '/..' . '/../lib/Listener/UserManagementEventListener.php',
4444
);
4545

apps/admin_audit/lib/Actions/Trashbin.php

Lines changed: 0 additions & 22 deletions
This file was deleted.

apps/admin_audit/lib/AppInfo/Application.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@
2626
use OCA\AdminAudit\Listener\GroupManagementEventListener;
2727
use OCA\AdminAudit\Listener\SecurityEventListener;
2828
use OCA\AdminAudit\Listener\SharingEventListener;
29+
use OCA\AdminAudit\Listener\TrashbinEventListener;
2930
use OCA\AdminAudit\Listener\UserManagementEventListener;
31+
use OCA\Files_Trashbin\Events\BeforeNodeDeletedEvent as TrashbinBeforeNodeDeletedEvent;
32+
use OCA\Files_Trashbin\Events\NodeRestoredEvent;
3033
use OCA\Files_Versions\Events\VersionRestoredEvent;
3134
use OCP\App\Events\AppDisableEvent;
3235
use OCP\App\Events\AppEnableEvent;
@@ -123,6 +126,10 @@ public function register(IRegistrationContext $context): void {
123126

124127
// Console events
125128
$context->registerEventListener(ConsoleEvent::class, ConsoleEventListener::class);
129+
130+
// Trashbin events
131+
$context->registerEventListener(TrashbinBeforeNodeDeletedEvent::class, TrashbinEventListener::class);
132+
$context->registerEventListener(NodeRestoredEvent::class, TrashbinEventListener::class);
126133
}
127134

128135
public function boot(IBootContext $context): void {
@@ -144,7 +151,6 @@ private function registerLegacyHooks(IAuditLogger $logger, ContainerInterface $s
144151
$eventDispatcher = $serverContainer->get(IEventDispatcher::class);
145152
$this->sharingLegacyHooks($logger);
146153
$this->fileHooks($logger, $eventDispatcher);
147-
$this->trashbinHooks($logger);
148154
$this->versionsHooks($logger);
149155
$this->tagHooks($logger, $eventDispatcher);
150156
}
@@ -216,10 +222,4 @@ private function versionsHooks(IAuditLogger $logger): void {
216222
$versionsActions = new Versions($logger);
217223
Util::connectHook('\OCP\Versions', 'delete', $versionsActions, 'delete');
218224
}
219-
220-
private function trashbinHooks(IAuditLogger $logger): void {
221-
$trashActions = new Trashbin($logger);
222-
Util::connectHook('\OCP\Trashbin', 'preDelete', $trashActions, 'delete');
223-
Util::connectHook('\OCA\Files_Trashbin\Trashbin', 'post_restore', $trashActions, 'restore');
224-
}
225225
}
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\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+
18+
/**
19+
* @template-implements IEventListener<BeforeNodeDeletedEvent|NodeRestoredEvent>
20+
*/
21+
class TrashbinEventListener extends Action implements IEventListener {
22+
23+
public function handle(Event $event): void {
24+
if ($event instanceof BeforeNodeDeletedEvent) {
25+
$this->log('File "%s" deleted from trash bin.',
26+
['path' => $event->getSource()->getPath()], ['path']
27+
);
28+
} elseif ($event instanceof NodeRestoredEvent) {
29+
$this->log('File "%s" restored from trash bin.',
30+
['path' => $event->getTarget()->getPath()], ['path']
31+
);
32+
}
33+
}
34+
}

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 32.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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
namespace OCA\Files_Trashbin\Events;
10+
11+
use OCP\EventDispatcher\Event;
12+
use OCP\Files\Node;
13+
14+
/**
15+
* Event send before a node is deleted definitively.
16+
* @since 32.0.0
17+
*/
18+
class BeforeNodeDeletedEvent extends Event {
19+
public function __construct(
20+
private readonly Node $source,
21+
) {
22+
parent::__construct();
23+
}
24+
25+
public function getSource(): Node {
26+
return $this->source;
27+
}
28+
}
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 32.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+
}

0 commit comments

Comments
 (0)