diff options
author | Côme Chilliet <91878298+come-nc@users.noreply.github.com> | 2024-09-24 17:15:39 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-24 17:15:39 +0200 |
commit | 280f6df66c199b9e72c23ffa921bbdad7c68d3c0 (patch) | |
tree | b26ff144caabbe4094fbdd710dc6dfbab9b0cd22 /apps | |
parent | e247c260715c0dc7db7b4c0649413bed2629aafa (diff) | |
parent | 4adf5fd5b0f409d9f6bd7ffd8b4293e4a1cfa03c (diff) | |
download | nextcloud-server-280f6df66c199b9e72c23ffa921bbdad7c68d3c0.tar.gz nextcloud-server-280f6df66c199b9e72c23ffa921bbdad7c68d3c0.zip |
Merge pull request #32018 from nextcloud/cleanup/event/trashbin
Port files trashbin events to IEventDispatcher/IEventListener
Diffstat (limited to 'apps')
-rw-r--r-- | apps/files/tests/Command/DeleteOrphanedFilesTest.php | 28 | ||||
-rw-r--r-- | apps/files_trashbin/composer/composer/autoload_classmap.php | 2 | ||||
-rw-r--r-- | apps/files_trashbin/composer/composer/autoload_static.php | 2 | ||||
-rw-r--r-- | apps/files_trashbin/lib/AppInfo/Application.php | 22 | ||||
-rw-r--r-- | apps/files_trashbin/lib/Hooks.php | 29 | ||||
-rw-r--r-- | apps/files_trashbin/lib/Listener/EventListener.php | 45 | ||||
-rw-r--r-- | apps/files_trashbin/lib/Storage.php | 6 | ||||
-rw-r--r-- | apps/files_trashbin/lib/Trashbin.php | 24 |
8 files changed, 87 insertions, 71 deletions
diff --git a/apps/files/tests/Command/DeleteOrphanedFilesTest.php b/apps/files/tests/Command/DeleteOrphanedFilesTest.php index 1205e204ec4..6e9fbead34e 100644 --- a/apps/files/tests/Command/DeleteOrphanedFilesTest.php +++ b/apps/files/tests/Command/DeleteOrphanedFilesTest.php @@ -9,7 +9,9 @@ namespace OCA\Files\Tests\Command; use OC\Files\View; use OCA\Files\Command\DeleteOrphanedFiles; +use OCP\Files\IRootFolder; use OCP\Files\StorageNotAvailableException; +use OCP\IDBConnection; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Test\TestCase; @@ -23,25 +25,14 @@ use Test\TestCase; */ class DeleteOrphanedFilesTest extends TestCase { - /** - * @var DeleteOrphanedFiles - */ - private $command; - - /** - * @var \OCP\IDBConnection - */ - private $connection; - - /** - * @var string - */ - private $user1; + private DeleteOrphanedFiles $command; + private IDBConnection $connection; + private string $user1; protected function setUp(): void { parent::setUp(); - $this->connection = \OC::$server->getDatabaseConnection(); + $this->connection = \OCP\Server::get(IDBConnection::class); $this->user1 = $this->getUniqueID('user1_'); @@ -90,12 +81,13 @@ class DeleteOrphanedFilesTest extends TestCase { ->disableOriginalConstructor() ->getMock(); + $rootFolder = \OCP\Server::get(IRootFolder::class); + // scan home storage so that mounts are properly setup - \OC::$server->getRootFolder()->getUserFolder($this->user1)->getStorage()->getScanner()->scan(''); + $rootFolder->getUserFolder($this->user1)->getStorage()->getScanner()->scan(''); $this->loginAsUser($this->user1); - $view = new View('/' . $this->user1 . '/'); $view->mkdir('files/test'); @@ -132,6 +124,8 @@ class DeleteOrphanedFilesTest extends TestCase { $this->assertCount(0, $this->getFile($fileInfo->getId()), 'Asserts that file gets cleaned up'); $this->assertCount(0, $this->getMounts($numericStorageId), 'Asserts that mount gets cleaned up'); + // Rescan folder to add back to cache before deleting + $rootFolder->getUserFolder($this->user1)->getStorage()->getScanner()->scan(''); // since we deleted the storage it might throw a (valid) StorageNotAvailableException try { $view->unlink('files/test'); diff --git a/apps/files_trashbin/composer/composer/autoload_classmap.php b/apps/files_trashbin/composer/composer/autoload_classmap.php index 052063d3550..4ff7c561748 100644 --- a/apps/files_trashbin/composer/composer/autoload_classmap.php +++ b/apps/files_trashbin/composer/composer/autoload_classmap.php @@ -22,7 +22,7 @@ return array( 'OCA\\Files_Trashbin\\Exceptions\\CopyRecursiveException' => $baseDir . '/../lib/Exceptions/CopyRecursiveException.php', 'OCA\\Files_Trashbin\\Expiration' => $baseDir . '/../lib/Expiration.php', 'OCA\\Files_Trashbin\\Helper' => $baseDir . '/../lib/Helper.php', - 'OCA\\Files_Trashbin\\Hooks' => $baseDir . '/../lib/Hooks.php', + 'OCA\\Files_Trashbin\\Listener\\EventListener' => $baseDir . '/../lib/Listener/EventListener.php', 'OCA\\Files_Trashbin\\Listeners\\LoadAdditionalScripts' => $baseDir . '/../lib/Listeners/LoadAdditionalScripts.php', 'OCA\\Files_Trashbin\\Listeners\\SyncLivePhotosListener' => $baseDir . '/../lib/Listeners/SyncLivePhotosListener.php', 'OCA\\Files_Trashbin\\Migration\\Version1010Date20200630192639' => $baseDir . '/../lib/Migration/Version1010Date20200630192639.php', diff --git a/apps/files_trashbin/composer/composer/autoload_static.php b/apps/files_trashbin/composer/composer/autoload_static.php index 4f5227b8889..4b09239aa1e 100644 --- a/apps/files_trashbin/composer/composer/autoload_static.php +++ b/apps/files_trashbin/composer/composer/autoload_static.php @@ -37,7 +37,7 @@ class ComposerStaticInitFiles_Trashbin 'OCA\\Files_Trashbin\\Exceptions\\CopyRecursiveException' => __DIR__ . '/..' . '/../lib/Exceptions/CopyRecursiveException.php', 'OCA\\Files_Trashbin\\Expiration' => __DIR__ . '/..' . '/../lib/Expiration.php', 'OCA\\Files_Trashbin\\Helper' => __DIR__ . '/..' . '/../lib/Helper.php', - 'OCA\\Files_Trashbin\\Hooks' => __DIR__ . '/..' . '/../lib/Hooks.php', + 'OCA\\Files_Trashbin\\Listener\\EventListener' => __DIR__ . '/..' . '/../lib/Listener/EventListener.php', 'OCA\\Files_Trashbin\\Listeners\\LoadAdditionalScripts' => __DIR__ . '/..' . '/../lib/Listeners/LoadAdditionalScripts.php', 'OCA\\Files_Trashbin\\Listeners\\SyncLivePhotosListener' => __DIR__ . '/..' . '/../lib/Listeners/SyncLivePhotosListener.php', 'OCA\\Files_Trashbin\\Migration\\Version1010Date20200630192639' => __DIR__ . '/..' . '/../lib/Migration/Version1010Date20200630192639.php', diff --git a/apps/files_trashbin/lib/AppInfo/Application.php b/apps/files_trashbin/lib/AppInfo/Application.php index e1a8e02afaf..115e6e418b4 100644 --- a/apps/files_trashbin/lib/AppInfo/Application.php +++ b/apps/files_trashbin/lib/AppInfo/Application.php @@ -11,16 +11,22 @@ use OCA\Files\Event\LoadAdditionalScriptsEvent; use OCA\Files_Trashbin\Capabilities; use OCA\Files_Trashbin\Events\BeforeNodeRestoredEvent; use OCA\Files_Trashbin\Expiration; +use OCA\Files_Trashbin\Listener\EventListener; use OCA\Files_Trashbin\Listeners\LoadAdditionalScripts; use OCA\Files_Trashbin\Listeners\SyncLivePhotosListener; use OCA\Files_Trashbin\Trash\ITrashManager; use OCA\Files_Trashbin\Trash\TrashManager; +use OCA\Files_Trashbin\Trashbin; use OCA\Files_Trashbin\UserMigration\TrashbinMigrator; use OCP\App\IAppManager; use OCP\AppFramework\App; use OCP\AppFramework\Bootstrap\IBootContext; use OCP\AppFramework\Bootstrap\IBootstrap; use OCP\AppFramework\Bootstrap\IRegistrationContext; +use OCP\Files\Events\BeforeFileSystemSetupEvent; +use OCP\Files\Events\Node\BeforeNodeDeletedEvent; +use OCP\Files\Events\Node\NodeWrittenEvent; +use OCP\User\Events\BeforeUserDeletedEvent; use Psr\Container\ContainerInterface; use Psr\Log\LoggerInterface; @@ -47,19 +53,17 @@ class Application extends App implements IBootstrap { ); $context->registerEventListener(BeforeNodeRestoredEvent::class, SyncLivePhotosListener::class); + + $context->registerEventListener(NodeWrittenEvent::class, EventListener::class); + $context->registerEventListener(BeforeUserDeletedEvent::class, EventListener::class); + $context->registerEventListener(BeforeFileSystemSetupEvent::class, EventListener::class); + + // pre and post-rename, disable trash logic for the copy+unlink case + $context->registerEventListener(BeforeNodeDeletedEvent::class, Trashbin::class); } public function boot(IBootContext $context): void { $context->injectFn([$this, 'registerTrashBackends']); - - // create storage wrapper on setup - \OCP\Util::connectHook('OC_Filesystem', 'preSetup', 'OCA\Files_Trashbin\Storage', 'setupStorage'); - //Listen to delete user signal - \OCP\Util::connectHook('OC_User', 'pre_deleteUser', 'OCA\Files_Trashbin\Hooks', 'deleteUser_hook'); - //Listen to post write hook - \OCP\Util::connectHook('OC_Filesystem', 'post_write', 'OCA\Files_Trashbin\Hooks', 'post_write_hook'); - // pre and post-rename, disable trash logic for the copy+unlink case - \OCP\Util::connectHook('OC_Filesystem', 'delete', 'OCA\Files_Trashbin\Trashbin', 'ensureFileScannedHook'); } public function registerTrashBackends(ContainerInterface $serverContainer, LoggerInterface $logger, IAppManager $appManager, ITrashManager $trashManager): void { diff --git a/apps/files_trashbin/lib/Hooks.php b/apps/files_trashbin/lib/Hooks.php deleted file mode 100644 index 1e2a8d41098..00000000000 --- a/apps/files_trashbin/lib/Hooks.php +++ /dev/null @@ -1,29 +0,0 @@ -<?php -/** - * SPDX-FileCopyrightText: 2021-2024 Nextcloud GmbH and Nextcloud contributors - * SPDX-FileCopyrightText: 2016 ownCloud, Inc. - * SPDX-License-Identifier: AGPL-3.0-only - */ -namespace OCA\Files_Trashbin; - -class Hooks { - - /** - * clean up user specific settings if user gets deleted - * @param array $params array with uid - * - * This function is connected to the pre_deleteUser signal of OC_Users - * to remove the used space for the trash bin stored in the database - */ - public static function deleteUser_hook($params) { - $uid = $params['uid']; - Trashbin::deleteUser($uid); - } - - public static function post_write_hook($params) { - $user = \OC_User::getUser(); - if (!empty($user)) { - Trashbin::resizeTrash($user); - } - } -} diff --git a/apps/files_trashbin/lib/Listener/EventListener.php b/apps/files_trashbin/lib/Listener/EventListener.php new file mode 100644 index 00000000000..7568524e853 --- /dev/null +++ b/apps/files_trashbin/lib/Listener/EventListener.php @@ -0,0 +1,45 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-only + */ + +namespace OCA\Files_Trashbin\Listener; + +use OCA\Files_Trashbin\Storage; +use OCA\Files_Trashbin\Trashbin; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use OCP\Files\Events\BeforeFileSystemSetupEvent; +use OCP\Files\Events\Node\NodeWrittenEvent; +use OCP\User\Events\BeforeUserDeletedEvent; + +/** @template-implements IEventListener<NodeWrittenEvent|BeforeUserDeletedEvent|BeforeFileSystemSetupEvent> */ +class EventListener implements IEventListener { + private ?string $userId; + + public function __construct(?string $userId = null) { + $this->userId = $userId; + } + + public function handle(Event $event): void { + if ($event instanceof NodeWrittenEvent) { + // Resize trash + if (!empty($this->userId)) { + Trashbin::resizeTrash($this->userId); + } + } + + // Clean up user specific settings if user gets deleted + if ($event instanceof BeforeUserDeletedEvent) { + Trashbin::deleteUser($event->getUser()->getUID()); + } + + if ($event instanceof BeforeFileSystemSetupEvent) { + Storage::setupStorage(); + } + } +} diff --git a/apps/files_trashbin/lib/Storage.php b/apps/files_trashbin/lib/Storage.php index 412b9f44df9..c05d248c9d0 100644 --- a/apps/files_trashbin/lib/Storage.php +++ b/apps/files_trashbin/lib/Storage.php @@ -29,13 +29,7 @@ class Storage extends Wrapper { /** * Storage constructor. - * * @param array $parameters - * @param ITrashManager|null $trashManager - * @param IUserManager|null $userManager - * @param LoggerInterface|null $logger - * @param IEventDispatcher|null $eventDispatcher - * @param IRootFolder|null $rootFolder */ public function __construct( $parameters, diff --git a/apps/files_trashbin/lib/Trashbin.php b/apps/files_trashbin/lib/Trashbin.php index de79f22ae5b..a30ba0ce055 100644 --- a/apps/files_trashbin/lib/Trashbin.php +++ b/apps/files_trashbin/lib/Trashbin.php @@ -11,8 +11,6 @@ use OC\Files\Cache\Cache; use OC\Files\Cache\CacheEntry; use OC\Files\Cache\CacheQueryBuilder; use OC\Files\Filesystem; -use OC\Files\Node\File; -use OC\Files\Node\Folder; use OC\Files\Node\NonExistingFile; use OC\Files\Node\NonExistingFolder; use OC\Files\View; @@ -23,7 +21,12 @@ use OCA\Files_Trashbin\Events\BeforeNodeRestoredEvent; use OCA\Files_Trashbin\Events\NodeRestoredEvent; use OCP\App\IAppManager; use OCP\AppFramework\Utility\ITimeFactory; +use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventDispatcher; +use OCP\EventDispatcher\IEventListener; +use OCP\Files\Events\Node\BeforeNodeDeletedEvent; +use OCP\Files\File; +use OCP\Files\Folder; use OCP\Files\IRootFolder; use OCP\Files\Node; use OCP\Files\NotFoundException; @@ -38,21 +41,20 @@ use OCP\Lock\LockedException; use OCP\Server; use Psr\Log\LoggerInterface; -class Trashbin { +/** @template-implements IEventListener<BeforeNodeDeletedEvent> */ +class Trashbin implements IEventListener { // unit: percentage; 50% of available disk space/quota public const DEFAULTMAXSIZE = 50; /** * Ensure we don't need to scan the file during the move to trash * by triggering the scan in the pre-hook - * - * @param array $params */ - public static function ensureFileScannedHook($params) { + public static function ensureFileScannedHook(Node $node): void { try { - self::getUidAndFilename($params['path']); + self::getUidAndFilename($node->getPath()); } catch (NotFoundException $e) { - // nothing to scan for non existing files + // Nothing to scan for non existing files } } @@ -1170,4 +1172,10 @@ class Trashbin { return new NonExistingFile($rootFolder, $view, $fullPath); } } + + public function handle(Event $event): void { + if ($event instanceof BeforeNodeDeletedEvent) { + self::ensureFileScannedHook($event->getNode()); + } + } } |