1
0

CleanPreviewsBackgroundJob.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * @copyright 2016 Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Julius Härtl <jus@bitgrid.net>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OC\Repair\Owncloud;
  24. use OC\BackgroundJob\QueuedJob;
  25. use OCP\AppFramework\Utility\ITimeFactory;
  26. use OCP\BackgroundJob\IJobList;
  27. use OCP\Files\Folder;
  28. use OCP\Files\IRootFolder;
  29. use OCP\Files\NotFoundException;
  30. use OCP\Files\NotPermittedException;
  31. use OCP\IUserManager;
  32. use Psr\Log\LoggerInterface;
  33. class CleanPreviewsBackgroundJob extends QueuedJob {
  34. /** @var IRootFolder */
  35. private $rootFolder;
  36. private LoggerInterface $logger;
  37. /** @var IJobList */
  38. private $jobList;
  39. /** @var ITimeFactory */
  40. private $timeFactory;
  41. /** @var IUserManager */
  42. private $userManager;
  43. /**
  44. * CleanPreviewsBackgroundJob constructor.
  45. */
  46. public function __construct(IRootFolder $rootFolder,
  47. LoggerInterface $logger,
  48. IJobList $jobList,
  49. ITimeFactory $timeFactory,
  50. IUserManager $userManager) {
  51. $this->rootFolder = $rootFolder;
  52. $this->logger = $logger;
  53. $this->jobList = $jobList;
  54. $this->timeFactory = $timeFactory;
  55. $this->userManager = $userManager;
  56. }
  57. public function run($arguments) {
  58. $uid = $arguments['uid'];
  59. if (!$this->userManager->userExists($uid)) {
  60. $this->logger->info('User no longer exists, skip user ' . $uid);
  61. return;
  62. }
  63. $this->logger->info('Started preview cleanup for ' . $uid);
  64. $empty = $this->cleanupPreviews($uid);
  65. if (!$empty) {
  66. $this->jobList->add(self::class, ['uid' => $uid]);
  67. $this->logger->info('New preview cleanup scheduled for ' . $uid);
  68. } else {
  69. $this->logger->info('Preview cleanup done for ' . $uid);
  70. }
  71. }
  72. /**
  73. * @param $uid
  74. * @return bool
  75. */
  76. private function cleanupPreviews($uid) {
  77. try {
  78. $userFolder = $this->rootFolder->getUserFolder($uid);
  79. } catch (NotFoundException $e) {
  80. return true;
  81. }
  82. $userRoot = $userFolder->getParent();
  83. try {
  84. /** @var Folder $thumbnailFolder */
  85. $thumbnailFolder = $userRoot->get('thumbnails');
  86. } catch (NotFoundException $e) {
  87. return true;
  88. }
  89. $thumbnails = $thumbnailFolder->getDirectoryListing();
  90. $start = $this->timeFactory->getTime();
  91. foreach ($thumbnails as $thumbnail) {
  92. try {
  93. $thumbnail->delete();
  94. } catch (NotPermittedException $e) {
  95. // Ignore
  96. }
  97. if (($this->timeFactory->getTime() - $start) > 15) {
  98. return false;
  99. }
  100. }
  101. try {
  102. $thumbnailFolder->delete();
  103. } catch (NotPermittedException $e) {
  104. // Ignore
  105. }
  106. return true;
  107. }
  108. }