ExpireVersions.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_Versions\BackgroundJob;
  8. use OCA\Files_Versions\Expiration;
  9. use OCA\Files_Versions\Storage;
  10. use OCP\AppFramework\Utility\ITimeFactory;
  11. use OCP\BackgroundJob\TimedJob;
  12. use OCP\IConfig;
  13. use OCP\IUser;
  14. use OCP\IUserManager;
  15. class ExpireVersions extends TimedJob {
  16. public const ITEMS_PER_SESSION = 1000;
  17. private IConfig $config;
  18. private Expiration $expiration;
  19. private IUserManager $userManager;
  20. public function __construct(IConfig $config, IUserManager $userManager, Expiration $expiration, ITimeFactory $time) {
  21. parent::__construct($time);
  22. // Run once per 30 minutes
  23. $this->setInterval(60 * 30);
  24. $this->config = $config;
  25. $this->expiration = $expiration;
  26. $this->userManager = $userManager;
  27. }
  28. public function run($argument) {
  29. $backgroundJob = $this->config->getAppValue('files_versions', 'background_job_expire_versions', 'yes');
  30. if ($backgroundJob === 'no') {
  31. return;
  32. }
  33. $maxAge = $this->expiration->getMaxAgeAsTimestamp();
  34. if (!$maxAge) {
  35. return;
  36. }
  37. $this->userManager->callForSeenUsers(function (IUser $user) {
  38. $uid = $user->getUID();
  39. if (!$this->setupFS($uid)) {
  40. return;
  41. }
  42. Storage::expireOlderThanMaxForUser($uid);
  43. });
  44. }
  45. /**
  46. * Act on behalf on trash item owner
  47. */
  48. protected function setupFS(string $user): bool {
  49. \OC_Util::tearDownFS();
  50. \OC_Util::setupFS($user);
  51. // Check if this user has a versions directory
  52. $view = new \OC\Files\View('/' . $user);
  53. if (!$view->is_dir('/files_versions')) {
  54. return false;
  55. }
  56. return true;
  57. }
  58. }