ExpireTrash.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-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\BackgroundJob;
  8. use OCA\Files_Trashbin\Expiration;
  9. use OCA\Files_Trashbin\Helper;
  10. use OCA\Files_Trashbin\Trashbin;
  11. use OCP\AppFramework\Utility\ITimeFactory;
  12. use OCP\BackgroundJob\TimedJob;
  13. use OCP\IConfig;
  14. use OCP\IUser;
  15. use OCP\IUserManager;
  16. class ExpireTrash extends TimedJob {
  17. private IConfig $config;
  18. private Expiration $expiration;
  19. private IUserManager $userManager;
  20. public function __construct(
  21. IConfig $config,
  22. IUserManager $userManager,
  23. Expiration $expiration,
  24. ITimeFactory $time
  25. ) {
  26. parent::__construct($time);
  27. // Run once per 30 minutes
  28. $this->setInterval(60 * 30);
  29. $this->config = $config;
  30. $this->userManager = $userManager;
  31. $this->expiration = $expiration;
  32. }
  33. /**
  34. * @param $argument
  35. * @throws \Exception
  36. */
  37. protected function run($argument) {
  38. $backgroundJob = $this->config->getAppValue('files_trashbin', 'background_job_expire_trash', 'yes');
  39. if ($backgroundJob === 'no') {
  40. return;
  41. }
  42. $maxAge = $this->expiration->getMaxAgeAsTimestamp();
  43. if (!$maxAge) {
  44. return;
  45. }
  46. $this->userManager->callForSeenUsers(function (IUser $user) {
  47. $uid = $user->getUID();
  48. if (!$this->setupFS($uid)) {
  49. return;
  50. }
  51. $dirContent = Helper::getTrashFiles('/', $uid, 'mtime');
  52. Trashbin::deleteExpiredFiles($dirContent, $uid);
  53. });
  54. \OC_Util::tearDownFS();
  55. }
  56. /**
  57. * Act on behalf on trash item owner
  58. */
  59. protected function setupFS(string $user): bool {
  60. \OC_Util::tearDownFS();
  61. \OC_Util::setupFS($user);
  62. // Check if this user has a trashbin directory
  63. $view = new \OC\Files\View('/' . $user);
  64. if (!$view->is_dir('/files_trashbin/files')) {
  65. return false;
  66. }
  67. return true;
  68. }
  69. }