Skip to content

Commit 0766816

Browse files
committed
Port files trashbin events to IEventDispatcher/IEventListener
oc_hooks is deprecated and will at some point be removed Signed-off-by: Carl Schwan <carl@carlschwan.eu>
1 parent 9a76f06 commit 0766816

16 files changed

Lines changed: 183 additions & 105 deletions

File tree

apps/files_trashbin/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
'OCA\\Files_Trashbin\\Exceptions\\CopyRecursiveException' => $baseDir . '/../lib/Exceptions/CopyRecursiveException.php',
2121
'OCA\\Files_Trashbin\\Expiration' => $baseDir . '/../lib/Expiration.php',
2222
'OCA\\Files_Trashbin\\Helper' => $baseDir . '/../lib/Helper.php',
23-
'OCA\\Files_Trashbin\\Hooks' => $baseDir . '/../lib/Hooks.php',
23+
'OCA\\Files_Trashbin\\Listener\\EventListener' => $baseDir . '/../lib/Listener/EventListener.php',
2424
'OCA\\Files_Trashbin\\Migration\\Version1010Date20200630192639' => $baseDir . '/../lib/Migration/Version1010Date20200630192639.php',
2525
'OCA\\Files_Trashbin\\Sabre\\AbstractTrash' => $baseDir . '/../lib/Sabre/AbstractTrash.php',
2626
'OCA\\Files_Trashbin\\Sabre\\AbstractTrashFile' => $baseDir . '/../lib/Sabre/AbstractTrashFile.php',

apps/files_trashbin/composer/composer/autoload_static.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class ComposerStaticInitFiles_Trashbin
3535
'OCA\\Files_Trashbin\\Exceptions\\CopyRecursiveException' => __DIR__ . '/..' . '/../lib/Exceptions/CopyRecursiveException.php',
3636
'OCA\\Files_Trashbin\\Expiration' => __DIR__ . '/..' . '/../lib/Expiration.php',
3737
'OCA\\Files_Trashbin\\Helper' => __DIR__ . '/..' . '/../lib/Helper.php',
38-
'OCA\\Files_Trashbin\\Hooks' => __DIR__ . '/..' . '/../lib/Hooks.php',
38+
'OCA\\Files_Trashbin\\Listener\\EventListener' => __DIR__ . '/..' . '/../lib/Listener/EventListener.php',
3939
'OCA\\Files_Trashbin\\Migration\\Version1010Date20200630192639' => __DIR__ . '/..' . '/../lib/Migration/Version1010Date20200630192639.php',
4040
'OCA\\Files_Trashbin\\Sabre\\AbstractTrash' => __DIR__ . '/..' . '/../lib/Sabre/AbstractTrash.php',
4141
'OCA\\Files_Trashbin\\Sabre\\AbstractTrashFile' => __DIR__ . '/..' . '/../lib/Sabre/AbstractTrashFile.php',

apps/files_trashbin/lib/AppInfo/Application.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,23 @@
2828
use OCA\DAV\Connector\Sabre\Principal;
2929
use OCA\Files_Trashbin\Capabilities;
3030
use OCA\Files_Trashbin\Expiration;
31+
use OCA\Files_Trashbin\Listener\EventListener;
32+
use OCA\Files_Trashbin\Storage;
3133
use OCA\Files_Trashbin\Trash\ITrashManager;
3234
use OCA\Files_Trashbin\Trash\TrashManager;
35+
use OCA\Files_Trashbin\Trashbin;
3336
use OCA\Files_Trashbin\UserMigration\TrashbinMigrator;
3437
use OCP\AppFramework\App;
3538
use OCP\AppFramework\Bootstrap\IBootContext;
3639
use OCP\AppFramework\Bootstrap\IBootstrap;
3740
use OCP\AppFramework\Bootstrap\IRegistrationContext;
3841
use OCP\App\IAppManager;
42+
use OCP\Files\Events\BeforeFileSystemSetupEvent;
43+
use OCP\Files\Events\Node\BeforeNodeDeletedEvent;
44+
use OCP\Files\Events\Node\NodeWrittenEvent;
3945
use OCP\ILogger;
4046
use OCP\IServerContainer;
47+
use OCP\User\Events\BeforeUserDeletedEvent;
4148

