ExpireVersions.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 OC\Files\View;
  9. use OCA\Files_Versions\Expiration;
  10. use OCA\Files_Versions\Storage;
  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 ExpireVersions extends TimedJob {
  17. public const ITEMS_PER_SESSION = 1000;
  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. 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): void {
  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 View('/' . $user);
  53. if (!$view->is_dir('/files_versions')) {
  54. return false;
  55. }
  56. return true;
  57. }
  58. }