ExpireTrash.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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\AppInfo\Application;
  29. use OCA\Files_Trashbin\Expiration;
  30. use OCA\Files_Trashbin\Helper;
  31. use OCA\Files_Trashbin\Trashbin;
  32. use OCP\AppFramework\Utility\ITimeFactory;
  33. use OCP\BackgroundJob\TimedJob;
  34. use OCP\IConfig;
  35. use OCP\IUser;
  36. use OCP\IUserManager;
  37. class ExpireTrash extends TimedJob {
  38. private IConfig $config;
  39. private Expiration $expiration;
  40. private IUserManager $userManager;
  41. public function __construct(
  42. IConfig $config,
  43. IUserManager $userManager,
  44. Expiration $expiration,
  45. ITimeFactory $time
  46. ) {
  47. parent::__construct($time);
  48. // Run once per 30 minutes
  49. $this->setInterval(60 * 30);
  50. $this->config = $config;
  51. $this->userManager = $userManager;
  52. $this->expiration = $expiration;
  53. }
  54. /**
  55. * @param $argument
  56. * @throws \Exception
  57. */
  58. protected function run($argument) {
  59. $backgroundJob = $this->config->getAppValue('files_trashbin', 'background_job_expire_trash', 'yes');
  60. if ($backgroundJob === 'no') {
  61. return;
  62. }
  63. $maxAge = $this->expiration->getMaxAgeAsTimestamp();
  64. if (!$maxAge) {
  65. return;
  66. }
  67. $this->userManager->callForSeenUsers(function (IUser $user) {
  68. $uid = $user->getUID();
  69. if (!$this->setupFS($uid)) {
  70. return;
  71. }
  72. $dirContent = Helper::getTrashFiles('/', $uid, 'mtime');
  73. Trashbin::deleteExpiredFiles($dirContent, $uid);
  74. });
  75. \OC_Util::tearDownFS();
  76. }
  77. /**
  78. * Act on behalf on trash item owner
  79. */
  80. protected function setupFS(string $user): bool {
  81. \OC_Util::tearDownFS();
  82. \OC_Util::setupFS($user);
  83. // Check if this user has a trashbin directory
  84. $view = new \OC\Files\View('/' . $user);
  85. if (!$view->is_dir('/files_trashbin/files')) {
  86. return false;
  87. }
  88. return true;
  89. }
  90. }