ExpireTrash.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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\IUser;
  33. use OCP\IUserManager;
  34. class ExpireTrash extends \OC\BackgroundJob\TimedJob {
  35. /**
  36. * @var Expiration
  37. */
  38. private $expiration;
  39. /**
  40. * @var IUserManager
  41. */
  42. private $userManager;
  43. /**
  44. * @param IUserManager|null $userManager
  45. * @param Expiration|null $expiration
  46. */
  47. public function __construct(IUserManager $userManager = null,
  48. Expiration $expiration = null) {
  49. // Run once per 30 minutes
  50. $this->setInterval(60 * 30);
  51. if (is_null($expiration) || is_null($userManager)) {
  52. $this->fixDIForJobs();
  53. } else {
  54. $this->userManager = $userManager;
  55. $this->expiration = $expiration;
  56. }
  57. }
  58. protected function fixDIForJobs() {
  59. /** @var Application $application */
  60. $application = \OC::$server->query(Application::class);
  61. $this->userManager = \OC::$server->getUserManager();
  62. $this->expiration = $application->getContainer()->query('Expiration');
  63. }
  64. /**
  65. * @param $argument
  66. * @throws \Exception
  67. */
  68. protected function run($argument) {
  69. $maxAge = $this->expiration->getMaxAgeAsTimestamp();
  70. if (!$maxAge) {
  71. return;
  72. }
  73. $this->userManager->callForSeenUsers(function (IUser $user) {
  74. $uid = $user->getUID();
  75. if (!$this->setupFS($uid)) {
  76. return;
  77. }
  78. $dirContent = Helper::getTrashFiles('/', $uid, 'mtime');
  79. Trashbin::deleteExpiredFiles($dirContent, $uid);
  80. });
  81. \OC_Util::tearDownFS();
  82. }
  83. /**
  84. * Act on behalf on trash item owner
  85. * @param string $user
  86. * @return boolean
  87. */
  88. protected function setupFS($user) {
  89. \OC_Util::tearDownFS();
  90. \OC_Util::setupFS($user);
  91. // Check if this user has a trashbin directory
  92. $view = new \OC\Files\View('/' . $user);
  93. if (!$view->is_dir('/files_trashbin/files')) {
  94. return false;
  95. }
  96. return true;
  97. }
  98. }