Skip to content

Commit 0291ef5

Browse files
author
Carl Schwan
committed
cleanup(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 2211390 commit 0291ef5

File tree

10 files changed

+225
-117
lines changed

10 files changed

+225
-117
lines changed

apps/admin_audit/lib/Actions/Trashbin.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,19 @@
77
*/
88
namespace OCA\AdminAudit\Actions;
99

10+
use OCA\Files_Trashbin\Events\BeforeNodeDeletedEvent;
11+
use OCA\Files_Trashbin\Events\NodeRestoredEvent;
12+
1013
class Trashbin extends Action {
11-
public function delete(array $params): void {
14+
public function delete(BeforeNodeDeletedEvent $beforeNodeDeletedEvent): void {
1215
$this->log('File "%s" deleted from trash bin.',
13-
['path' => $params['path']], ['path']
16+
['path' => $beforeNodeDeletedEvent->getSource()->getPath()], ['path']
1417
);
1518
}
1619

17-
public function restore(array $params): void {
20+
public function restore(NodeRestoredEvent $nodeRestoredEvent): void {
1821
$this->log('File "%s" restored from trash bin.',
19-
['path' => $params['filePath']], ['path']
22+
['path' => $nodeRestoredEvent->getTarget()], ['path']
2023
);
2124
}
2225
}

apps/admin_audit/lib/AppInfo/Application.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
use OCA\AdminAudit\Listener\SecurityEventListener;
2828
use OCA\AdminAudit\Listener\SharingEventListener;
2929
use OCA\AdminAudit\Listener\UserManagementEventListener;
30+
use OCA\Files_Trashbin\Events\NodeRestoredEvent;
31+
use OCA\Files_Trashbin\Events\BeforeNodeDeletedEvent as TrashbinBeforeNodeDeletedEvent;
3032
use OCA\Files_Versions\Events\VersionRestoredEvent;
3133
use OCP\App\Events\AppDisableEvent;
3234
use OCP\App\Events\AppEnableEvent;
@@ -44,6 +46,7 @@
4446
use OCP\Files\Events\Node\BeforeNodeReadEvent;
4547
use OCP\Files\Events\Node\NodeCopiedEvent;
4648
use OCP\Files\Events\Node\NodeCreatedEvent;
49+
use OCP\Files\Events\Node\NodeDeletedEvent;
4750
use OCP\Files\Events\Node\NodeRenamedEvent;
4851
use OCP\Files\Events\Node\NodeWrittenEvent;
4952
use OCP\Group\Events\GroupCreatedEvent;
@@ -217,9 +220,20 @@ private function versionsHooks(IAuditLogger $logger): void {
217220
Util::connectHook('\OCP\Versions', 'delete', $versionsActions, 'delete');
218221
}
219222

220-
private function trashbinHooks(IAuditLogger $logger): void {
223+
private function trashbinHooks(IAuditLogger $logger, IEventDispatcher $eventDispatcher): void {
221224
$trashActions = new Trashbin($logger);
222-
Util::connectHook('\OCP\Trashbin', 'preDelete', $trashActions, 'delete');
223-
Util::connectHook('\OCA\Files_Trashbin\Trashbin', 'post_restore', $trashActions, 'restore');
225+
226+
$eventDispatcher->addListener(TrashbinBeforeNodeDeletedEvent::class,
227+
function (TrashbinBeforeNodeDeletedEvent $event) use ($trashActions): void {
228+
$trashActions->delete($event);
229+
}
230+
);
231+
232+
$eventDispatcher->addListener(
233+
NodeRestoredEvent::class,
234+
function (NodeRestoredEvent $event) use ($trashActions): void {
235+
$trashActions->restore($event);
236+
}
237+
);
224238
}
225239
}

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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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(private readonly array $deletedNodes) {
25+
}
26+
27+
/** @return Node[] */
28+
public function getDeletedNodes(): array {
29+
return $this->deletedNodes;
30+
}
31+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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(private readonly Node $source) {
20+
}
21+
22+
public function getSource(): Node {
23+
return $this->source;
24+
}
25+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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(private readonly array $deletedNodes) {
25+
}
26+
27+
/** @return Node[] */
28+
public function getDeletedNodes(): array {
29+
return $this->deletedNodes;
30+
}
31+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 NodeDeletedEvent extends Event {
19+
public function __construct(private Node $source) {
20+
}
21+
22+
public function getSource(): Node {
23+
return $this->source;
24+
}
25+
}

apps/files_trashbin/lib/Sabre/TrashRoot.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use Sabre\DAV\ICollection;
2020

2121
class TrashRoot implements ICollection {
22-
2322
public function __construct(
2423
private IUser $user,
2524
private ITrashManager $trashManager,
@@ -31,7 +30,7 @@ public function delete() {
3130
throw new Forbidden('Not allowed to delete items from the trash bin');
3231
}
3332

34-
Trashbin::deleteAll();
33+
Trashbin::deleteAll($this->user);
3534
foreach ($this->trashManager->listTrashRoot($this->user) as $trashItem) {
3635
$this->trashManager->removeItem($trashItem);
3736
}

0 commit comments

Comments
 (0)