1
0

ExpireVersions.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  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_Versions\BackgroundJob;
  27. use OCA\Files_Versions\Expiration;
  28. use OCA\Files_Versions\Storage;
  29. use OCP\AppFramework\Utility\ITimeFactory;
  30. use OCP\BackgroundJob\TimedJob;
  31. use OCP\IConfig;
  32. use OCP\IUser;
  33. use OCP\IUserManager;
  34. class ExpireVersions extends TimedJob {
  35. public const ITEMS_PER_SESSION = 1000;
  36. private IConfig $config;
  37. private Expiration $expiration;
  38. private IUserManager $userManager;
  39. public function __construct(IConfig $config, IUserManager $userManager, Expiration $expiration, ITimeFactory $time) {
  40. parent::__construct($time);
  41. // Run once per 30 minutes
  42. $this->setInterval(60 * 30);
  43. $this->config = $config;
  44. $this->expiration = $expiration;
  45. $this->userManager = $userManager;
  46. }
  47. public function run($argument) {
  48. $backgroundJob = $this->config->getAppValue('files_versions', 'background_job_expire_versions', 'yes');
  49. if ($backgroundJob === 'no') {
  50. return;
  51. }
  52. $maxAge = $this->expiration->getMaxAgeAsTimestamp();
  53. if (!$maxAge) {
  54. return;
  55. }
  56. $this->userManager->callForSeenUsers(function (IUser $user) {
  57. $uid = $user->getUID();
  58. if (!$this->setupFS($uid)) {
  59. return;
  60. }
  61. Storage::expireOlderThanMaxForUser($uid);
  62. });
  63. }
  64. /**
  65. * Act on behalf on trash item owner
  66. */
  67. protected function setupFS(string $user): bool {
  68. \OC_Util::tearDownFS();
  69. \OC_Util::setupFS($user);
  70. // Check if this user has a versions directory
  71. $view = new \OC\Files\View('/' . $user);
  72. if (!$view->is_dir('/files_versions')) {
  73. return false;
  74. }
  75. return true;
  76. }
  77. }