ExpireTrash.php 1.7 KB

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