Skip to content

Commit

Permalink
Port files trashbin events to IEventDispatcher/IEventListener
Browse files Browse the repository at this point in the history
oc_hooks is deprecated and will at some point be removed

Signed-off-by: Carl Schwan <carl@carlschwan.eu>
  • Loading branch information
CarlSchwan committed May 5, 2022
1 parent 9a76f06 commit 0766816
Show file tree
Hide file tree
Showing 16 changed files with 183 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
'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\\Migration\\Version1010Date20200630192639' => $baseDir . '/../lib/Migration/Version1010Date20200630192639.php',
'OCA\\Files_Trashbin\\Sabre\\AbstractTrash' => $baseDir . '/../lib/Sabre/AbstractTrash.php',
'OCA\\Files_Trashbin\\Sabre\\AbstractTrashFile' => $baseDir . '/../lib/Sabre/AbstractTrashFile.php',
Expand Down
2 changes: 1 addition & 1 deletion apps/files_trashbin/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,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\\Migration\\Version1010Date20200630192639' => __DIR__ . '/..' . '/../lib/Migration/Version1010Date20200630192639.php',
'OCA\\Files_Trashbin\\Sabre\\AbstractTrash' => __DIR__ . '/..' . '/../lib/Sabre/AbstractTrash.php',
'OCA\\Files_Trashbin\\Sabre\\AbstractTrashFile' => __DIR__ . '/..' . '/../lib/Sabre/AbstractTrashFile.php',
Expand Down
23 changes: 14 additions & 9 deletions apps/files_trashbin/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,23 @@
use OCA\DAV\Connector\Sabre\Principal;
use OCA\Files_Trashbin\Capabilities;
use OCA\Files_Trashbin\Expiration;
use OCA\Files_Trashbin\Listener\EventListener;
use OCA\Files_Trashbin\Storage;
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\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\App\IAppManager;
use OCP\Files\Events\BeforeFileSystemSetupEvent;
use OCP\Files\Events\Node\BeforeNodeDeletedEvent;
use OCP\Files\Events\Node\NodeWrittenEvent;
use OCP\ILogger;
use OCP\IServerContainer;
use OCP\User\Events\BeforeUserDeletedEvent;

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