4249
class Application extends App implements IBootstrap {
4350
public const APP_ID = 'files_trashbin';
@@ -55,20 +62,18 @@ public function register(IRegistrationContext $context): void {
5562
$context->registerServiceAlias('principalBackend', Principal::class);
5663

5764
$context->registerUserMigrator(TrashbinMigrator::class);
65+
66+
$context->registerEventListener(NodeWrittenEvent::class, EventListener::class);
67+
$context->registerEventListener(BeforeUserDeletedEvent::class, EventListener::class);
68+
$context->registerEventListener(BeforeFileSystemSetupEvent::class, EventListener::class);
69+
70+
// pre and post-rename, disable trash logic for the copy+unlink case
71+
$context->registerEventListener(BeforeNodeDeletedEvent::class, Trashbin::class);
5872
}
5973

6074
public function boot(IBootContext $context): void {
6175
$context->injectFn([$this, 'registerTrashBackends']);
6276

63-
// create storage wrapper on setup
64-
\OCP\Util::connectHook('OC_Filesystem', 'preSetup', 'OCA\Files_Trashbin\Storage', 'setupStorage');
65-
//Listen to delete user signal
66-
\OCP\Util::connectHook('OC_User', 'pre_deleteUser', 'OCA\Files_Trashbin\Hooks', 'deleteUser_hook');
67-
//Listen to post write hook
68-
\OCP\Util::connectHook('OC_Filesystem', 'post_write', 'OCA\Files_Trashbin\Hooks', 'post_write_hook');
69-
// pre and post-rename, disable trash logic for the copy+unlink case
70-
\OCP\Util::connectHook('OC_Filesystem', 'delete', 'OCA\Files_Trashbin\Trashbin', 'ensureFileScannedHook');
71-
7277
\OCA\Files\App::getNavigationManager()->add(function () {
7378
$l = \OC::$server->getL10N(self::APP_ID);
7479
return [

apps/files_trashbin/lib/Hooks.php

Lines changed: 0 additions & 48 deletions
This file was deleted.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
/**
5+
* @copyright 2022 Carl Schwan <carl@carlschwan.eu>
6+
*
7+
* @license GNU AGPL version 3 or any later version
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Affero General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
25+
namespace OCA\Files_Trashbin\Listener;
26+
27+
use OCA\Files_Trashbin\Storage;
28+
use OCA\Files_Trashbin\Trashbin;
29+
use OCP\EventDispatcher\Event;
30+
use OCP\EventDispatcher\IEventListener;
31+
use OCP\Files\Events\BeforeFileSystemSetupEvent;
32+
use OCP\Files\Events\Node\NodeWrittenEvent;
33+
use OCP\User\Events\BeforeUserDeletedEvent;
34+
35+
class EventListener implements IEventListener {
36+
private ?string $userId;
37+
38+
public function __construct(?string $userId = null) {
39+
$this->userId = $userId;
40+
}
41+
42+
public function handle(Event $event): void {
43+
if ($event instanceof NodeWrittenEvent) {
44+
// Resize trash
45+
if (!empty($this->userId)) {
46+
Trashbin::resizeTrash($this->userId);
47+
}
48+
}
49+
50+
// Clean up user specific settings if user gets deleted
51+
if ($event instanceof BeforeUserDeletedEvent) {
52+
Trashbin::deleteUser($event->getUser()->getUID());
53+
}
54+
55+
if ($event instanceof BeforeFileSystemSetupEvent) {
56+
Storage::setupStorage();
57+
}
58+
}
59+
}

apps/files_trashbin/lib/Storage.php

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -37,48 +37,31 @@
3737
use OCP\Files\Mount\IMountPoint;
3838
use OCP\Files\Node;
3939
use OCP\Files\Storage\IStorage;
40-
use OCP\ILogger;
4140
use OCP\IUserManager;
41+
use Psr\Log\LoggerInterface;
4242
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
4343

4444
class Storage extends Wrapper {
45-
/** @var IMountPoint */
45+
/** @var string */
4646
private $mountPoint;
47-
48-
/** @var IUserManager */
49-
private $userManager;
50-
51-
/** @var ILogger */
52-
private $logger;
53-
54-
/** @var EventDispatcherInterface */
55-
private $eventDispatcher;
56-
57-
/** @var IRootFolder */
58-
private $rootFolder;
59-
60-
/** @var ITrashManager */
61-
private $trashManager;
62-
63-
private $trashEnabled = true;
47+
private IUserManager $userManager;
48+
private LoggerInterface $logger;
49+
private EventDispatcherInterface $eventDispatcher;
50+
private IRootFolder $rootFolder;
51+
private ITrashManager $trashManager;
52+
private bool $trashEnabled = true;
6453

6554
/**
6655
* Storage constructor.
67-
*
6856
* @param array $parameters
69-
* @param ITrashManager $trashManager
70-
* @param IUserManager|null $userManager
71-
* @param ILogger|null $logger
72-
* @param EventDispatcherInterface|null $eventDispatcher
73-
* @param IRootFolder|null $rootFolder
7457
*/
7558
public function __construct(
7659
$parameters,
77-
ITrashManager $trashManager = null,
78-
IUserManager $userManager = null,
79-
ILogger $logger = null,
80-
EventDispatcherInterface $eventDispatcher = null,
81-
IRootFolder $rootFolder = null
60+
?ITrashManager $trashManager = null,
61+
?IUserManager $userManager = null,
62+
?LoggerInterface $logger = null,
63+
?EventDispatcherInterface $eventDispatcher = null,
64+
?IRootFolder $rootFolder = null
8265
) {
8366
$this->mountPoint = $parameters['mountPoint'];
8467
$this->trashManager = $trashManager;
@@ -216,7 +199,7 @@ public static function setupStorage() {
216199
['storage' => $storage, 'mountPoint' => $mountPoint],
217200
\OC::$server->query(ITrashManager::class),
218201
\OC::$server->getUserManager(),
219-
\OC::$server->getLogger(),
202+
\OC::$server->get(LoggerInterface::class),
220203
\OC::$server->getEventDispatcher(),
221204
\OC::$server->getLazyRootFolder()
222205
);

apps/files_trashbin/lib/Trashbin.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
*/
4545
namespace OCA\Files_Trashbin;
4646

47+
use OCP\Files\Node;
4748
use OC_User;
4849
use OC\Files\Cache\Cache;
4950
use OC\Files\Cache\CacheEntry;
@@ -54,6 +55,9 @@
5455
use OCA\Files_Trashbin\AppInfo\Application;
5556
use OCA\Files_Trashbin\Command\Expire;
5657
use OCP\AppFramework\Utility\ITimeFactory;
58+
use OCP\EventDispatcher\Event;
59+
use OCP\EventDispatcher\IEventListener;
60+
use OCP\Files\Events\Node\BeforeNodeDeletedEvent;
5761
use OCP\Files\File;
5862
use OCP\Files\Folder;
5963
use OCP\Files\NotFoundException;
@@ -62,7 +66,7 @@
6266
use OCP\Lock\LockedException;
6367
use Psr\Log\LoggerInterface;
6468

65-
class Trashbin {
69+
class Trashbin implements IEventListener {
6670

6771
// unit: percentage; 50% of available disk space/quota
6872
public const DEFAULTMAXSIZE = 50;
@@ -77,14 +81,12 @@ class Trashbin {
7781
/**
7882
* Ensure we don't need to scan the file during the move to trash
7983
* by triggering the scan in the pre-hook
80-
*
81-
* @param array $params
8284
*/
83-
public static function ensureFileScannedHook($params) {
85+
public static function ensureFileScannedHook(Node $node): void {
8486
try {
85-
self::getUidAndFilename($params['path']);
87+
self::getUidAndFilename($node->getPath());
8688
} catch (NotFoundException $e) {
87-
// nothing to scan for non existing files
89+
// Nothing to scan for non existing files
8890
}
8991
}
9092

@@ -1142,4 +1144,11 @@ public static function isEmpty($user) {
11421144
public static function preview_icon($path) {
11431145
return \OC::$server->getURLGenerator()->linkToRoute('core_ajax_trashbin_preview', ['x' => 32, 'y' => 32, 'file' => $path]);
11441146
}
1147+
1148+
public function handle(Event $event): void {
1149+
if ($event instanceof BeforeNodeDeletedEvent) {
1150+
self::ensureFileScannedHook($event->getNode());
1151+
1152+
}
1153+
}
11451154
}

apps/files_trashbin/tests/StorageTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@
4646
use OCP\Files\IRootFolder;
4747
use OCP\Files\Node;
4848
use OCP\Files\Storage\IStorage;
49-
use OCP\ILogger;
5049
use OCP\IUserManager;
5150
use OCP\Lock\ILockingProvider;
5251
use OCP\Share\IShare;
52+
use Psr\Log\LoggerInterface;
5353
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
5454
use Test\Traits\MountProviderTrait;
5555

@@ -571,7 +571,7 @@ public function testShouldMoveToTrash($mountPoint, $path, $userExists, $appDisab
571571
->disableOriginalConstructor()->getMock();
572572
$userManager->expects($this->any())
573573
->method('userExists')->willReturn($userExists);
574-
$logger = $this->getMockBuilder(ILogger::class)->getMock();
574+
$logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
575575
$eventDispatcher = $this->createMock(EventDispatcherInterface::class);
576576
$rootFolder = $this->createMock(IRootFolder::class);
577577
$userFolder = $this->createMock(Folder::class);

build/psalm-baseline.xml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1687,9 +1687,6 @@
16871687
<InvalidArgument occurrences="1">
16881688
<code>'OCA\Files_Trashbin::moveToTrash'</code>
16891689
</InvalidArgument>
1690-
<InvalidOperand occurrences="1">
1691-
<code>$this-&gt;mountPoint</code>
1692-
</InvalidOperand>
16931690
<TooManyArguments occurrences="1">
16941691
<code>dispatch</code>
16951692
</TooManyArguments>

lib/composer/autoload.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
// autoload.php @generated by Composer
44

5+
if (PHP_VERSION_ID < 50600) {
6+
echo 'Composer 2.3.0 dropped support for autoloading on PHP <5.6 and you are running '.PHP_VERSION.', please upgrade PHP or use Composer 2.2 LTS via "composer self-update --2.2". Aborting.'.PHP_EOL;
7+
exit(1);
8+
}
9+
510
require_once __DIR__ . '/composer/autoload_real.php';
611

712
return ComposerAutoloaderInit53792487c5a8370acc0b06b1a864ff4c::getLoader();

0 commit comments

Comments
 (0)