1
0

Application.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\Files_Trashbin\AppInfo;
  27. use OCA\DAV\Connector\Sabre\Principal;
  28. use OCA\Files\Event\LoadAdditionalScriptsEvent;
  29. use OCA\Files_Trashbin\Capabilities;
  30. use OCA\Files_Trashbin\Events\BeforeNodeRestoredEvent;
  31. use OCA\Files_Trashbin\Expiration;
  32. use OCA\Files_Trashbin\Listeners\LoadAdditionalScripts;
  33. use OCA\Files_Trashbin\Listeners\SyncLivePhotosListener;
  34. use OCA\Files_Trashbin\Trash\ITrashManager;
  35. use OCA\Files_Trashbin\Trash\TrashManager;
  36. use OCA\Files_Trashbin\UserMigration\TrashbinMigrator;
  37. use OCP\App\IAppManager;
  38. use OCP\AppFramework\App;
  39. use OCP\AppFramework\Bootstrap\IBootContext;
  40. use OCP\AppFramework\Bootstrap\IBootstrap;
  41. use OCP\AppFramework\Bootstrap\IRegistrationContext;
  42. use Psr\Container\ContainerInterface;
  43. use Psr\Log\LoggerInterface;
  44. class Application extends App implements IBootstrap {
  45. public const APP_ID = 'files_trashbin';
  46. public function __construct(array $urlParams = []) {
  47. parent::__construct(self::APP_ID, $urlParams);
  48. }
  49. public function register(IRegistrationContext $context): void {
  50. $context->registerCapability(Capabilities::class);
  51. $context->registerServiceAlias('Expiration', Expiration::class);
  52. $context->registerServiceAlias(ITrashManager::class, TrashManager::class);
  53. /** Register $principalBackend for the DAV collection */
  54. $context->registerServiceAlias('principalBackend', Principal::class);
  55. $context->registerUserMigrator(TrashbinMigrator::class);
  56. $context->registerEventListener(
  57. LoadAdditionalScriptsEvent::class,
  58. LoadAdditionalScripts::class
  59. );
  60. $context->registerEventListener(BeforeNodeRestoredEvent::class, SyncLivePhotosListener::class);
  61. }
  62. public function boot(IBootContext $context): void {
  63. $context->injectFn([$this, 'registerTrashBackends']);
  64. // create storage wrapper on setup
  65. \OCP\Util::connectHook('OC_Filesystem', 'preSetup', 'OCA\Files_Trashbin\Storage', 'setupStorage');
  66. //Listen to delete user signal
  67. \OCP\Util::connectHook('OC_User', 'pre_deleteUser', 'OCA\Files_Trashbin\Hooks', 'deleteUser_hook');
  68. //Listen to post write hook
  69. \OCP\Util::connectHook('OC_Filesystem', 'post_write', 'OCA\Files_Trashbin\Hooks', 'post_write_hook');
  70. // pre and post-rename, disable trash logic for the copy+unlink case
  71. \OCP\Util::connectHook('OC_Filesystem', 'delete', 'OCA\Files_Trashbin\Trashbin', 'ensureFileScannedHook');
  72. }
  73. public function registerTrashBackends(ContainerInterface $serverContainer, LoggerInterface $logger, IAppManager $appManager, ITrashManager $trashManager): void {
  74. foreach ($appManager->getInstalledApps() as $app) {
  75. $appInfo = $appManager->getAppInfo($app);
  76. if (isset($appInfo['trash'])) {
  77. $backends = $appInfo['trash'];
  78. foreach ($backends as $backend) {
  79. $class = $backend['@value'];
  80. $for = $backend['@attributes']['for'];
  81. try {
  82. $backendObject = $serverContainer->get($class);
  83. $trashManager->registerBackend($for, $backendObject);
  84. } catch (\Exception $e) {
  85. $logger->error($e->getMessage(), ['exception' => $e]);
  86. }
  87. }
  88. }
  89. }
  90. }
  91. }