ExpireTrash.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 Lukas Reschke <lukas@statuscode.ch>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\Files_Trashbin\BackgroundJob;
  28. use OCA\Files_Trashbin\Expiration;
  29. use OCA\Files_Trashbin\Helper;
  30. use OCA\Files_Trashbin\Trashbin;
  31. use OCP\AppFramework\Utility\ITimeFactory;
  32. use OCP\BackgroundJob\TimedJob;
  33. use OCP\IConfig;
  34. use OCP\IUser;
  35. use OCP\IUserManager;
  36. class ExpireTrash extends TimedJob {
  37. private IConfig $config;
  38. private Expiration $expiration;
  39. private IUserManager $userManager;
  40. public function __construct(
  41. IConfig $config,
  42. IUserManager $userManager,
  43. Expiration $expiration,
  44. ITimeFactory $time
  45. ) {
  46. parent::__construct($time);
  47. // Run once per 30 minutes
  48. $this->setInterval(60 * 30);
  49. $this->config = $config;
  50. $this->userManager = $userManager;
  51. $this->expiration = $expiration;
  52. }
  53. /**
  54. * @param $argument
  55. * @throws \Exception
  56. */
  57. protected function run($argument) {
  58. $backgroundJob = $this->config->getAppValue('files_trashbin', 'background_job_expire_trash', 'yes');
  59. if ($backgroundJob === 'no') {
  60. return;
  61. }
  62. $maxAge = $this->expiration->getMaxAgeAsTimestamp();
  63. if (!$maxAge) {
  64. return;
  65. }
  66. $this->userManager->callForSeenUsers(function (IUser $user) {
  67. $uid = $user->getUID();
  68. if (!$this->setupFS($uid)) {
  69. return;
  70. }
  71. $dirContent = Helper::getTrashFiles('/', $uid, 'mtime');
  72. Trashbin::deleteExpiredFiles($dirContent, $uid);
  73. });
  74. \OC_Util::tearDownFS();
  75. }
  76. /**
  77. * Act on behalf on trash item owner
  78. */
  79. protected function setupFS(string $user): bool {
  80. \OC_Util::tearDownFS();
  81. \OC_Util::setupFS($user);
  82. // Check if this user has a trashbin directory
  83. $view = new \OC\Files\View('/' . $user);
  84. if (!$view->is_dir('/files_trashbin/files')) {
  85. return false;
  86. }
  87. return true;
  88. }
  89. }