$context->registerUserMigrator(TrashbinMigrator::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');

\OCA\Files\App::getNavigationManager()->add(function () {
$l = \OC::$server->getL10N(self::APP_ID);
return [
Expand Down
48 changes: 0 additions & 48 deletions apps/files_trashbin/lib/Hooks.php

This file was deleted.

59 changes: 59 additions & 0 deletions apps/files_trashbin/lib/Listener/EventListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
declare(strict_types=1);

/**
* @copyright 2022 Carl Schwan <carl@carlschwan.eu>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/


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;

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();
}
}
}
45 changes: 14 additions & 31 deletions apps/files_trashbin/lib/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,48 +37,31 @@
use OCP\Files\Mount\IMountPoint;
use OCP\Files\Node;
use OCP\Files\Storage\IStorage;
use OCP\ILogger;
use OCP\IUserManager;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class Storage extends Wrapper {
/** @var IMountPoint */
/** @var string */
private $mountPoint;

/** @var IUserManager */
private $userManager;

/** @var ILogger */
private $logger;

/** @var EventDispatcherInterface */
private $eventDispatcher;

/** @var IRootFolder */
private $rootFolder;

/** @var ITrashManager */
private $trashManager;

private $trashEnabled = true;
private IUserManager $userManager;
private LoggerInterface $logger;
private EventDispatcherInterface $eventDispatcher;
private IRootFolder $rootFolder;
private ITrashManager $trashManager;
private bool $trashEnabled = true;

/**
* Storage constructor.
*
* @param array $parameters
* @param ITrashManager $trashManager
* @param IUserManager|null $userManager
* @param ILogger|null $logger
* @param EventDispatcherInterface|null $eventDispatcher
* @param IRootFolder|null $rootFolder
*/
public function __construct(
$parameters,
ITrashManager $trashManager = null,
IUserManager $userManager = null,
ILogger $logger = null,
EventDispatcherInterface $eventDispatcher = null,
IRootFolder $rootFolder = null
?ITrashManager $trashManager = null,
?IUserManager $userManager = null,
?LoggerInterface $logger = null,
?EventDispatcherInterface $eventDispatcher = null,
?IRootFolder $rootFolder = null
) {
$this->mountPoint = $parameters['mountPoint'];
$this->trashManager = $trashManager;
Expand Down Expand Up @@ -216,7 +199,7 @@ public static function setupStorage() {
['storage' => $storage, 'mountPoint' => $mountPoint],
\OC::$server->query(ITrashManager::class),
\OC::$server->getUserManager(),
\OC::$server->getLogger(),
\OC::$server->get(LoggerInterface::class),
\OC::$server->getEventDispatcher(),
\OC::$server->getLazyRootFolder()
);
Expand Down
21 changes: 15 additions & 6 deletions apps/files_trashbin/lib/Trashbin.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
*/
namespace OCA\Files_Trashbin;

use OCP\Files\Node;
use OC_User;
use OC\Files\Cache\Cache;
use OC\Files\Cache\CacheEntry;
Expand All @@ -54,6 +55,9 @@
use OCA\Files_Trashbin\AppInfo\Application;
use OCA\Files_Trashbin\Command\Expire;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Files\Events\Node\BeforeNodeDeletedEvent;
use OCP\Files\File;
use OCP\Files\Folder;
use OCP\Files\NotFoundException;
Expand All @@ -62,7 +66,7 @@
use OCP\Lock\LockedException;
use Psr\Log\LoggerInterface;

class Trashbin {
class Trashbin implements IEventListener {

// unit: percentage; 50% of available disk space/quota
public const DEFAULTMAXSIZE = 50;
Expand All @@ -77,14 +81,12 @@ class Trashbin {
/**
* 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
}
}

Expand Down Expand Up @@ -1142,4 +1144,11 @@ public static function isEmpty($user) {
public static function preview_icon($path) {
return \OC::$server->getURLGenerator()->linkToRoute('core_ajax_trashbin_preview', ['x' => 32, 'y' => 32, 'file' => $path]);
}

public function handle(Event $event): void {
if ($event instanceof BeforeNodeDeletedEvent) {
self::ensureFileScannedHook($event->getNode());

}
}
}
4 changes: 2 additions & 2 deletions apps/files_trashbin/tests/StorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\Files\Storage\IStorage;
use OCP\ILogger;
use OCP\IUserManager;
use OCP\Lock\ILockingProvider;
use OCP\Share\IShare;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Test\Traits\MountProviderTrait;

Expand Down Expand Up @@ -571,7 +571,7 @@ public function testShouldMoveToTrash($mountPoint, $path, $userExists, $appDisab
->disableOriginalConstructor()->getMock();
$userManager->expects($this->any())
->method('userExists')->willReturn($userExists);
$logger = $this->getMockBuilder(ILogger::class)->getMock();
$logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
$eventDispatcher = $this->createMock(EventDispatcherInterface::class);
$rootFolder = $this->createMock(IRootFolder::class);
$userFolder = $this->createMock(Folder::class);
Expand Down
3 changes: 0 additions & 3 deletions build/psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1687,9 +1687,6 @@
<InvalidArgument occurrences="1">
<code>'OCA\Files_Trashbin::moveToTrash'</code>
</InvalidArgument>
<InvalidOperand occurrences="1">
<code>$this-&gt;mountPoint</code>
</InvalidOperand>
<TooManyArguments occurrences="1">
<code>dispatch</code>
</TooManyArguments>
Expand Down
5 changes: 5 additions & 0 deletions lib/composer/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

// autoload.php @generated by Composer

if (PHP_VERSION_ID < 50600) {
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;
exit(1);
}

require_once __DIR__ . '/composer/autoload_real.php';

return ComposerAutoloaderInit53792487c5a8370acc0b06b1a864ff4c::getLoader();
2 changes: 2 additions & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@
'OCP\\Files\\EmptyFileNameException' => $baseDir . '/lib/public/Files/EmptyFileNameException.php',
'OCP\\Files\\EntityTooLargeException' => $baseDir . '/lib/public/Files/EntityTooLargeException.php',
'OCP\\Files\\Events\\BeforeFileScannedEvent' => $baseDir . '/lib/public/Files/Events/BeforeFileScannedEvent.php',
'OCP\\Files\\Events\\BeforeFileSystemSetupEvent' => $baseDir . '/lib/public/Files/Events/BeforeFileSystemSetupEvent.php',
'OCP\\Files\\Events\\BeforeFolderScannedEvent' => $baseDir . '/lib/public/Files/Events/BeforeFolderScannedEvent.php',
'OCP\\Files\\Events\\FileCacheUpdated' => $baseDir . '/lib/public/Files/Events/FileCacheUpdated.php',
'OCP\\Files\\Events\\FileScannedEvent' => $baseDir . '/lib/public/Files/Events/FileScannedEvent.php',
Expand Down Expand Up @@ -911,6 +912,7 @@
'OC\\Core\\Command\\Maintenance\\Repair' => $baseDir . '/core/Command/Maintenance/Repair.php',
'OC\\Core\\Command\\Maintenance\\UpdateHtaccess' => $baseDir . '/core/Command/Maintenance/UpdateHtaccess.php',
'OC\\Core\\Command\\Maintenance\\UpdateTheme' => $baseDir . '/core/Command/Maintenance/UpdateTheme.php',
'OC\\Core\\Command\\Preview\\Regenerate' => $baseDir . '/core/Command/Preview/Regenerate.php',
'OC\\Core\\Command\\Preview\\Repair' => $baseDir . '/core/Command/Preview/Repair.php',
'OC\\Core\\Command\\Preview\\ResetRenderedTexts' => $baseDir . '/core/Command/Preview/ResetRenderedTexts.php',
'OC\\Core\\Command\\Security\\ImportCertificate' => $baseDir . '/core/Command/Security/ImportCertificate.php',
Expand Down
2 changes: 1 addition & 1 deletion lib/composer/composer/autoload_real.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static function getLoader()
spl_autoload_unregister(array('ComposerAutoloaderInit53792487c5a8370acc0b06b1a864ff4c', 'loadClassLoader'));

require __DIR__ . '/autoload_static.php';
\Composer\Autoload\ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c::getInitializer($loader)();
call_user_func(\Composer\Autoload\ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c::getInitializer($loader));

$loader->register(true);

Expand Down
2 changes: 2 additions & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OCP\\Files\\EmptyFileNameException' => __DIR__ . '/../../..' . '/lib/public/Files/EmptyFileNameException.php',
'OCP\\Files\\EntityTooLargeException' => __DIR__ . '/../../..' . '/lib/public/Files/EntityTooLargeException.php',
'OCP\\Files\\Events\\BeforeFileScannedEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/BeforeFileScannedEvent.php',
'OCP\\Files\\Events\\BeforeFileSystemSetupEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/BeforeFileSystemSetupEvent.php',
'OCP\\Files\\Events\\BeforeFolderScannedEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/BeforeFolderScannedEvent.php',
'OCP\\Files\\Events\\FileCacheUpdated' => __DIR__ . '/../../..' . '/lib/public/Files/Events/FileCacheUpdated.php',
'OCP\\Files\\Events\\FileScannedEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/FileScannedEvent.php',
Expand Down Expand Up @@ -940,6 +941,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OC\\Core\\Command\\Maintenance\\Repair' => __DIR__ . '/../../..' . '/core/Command/Maintenance/Repair.php',
'OC\\Core\\Command\\Maintenance\\UpdateHtaccess' => __DIR__ . '/../../..' . '/core/Command/Maintenance/UpdateHtaccess.php',
'OC\\Core\\Command\\Maintenance\\UpdateTheme' => __DIR__ . '/../../..' . '/core/Command/Maintenance/UpdateTheme.php',
'OC\\Core\\Command\\Preview\\Regenerate' => __DIR__ . '/../../..' . '/core/Command/Preview/Regenerate.php',
'OC\\Core\\Command\\Preview\\Repair' => __DIR__ . '/../../..' . '/core/Command/Preview/Repair.php',
'OC\\Core\\Command\\Preview\\ResetRenderedTexts' => __DIR__ . '/../../..' . '/core/Command/Preview/ResetRenderedTexts.php',
'OC\\Core\\Command\\Security\\ImportCertificate' => __DIR__ . '/../../..' . '/core/Command/Security/ImportCertificate.php',
Expand Down
Loading

0 comments on commit 0766816

Please sign in to comment.