1
0

Application.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2018-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_Trashbin\AppInfo;
  8. use OCA\DAV\Connector\Sabre\Principal;
  9. use OCA\Files\Event\LoadAdditionalScriptsEvent;
  10. use OCA\Files_Trashbin\Capabilities;
  11. use OCA\Files_Trashbin\Events\BeforeNodeRestoredEvent;
  12. use OCA\Files_Trashbin\Expiration;
  13. use OCA\Files_Trashbin\Listeners\LoadAdditionalScripts;
  14. use OCA\Files_Trashbin\Listeners\SyncLivePhotosListener;
  15. use OCA\Files_Trashbin\Trash\ITrashManager;
  16. use OCA\Files_Trashbin\Trash\TrashManager;
  17. use OCA\Files_Trashbin\UserMigration\TrashbinMigrator;
  18. use OCP\App\IAppManager;
  19. use OCP\AppFramework\App;
  20. use OCP\AppFramework\Bootstrap\IBootContext;
  21. use OCP\AppFramework\Bootstrap\IBootstrap;
  22. use OCP\AppFramework\Bootstrap\IRegistrationContext;
  23. use Psr\Container\ContainerInterface;
  24. use Psr\Log\LoggerInterface;
  25. class Application extends App implements IBootstrap {
  26. public const APP_ID = 'files_trashbin';
  27. public function __construct(array $urlParams = []) {
  28. parent::__construct(self::APP_ID, $urlParams);
  29. }
  30. public function register(IRegistrationContext $context): void {
  31. $context->registerCapability(Capabilities::class);
  32. $context->registerServiceAlias('Expiration', Expiration::class);
  33. $context->registerServiceAlias(ITrashManager::class, TrashManager::class);
  34. /** Register $principalBackend for the DAV collection */
  35. $context->registerServiceAlias('principalBackend', Principal::class);
  36. $context->registerUserMigrator(TrashbinMigrator::class);
  37. $context->registerEventListener(
  38. LoadAdditionalScriptsEvent::class,
  39. LoadAdditionalScripts::class
  40. );
  41. $context->registerEventListener(BeforeNodeRestoredEvent::class, SyncLivePhotosListener::class);
  42. }
  43. public function boot(IBootContext $context): void {
  44. $context->injectFn([$this, 'registerTrashBackends']);
  45. // create storage wrapper on setup
  46. \OCP\Util::connectHook('OC_Filesystem', 'preSetup', 'OCA\Files_Trashbin\Storage', 'setupStorage');
  47. //Listen to delete user signal
  48. \OCP\Util::connectHook('OC_User', 'pre_deleteUser', 'OCA\Files_Trashbin\Hooks', 'deleteUser_hook');
  49. //Listen to post write hook
  50. \OCP\Util::connectHook('OC_Filesystem', 'post_write', 'OCA\Files_Trashbin\Hooks', 'post_write_hook');
  51. // pre and post-rename, disable trash logic for the copy+unlink case
  52. \OCP\Util::connectHook('OC_Filesystem', 'delete', 'OCA\Files_Trashbin\Trashbin', 'ensureFileScannedHook');
  53. }
  54. public function registerTrashBackends(ContainerInterface $serverContainer, LoggerInterface $logger, IAppManager $appManager, ITrashManager $trashManager): void {
  55. foreach ($appManager->getInstalledApps() as $app) {
  56. $appInfo = $appManager->getAppInfo($app);
  57. if (isset($appInfo['trash'])) {
  58. $backends = $appInfo['trash'];
  59. foreach ($backends as $backend) {
  60. $class = $backend['@value'];
  61. $for = $backend['@attributes']['for'];
  62. try {
  63. $backendObject = $serverContainer->get($class);
  64. $trashManager->registerBackend($for, $backendObject);
  65. } catch (\Exception $e) {
  66. $logger->error($e->getMessage(), ['exception' => $e]);
  67. }
  68. }
  69. }
  70. }
  71. }
  72. }