ExpireTrash.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\Files_Trashbin\BackgroundJob;
  26. use OCP\IUser;
  27. use OCP\IUserManager;
  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. class ExpireTrash extends \OC\BackgroundJob\TimedJob {
  33. /**
  34. * @var Expiration
  35. */
  36. private $expiration;
  37. /**
  38. * @var IUserManager
  39. */
  40. private $userManager;
  41. /**
  42. * @param IUserManager|null $userManager
  43. * @param Expiration|null $expiration
  44. */
  45. public function __construct(IUserManager $userManager = null,
  46. Expiration $expiration = null) {
  47. // Run once per 30 minutes
  48. $this->setInterval(60 * 30);
  49. if (is_null($expiration) || is_null($userManager)) {
  50. $this->fixDIForJobs();
  51. } else {
  52. $this->userManager = $userManager;
  53. $this->expiration = $expiration;
  54. }
  55. }
  56. protected function fixDIForJobs() {
  57. $application = new Application();
  58. $this->userManager = \OC::$server->getUserManager();
  59. $this->expiration = $application->getContainer()->query('Expiration');
  60. }
  61. /**
  62. * @param $argument
  63. * @throws \Exception
  64. */
  65. protected function run($argument) {
  66. $maxAge = $this->expiration->getMaxAgeAsTimestamp();
  67. if (!$maxAge) {
  68. return;
  69. }
  70. $this->userManager->callForSeenUsers(function(IUser $user) {
  71. $uid = $user->getUID();
  72. if (!$this->setupFS($uid)) {
  73. return;
  74. }
  75. $dirContent = Helper::getTrashFiles('/', $uid, 'mtime');
  76. Trashbin::deleteExpiredFiles($dirContent, $uid);
  77. });
  78. \OC_Util::tearDownFS();
  79. }
  80. /**
  81. * Act on behalf on trash item owner
  82. * @param string $user
  83. * @return boolean
  84. */
  85. protected function setupFS($user) {
  86. \OC_Util::tearDownFS();
  87. \OC_Util::setupFS($user);
  88. // Check if this user has a trashbin directory
  89. $view = new \OC\Files\View('/' . $user);
  90. if (!$view->is_dir('/files_trashbin/files')) {
  91. return false;
  92. }
  93. return true;
  94. }
  95. }